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

Re: st: RE: Taking averages, etc.

From   Richard Williams <[email protected]>
To   [email protected]
Subject   Re: st: RE: Taking averages, etc.
Date   Wed, 17 Dec 2003 07:15:37 -0500

At 10:55 AM 12/17/2003 +0000, Nick Cox wrote:
Non-programmers should note the crucial difference
in principle between

1. if <condition> <command>


2. <command> if <condition>

Form 1 carries out _one_ test of the <condition>
supplied. If it is true, <command> is carried
out, but not otherwise.

Form 2 carries a test of the <condition>
supplied for _every_ observation specified
and then carries out <command>
for the observations for which it is true.
That is very interesting Nick and something I might well have screwed up sooner or later. Form 1 looks very much like the SPSS if command; but it works very differently. Form 2 is what SPSS users will want as their equivalent to the SPSS if.

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

Richard Williams, Associate Professor
OFFICE: (574)631-6668, (574)631-6463
FAX: (574)288-4373
HOME: (574)289-5227
EMAIL: [email protected]
WWW (personal):
WWW (department):

* For searches and help try:

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