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]
Re: st: lags and leads
From
Claudius Li <[email protected]>
To
[email protected]
Subject
Re: st: lags and leads
Date
Tue, 11 Dec 2012 16:04:40 -0500
Thanks for all the comments on this. That seems to clean up my code a bit.
On Tue, Dec 11, 2012 at 1:43 PM, Nick Cox <[email protected]> wrote:
> You have panel data, so a separate variable for every state is a bad
> idea. Most things you might want to do with panel data are difficult
> or impossible with this structure. Even simple data management tasks
> will often require loops, as in this posting.
>
> With a better structure as obtained by -reshape long- you should also
> avoid subscripts. You should use time-series operators instead. See
> -help varlist-. Such operators will handle both panels and gaps
> correctly.
>
> In terms of your specific question, yes; -substr()- cannot be used like that.
>
> gen substr(`var',1,2)score+lag`dist' = `var'[_n-`dist']
>
> should be
>
> gen `=substr(`var',1,2)'scorelag`dist' = `var'[_n-`dist']
>
> but as above this is not recommended any way.
>
> Nick
>
> On Tue, Dec 11, 2012 at 6:26 PM, Claudius Li
> <[email protected]> wrote:
>
>> 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?
> *
> * 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/