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

#delimit ;
gen Y =
cond(X1 == 1 & X2 == 3, 3,
cond(X3 == 2 & X4 == 17, 4,
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.

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

Richard Williams, Associate Professor
OFFICE: (574)631-6668, (574)631-6463
FAX: (574)288-4373
HOME: (574)289-5227
EMAIL: Richard.A.Williams.5@ND.Edu
WWW (personal):
WWW (department):

* For searches and help try:

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