Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: How to tell a macro to use previous global value of that loop, akin to [_n-1] for columns


From   "Eva Poen" <[email protected]>
To   [email protected]
Subject   Re: st: How to tell a macro to use previous global value of that loop, akin to [_n-1] for columns
Date   Sun, 17 Aug 2008 13:43:14 +0100

Jared,

I am slightly confused. You talk about global macros in your message,
but there is no global macro to be seen in your code.

I assume that your line
               replace LHS_NMCE_`X'=5 if LHS_NMCE_`X'==. & LHS_NMCE_`X-1'=1
is part of a loop:

forvalues X = 2/10 {
               replace LHS_NMCE_`X'=5 if LHS_NMCE_`X'==. & LHS_NMCE_`X-1'=1
}
which is illegal syntax.

And I also assume that you don't want to hard code the number 5 in
here, but instead mean `X'-1. So, it appears that you have variables
LHS_NMCE_1 to LHS_NMCE_10, where the number at the end represents the
year. These variables are equal to 1 if the firm is present in that
year, and they are missing if it is not. You want to record the year
of exit, i.e. the last year for which the variable LHS_NMCE is not
missing for every observation. Am I correct so far?

If you want to keep your data in wide format, as you have it now, you
only need a little finetuning to your loop (no global macros needed at
all):

gen ExitYear = .
forvalues X = 2/10 {
               replace ExitYear= `X'-1 if LHS_NMCE_`X'==. & LHS_NMCE_`=`X'-1'==1
}

Hope this helps,
Eva


2008/8/17 Jared Fotis <[email protected]>:
> Sacrificial First Line...
> Please excuse the elementary nature (hopefully) of this question, but
> I've tried all of the key words to search for in the archives, etc. am
> at a lose as to how to describe the question to the search engine.
> What is the proper code to tell stata to take the previous value of
> `X' in the associated "global" for the macro?
>
> I am writing a macro to record the year and value of a firm
> disappearing from the data set, meaning that if a firm exits in year 7
> I want to the macro to record the value in year 6.  Firms are in rows
> and years are in columns with the associated varname.  How do I tell
> the macro to take the previous year (6) for that loop's "X" value (7)
> in the macro itself?
>
> Example with a global of 1 to 10:
>                replace LHS_NMCE_`X'=5 if LHS_NMCE_`X'==. & LHS_NMCE_`X-1'=1
>
> Here I try `X-1', but have also attemped (`X'-1), `(X-1)' and
> `X[_n-1]' to no avail.  Thanks in advance.
>
> Best,
>
> Jared
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index