Statalist The Stata Listserver

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

Re: st: Problem with "If" statement

From   Keith Dear <>
Subject   Re: st: Problem with "If" statement
Date   Mon, 05 Jun 2006 16:04:27 +1000

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.

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

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.


* For searches and help try:
*   For searches and help try:

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