|From||Fred Wolfe <firstname.lastname@example.org>|
|Subject||Re: st: RE: Taking averages, etc.|
|Date||Wed, 17 Dec 2003 06:34:01 -0600|
I'm probably just missing it, but it seems that Stata does not have a convenient if-then-else structure, i.e. the equivalent of SPSS's DO IF command. In SPSS, I could do something likeI suspect the way most of us handle this type of problem (which we don't see as much of a problem at all) is to make new variables to define the if condition.
DO IF X1 = 1 and X2 = 3
Compute Y = 3.
ELSE IF X3 = 2 and X4 = 17
Compute Y = 4.
Compute Y = 5.
In SPSS, this would result in Y equaling 3, 4, 5, or missing for all cases. If, say, X1 = 1, X2 = 3, X3 = 2, X4 = 17, Y would be coded 3, i.e. only the first compute would be executed.
In stata, it seems to me like I would have to write increasingly complicated if statements to achieve the same thing; something like
gen y = 3 if X1==1 & X2==3
replace y = 4 if X1 !=1 & X2 !=3 & X3==2 & X4==17
replace y = 5 if X1 !=1 & X2 !=3 & X3 !=2 & X4 !=17
or something like that. Is that correct, or is there an easier way I am overlooking?
can be programmed into a cond() function.DO IF X1 = 1 and X2 = 3 Compute Y = 3. ELSE IF X3 = 2 and X4 = 17 Compute Y = 4. ELSE Compute Y = 5. END IF.