# 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 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,
5));
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: [email protected]
WWW (personal): http://www.nd.edu/~rwilliam
WWW (department): http://www.nd.edu/~soc

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