[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
Fred Wolfe <fwolfe@arthritis-research.org> |

To |
statalist@hsphsun2.harvard.edu |

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

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?

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

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

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

fwolfe@arthritis-research.org

*

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

**Follow-Ups**:**Re: st: RE: Taking averages, etc.***From:*Richard Williams <Richard.A.Williams.5@nd.edu>

**References**:**st: RE: Taking averages, etc.***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**st: Taking averages, etc.***From:*"de la Garza, Adrian" <ADelagarza@imf.org>

**Re: st: RE: Taking averages, etc.***From:*Richard Williams <Richard.A.Williams.5@nd.edu>

- Prev by Date:
**Re: st: RE: Taking averages, etc.** - Next by Date:
**Re: st: RE: Taking averages, etc.** - Previous by thread:
**Re: st: RE: Taking averages, etc.** - Next by thread:
**Re: st: RE: Taking averages, etc.** - Index(es):

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