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

Re: st: RE: Taking averages, etc.


From   Allan Reese <[email protected]>
To   Stata distribution list <[email protected]>
Subject   Re: st: RE: Taking averages, etc.
Date   Wed, 17 Dec 2003 12:38:22 +0000 (GMT)

On Wed, 17 Dec 2003, Richard Williams wrote:
> 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 stata, ...
> 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 point is to analyse the logic.  Reforming a problem often speeds up
the execution far more than fancy coding, and leads to insights.  In this
case:
  gen y=5
  replace y=3 if x1==1 & x2==3
  replace y=4 if y==5 & (x3==2 & x4==17)

performs far fewer tests.  The parentheses do not affect the result but
emphasise the logic.

Stata, as Nick has already replied, has a completely general control
language that includes if/then/else, for and while.  Unlike SPSS, where
only data transformations can be put into DO IF and LOOP constructions,
Stata allows any statements or blocks of statements.  These may
conveniently be written using an editor (provided it doesn't add a file
extension!) and saved as .DO or .ADO on the fly.


R. Allan Reese                       Email:     [email protected]
Associate Manager GRI                Direct voice:   +44 1482 466845
Graduate School                      Voice messages: +44 1482 466844
Hull University, Hull HU6 7RX, UK.   Fax:            +44 1482 466436
====================================================================
Be careful in handling the battery of the remote control transmitter
If swallowed consult a physician immediately for emergency treatment
               [Safety instructions: Hitachi CP-X275 data projector]


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