# Re: st: Problem with "If" statement

 From Keith Dear To statalist@hsphsun2.harvard.edu Subject Re: st: Problem with "If" statement Date Mon, 05 Jun 2006 16:04:27 +1000

```Deepankar,
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.
kd

At 03:51 PM 5/6/06, you wrote:
```
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.

Any suggestions on how to tackle this issue will be greatly appreciated.

Deepankar

*
* 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/
```*
*   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/
```