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]

st: RE: A simple Stata Macro trick?!... I need your help please


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: A simple Stata Macro trick?!... I need your help please
Date   Wed, 27 Jul 2011 18:59:54 +0100

I guess that you want 

foreach item in X Y {
	bysort ID (DATE): gen `item'5=`item'[_n+5]
}

as otherwise the groups defined by -ID DATE- will just be single observations. 

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

John Litfiba

I do not know what is wrong in the following code:

X and Y are variables in my database

foreach item in  "X" "Y" {
bysort ID DATE: gen `item'5=`item'[_n+5]
}

That is, I would like to use a loop over these 2 variables names X and
Y that would allow the creation of new numeric variables called "X5"
and "Y5" and whose value are lagged copies of existing variables X and
Y... (5 row later)
It seems that the new variable are created correctly, but Stata do not
understand that it must EVALUATE the variable stored in `item[_n+5] :
so I get a missing value instead of the numeric value stored at the
nth+5 row...


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