# st: RE: STATA equivalent to SAS subsetting "else if" statements?

 From "Nick Cox" <[email protected]> To <[email protected]> Subject st: RE: STATA equivalent to SAS subsetting "else if" statements? Date Tue, 8 Jul 2003 20:18:17 +0100

```Deborah Garvey

> I have generally used SAS for data manipulation, so I am
> not familiar with the best way to code the equivalent of
> the subsetting "else if" statement in STATA.
>
> This SAS code defines RACEETH hierarchically, based on the
> value of HISPANG and RACEG.  It only permits RACEETH to
> change once (from default missing to a valid value), at the
> first occurrence of a true IF statement:
>
> if hispang = 0 then do;
>   if raceg = 1 then raceeth = 1;
>   else if raceg = 2 then raceeth = 2;
>   else raceeth = 3;
>   end;
> else if hispang = 9 then raceeth = .N;
> else if hispang ge 1 and hispang < 9 then raceeth = 7;
>
> How do I accomplish this in STATA?

In Stata
(cf. http://www.stata.com/support/faqs/res/statalist.html#spell)
you have to flip everything round to go on the
right-hand side. Also == tests for equality; = assigns RHS
to LHS only.

This looks like

gen raceeth = .
replace raceeth = 1 if hispang == 0 & raceg == 1
replace raceeth = 2 if hispang == 0 & raceg == 2
replace raceeth = 3 if hispang == 0 & raceeth == .
replace raceeth = <whatever> if hispang == 9
replace raceeth = 7 if hispang >= 1 & hispang < 9

gen raceeth = 1 if hispang == 0 & raceg == 1
replace raceeth = 2 if hispang == 0 & raceg == 2
replace raceeth = 3 if hispang == 0 & raceeth == .
replace raceeth = <whatever> if hispang == 9
replace raceeth = 7 if hispang >= 1 & hispang < 9

Somebody else please say what .N is in SAS as I have
.Not a clue.

Nick
[email protected]
*
*   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/
```