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


From   "Jacobs, David" <jacobs.184@sociology.osu.edu>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: lags and leads
Date   Tue, 11 Dec 2012 18:37:16 +0000

Why not just use Stata's convention for leads and lags?  Putting a -l.- before a variable lags it by one period.  Putting -l2.- lags it by 2 periods.  Putting -f2.- leads that variable by 2 periods.  

And you don't have to worry about lags slipping into the next case.  And and this convention works in front of parentheses, which will produce the same lags or leads for the all of the variables included within the parentheses.

Dave Jacobs

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Claudius Li
Sent: Tuesday, December 11, 2012 1:29 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: lags and leads

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/

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