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: lags and leads


From   Claudius Li <claudiusli@janysanalytics.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: lags and leads
Date   Tue, 11 Dec 2012 13:26:39 -0500

Hi,
I have a panel dataset with a "score" and "count" variable for every
state (ie akscore, akcount, alscore, alcount, etc.). I want to create 7
leads and lags of each of these variables (ie akscorelag1 ..
akscorelag7, alscorelag1 .. alscorelag7, akscorelead1 .. akscorelead7,
alscorelead1 .. akscorelead7, akcountlag1 .. akcountlag7, etc.).
So I've got:

order *score, sequential
order *count, sequential
foreach var of varlist *score{
	forvalues dist = 1/7{
		gen `var'lag`dist' = `var'[_n-`dist']
		gen `var'lead`dist' = `var'[_n+`dist']
	}
}

foreach var of varlist *count{
	forvalues dist = 1/7{
		gen `var'lag`dist' = `var'[_n-`dist']
		gen `var'lead`dist' = `var'[_n+`dist']
	}
}

That works but I want to combine the two loops so I tried:

order *score, sequential
order *count, sequential
foreach var of varlist *score{
	forvalues dist = 1/7{
		gen substr(`var',1,2)score+lag`dist' = `var'[_n-`dist']
		gen substr(`var',1,2)count+lag`dist' = `var'[_n-`dist']
		gen substr(`var',1,2)score+lead`dist' = `var'[_n+`dist']
		gen substr(`var',1,2)count+lead`dist' = `var'[_n+`dist']
	}
}

but I get:
( invalid name

Am I using substr wrong? Is there a better way to do this?

-Claudius

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


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