Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Taking averages, etc.


From   Fred Wolfe <[email protected]>
To   [email protected]
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 like

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.

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?
I 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.

gen use1 = if X1 !=1 & X2 !=3 & X3==2 & X4==17
gen use2 = if X1 !=1 & X2 !=3 & X3 !=2 & X4 !=17

and so on. Then use these variables, as in replace y = 5 if use2

If the conditions are mutually exclusive, you can put the conditions into a single categorical variable.

Another way handle the problem is with a text editor where copying and replacing is simple.

Some of this


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.
can be programmed into a cond() function.

However, as Nick will probably tell you soon, there is much to said for simplicity and clarity. I prefer to define the if condition into individual variables together with textual notes or labels to state what the variables mean. Complicated code is all well and good when you are writing it, but when you come back to the program 6 months later the code is very difficult to understand.

Fred


Fred Wolfe
National Data Bank for Rheumatic Diseases
Wichita, Kansas
Tel (316) 263-2125 Fax (316) 263-0761
[email protected]


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