Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Converting a SAS datastep to Stata
From
Austin Nichols <[email protected]>
To
[email protected]
Subject
Re: st: Converting a SAS datastep to Stata
Date
Tue, 14 Dec 2010 21:10:05 -0500
Dan <[email protected]>:
Even if you take out the illegal commas, that does not do what you want:
clear
range flpdyr 1990 2000 11
scalar expr1=1
scalar expr2=2
gen sample = 1 if flpdyr >1993 & flpdyr < 1998
gen lvalue1 = expr1 if sample
gen lvalue2 = expr2 if sample
list
since any nonzero value is "true" including missing--but try:
clear
range flpdyr 1990 2000 11
scalar expr1=1
scalar expr2=2
loc sample "if flpdyr >1993 & flpdyr < 1998"
gen lvalue1 = expr1 `sample'
gen lvalue2 = expr2 `sample'
list
But in general, it's often better to step back from your SAS code and
rewrite it in native Stata-ese.
For example, there are several kinds of -if- in Stata, but it's
possible you need none of them...
depending on what you want to accomplish in all of those blocks.
On Tue, Dec 14, 2010 at 8:16 PM, Daniel Feenberg <[email protected]> wrote:
>
> 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?
>
> I can see that in Mata there is such syntax, but apparently only if one
> loops over the observations, which I wish to avoid.
>
> Daniel Feenberg
*
* 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/