From | Richard Williams <[email protected]> |
To | [email protected] |
Subject | Re: st: RE: Taking averages, etc. |
Date | Wed, 17 Dec 2003 14:18:09 -0500 |
At 01:24 PM 12/17/2003 -0500, David Kantor wrote:
I have written about this previously, but I feel compelled to state this again. Some of us may find it confusing, but I always do these kinds of operations using nested cond() functions:I like this! It seems to do exactly what I had in mind in terms of being a counterpart to the SPSS DO IF command. I believe it is also very similar to how you would do this in Excel if you were trying to fit the formula into a cell instead of writing a macro. My only objection, as you also note, is that this syntax is harder to read than the SPSS counterpart, but splitting things up onto separate lines probably helps.
#delimit ;
gen Y =
cond(X1 == 1 & X2 == 3, 3,
cond(X3 == 2 & X4 == 17, 4,
5));
Actually, I made the mistake, not Fred :) But I think this underscores that it would be easy (at least for somebody like me) to make coding mistakes if there were a lot of conditions involved.As Fred wrote: 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 First, this needs a correction:
© Copyright 1996–2024 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |