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]

Re: st: lags and leads


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: lags and leads
Date   Tue, 11 Dec 2012 18:43:18 +0000

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
<claudiusli@janysanalytics.com> 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/


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