Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Problem with "If" statement


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Problem with "If" statement
Date   Mon, 5 Jun 2006 11:48:26 +0100

In addition to Keith's solution, note that there is an 
FAQ on the subject at 

FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if qualifier
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J. Wernow
        6/00    I have an if command in my program that only seems
                to evaluate the first observation, what's going on?
                http://www.stata.com/support/faqs/lang/ifqualifier.html

-search if- would point you to this. 

Nick 
[email protected] 

Keith Dear
 
> You don't need either sort of -if-.  Try it like this instead:
> gen byte p`size'`alive' = cond(`size'==`alive', 100, 10)
> Moreover there is then no need to initialise the pxy variables.

Deepankar

> >I am facing problems using the "if" statement (not the "if" 
> >qualifier) because the expression in the "if" statement has a 
> >variable. Hence, the commands are being executed taking only the 
> >first observation of the variable. What is the way to execute the 
> >commands for all the observations? I give a details of a prototype 
> >(simplified) problem below.
> >
> >I have two variables x and y. Each can take values 1 or 2. And I 
> >have four more variables p11, p12, p21 and p22. I want to go down 
> >each observation of x, compare it to y and if they are equal then 
> >make p`x'`y'=100, otherwise make p`x'`y'=10.
> >
> >To do the above task, I write the following program:
> >++++++++++++++++++++++++++++++++++++++
> >capture program drop rpc
> >program define rpc
> >version 8.0
> >args size alive
> >if `size'==`alive'  {
> >   replace p`size'`alive'=100
> >}
> >else  {
> >   replace p`size'`alive'=10
> >}
> >end
> >++++++++++++++++++++++++++++++++++++
> >
> >I run this program and then run the following do-file:
> >
> >+++++++++++++++++++++++++++++++++++
> >set more 1
> >version 8.0
> >local i 1
> >while `i' <=2  {
> >   local j 2
> >   while `j' <= `i'  {
> >     gen p`i'`j'=0
> >     local j = `j' + 1
> >   }
> >   local i = `i' + 1
> >}
> >
> >rpc x y
> >
> >+++++++++++++++++++++++++++++++++++++
> >
> >
> >My problem is that I cannot get the program to go through each 
> >observation on x and y. It takes the first observation of x and y 
> >and stops there. It does not move to the next observation.


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index