Bookmark and Share

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]

st: lags and leads


From   Claudius Li <[email protected]>
To   [email protected]
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index