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" <eva.poen@gmail.com>
To   statalist@hsphsun2.harvard.edu
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 15:36:46 +0100

Jared,

now I understand better. For future postings, just be a bit careful
with your wording. In Stata, the word "macro" refers to something very
specific: either a global macro or a local macro (see -help macro-). A
loop is just a loop (there is no such thing as a "global loop"). And
one or several lines of Stata commands are usually refered to as
"code". The only reason I am saying this is that it will help other
people understand better what you are doing, and therefore make it
easier for them to help you.

Now to your question:
I believe that I answered it with my last reply. Note the last bit of this line:

replace ExitYear= `X'-1 if LHS_NMCE_`X'==. & LHS_NMCE_`=`X'-1'==1

The key is in the expression - if ... LHS_NMCE_`=`X'-1'==1-. Your
[`_n'-1] for loops is my `=`X'-1'. Here is how it works: The first
left single quote and equal sign tell Stata that you want it to
evaluate (i.e. substitute and calculate) whatever follows. The next
thing is `X'-1. `X', as you know, is the placeholder in the loop and
refers to the current value. So, `X'-1 would be the previous value in
the loop, which is what you are after. The whole thing closes with a
closing single quote, to mark the end of the evaluation. So, if the
current value for `X' is 8, the above expression evolves to -if ...
LHS_NMCE_7==1-, which I hope is just what you want.

Hope this helps,
Eva


2008/8/17 Jared Fotis <kingjared@gmail.com>:
> Eva,
>
> Thank you for the timely response on a sunday.  Indeed that is but one
> line code for a long macro that i thought it better not to post in its
> entirety.  You are correct that i have vars LHS_NMCE_1 through _10,
> however these variables are equal to 1 2 3 or 4 representing the group
> of that firm for that year, which can be continuously changing.
> Therefore, the ==5 and ==1 in the example are meant to be hard code as
> the 1 represents the associated group for that firm in the year before
> that firm exited (LHS_NMCE_* value in the previous `X' global loop)
> while the 5 signals the year after a group 1 firm disappears
> (replacing the blank LHS_NMCE_* for that specific `X' loop).  This
> line of code is then repeated for groups 2 3 and 4 by replacing them
> with 6 7 and 8, respectively, in the first year after the firm exited.
>
> This is all done to see the rate at which firms from different groups
> drop out of the data by comparing the number of 5's in that year to
> the total number of 1's in the year before (while dropping out firms
> that have already exited, via a series of "save" and "use" commands in
> the macro, so that they are not double counted).  As my question only
> pertains to the way in which you tell stata to use the value of the
> previous loop (or even just the variable with a name of that loop
> value minus 1, if forgetting about the values in the global helps), I
> thought it unnecessary to go into such detail.  The reference to
> [_n-1], which refers to the value of the previous row, is that I am
> hoping there is an equal expression to instruct stata to refer to the
> previous value of the global loop it is then running.  Does such a
> shortcut exists, and if not, has anyone found a solution in their own
> research?
>
> 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index