# RE: st: STATA Vs. SAS

 From "Nick Cox" To Subject RE: st: STATA Vs. SAS Date Wed, 27 Nov 2002 19:33:46 -0000

```Richard Herrell
>
> Nick is absolutely right that part of the problem is simply
> not seeing how
> to do it in Stata.  I know I'd use Stata more if I didn't
> simply have to
> face learning how to do something in a new language when I
> could do it in
> my sleep in SAS.  Every time I spend time working in Stata
> I'm impressed
> by how well it's put together but end up returning to SAS for data
> management.
>
> One example of something I couldn't see how to do in Stata: In SAS I
> frequently use if ... then/else if ... then/else structures
> in the data
> step.  For example, in SAS I'd write:
>
> if x=0 then y=1;
> else if 1 < x < 10 then y=2;
> else if 11 < x < 20 then y=3;
> else y=.
>
> I know I can write a series if...then statements in Stata,
> but writing a
> structured statement that ends when the first true
> condition is met is
> more efficient code, and I find I seem to have a hard time
> getting all the
> conditions met correctly in Stata when the conditions get
> complicated.

There is more than one way to do it, but I
think slow and steady is best here:

gen y = .
replace y = 1 if x==0
replace y = 2 if x > 1 & x < 10
replace y = 3 if x > 11 & x < 20

-inrange()- could be used for conditions
defining closed intervals such as 1 <= x <= 10,
but your example has open intervals.

What's crucial to SAS users coming to Stata
is the distinction between the -if- command
and the -if- qualifier explained at
http://www.stata.com/support/faqs/lang/ifqualifier.html

Nick
n.j.cox@durham.ac.uk

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