Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: Converting a SAS datastep to Stata


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Converting a SAS datastep to Stata
Date   Wed, 15 Dec 2010 12:12:25 +0000

If I understand that SAS code correctly, and I've never used SAS in my life, an equivalent would be 

gen lvalue1 = expr1 if flpdyr > 1993 & flpdyr < 1998
gen lvalue2 = expr2 if flpdyr > 1993 & flpdyr < 1998

In fact 

if flpdyr > 1993 & flpdyr < 1998

could be translated to 

if inrange(flpdyr, 1994, 1997)

which isn't much shorter but is likely to match the way you think more closely. (Here I am taking it from context that year variables take integer values only.) 

There are problems on various levels with your code. 

1. There is no comma before -if-. Such commas precede options, and -if- is not an option. 

2. The creation of an extra variable -sample- does no great harm but is not needed. 

3. Your -generate sample = 1 if ...- would generate a variable that could be 1 or missing. Any subsequent -if sample- would select both the 1s and the missings as missings are non-zero in Stata. 

For more on the last point, see

FAQ     . . . . . . . . . . . . . . . . . . . . . . .  True and false in Stata
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        2/03    What is true and false in Stata?
                http://www.stata.com/support/faqs/data/trueorfalse.html

In essence, yes: an -if- qualifier applying, or not applying, observation by observation must be repeated on every command to which it applies. 

Whatever SAS code has been waved in front of me over the years has not given me the impression that it is a much more concise and elegant language! 

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

Daniel Feenberg

[...] 

Even in Stata, I am worried a bit about translating block if statements. 
The SAS block:

    if flpdyr > 1993 & flpdyr < 1998 then do;
        lvalue1 = expr1;
        lvalue2 = expr2;
     end;

could  be translated to:

    gen sample = 1, if flpdyr >1993 & flpdyr < 1998
    gen lvalue1 = expr1, if sample
    gen lvalue2 = expr2, if sample

but that requires redundant and repetive code. There are many blocks and 
some are 50 to 100 lines long. I am still surprised there isn't any syntax 
to apply the "if qualifier" to a block of code, and the ability to nest 
such blocks. Or am I missing something again?

[...] 

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index