Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to concisely generate many lagged variables

    Hello all,

    I am trying to generate a large number of lagged variables, all lagged once, and all mostly the same.

    I have 24 variables which exist, all of which I am trying to lag into new variables - only lagging each by one level. The code I am using for each individually is
    Code:
    bys country: gen l_[variable] = l.[variable]
    Do I need to write this out 24 times or is it possible to write a concise version of this? Thanks!

  • #2
    Code:
    xtset country year
    foreach var in var_a var_b var_c var_d var_e{
        gen l_`var'= L.`var'
    }
    In most cases, this is unnecessary as Stata supports time-series operators for most estimation commands. For example, if I need to regress the lags of mvalue and kstock on invest in the Grunfeld dataset:

    Code:
    webuse grunfeld, clear
    xtset company year
    regress invest L.(mvalue kstock)
    Res.:

    Code:
    . regress invest L.(mvalue kstock)
    
          Source |       SS           df       MS      Number of obs   =       190
    -------------+----------------------------------   F(2, 187)       =    340.11
           Model |  7220371.82         2  3610185.91   Prob > F        =    0.0000
        Residual |  1984955.18       187  10614.7336   R-squared       =    0.7844
    -------------+----------------------------------   Adj R-squared   =    0.7821
           Total |     9205327       189  48705.4339   Root MSE        =    103.03
    
    ------------------------------------------------------------------------------
          invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          mvalue |
             L1. |   .1212423   .0064621    18.76   0.000     .1084943    .1339903
                 |
          kstock |
             L1. |   .2503195   .0316839     7.90   0.000     .1878156    .3128233
                 |
           _cons |  -43.29118   10.93372    -3.96   0.000    -64.86048   -21.72188
    ------------------------------------------------------------------------------
    See

    Code:
    help tsvarlist
    Last edited by Andrew Musau; 18 Feb 2022, 10:10.

    Comment


    • #3
      Thanks!
      My one question is whether this would be possible with panel data? I forgot to mention, but the reason that it is -bys country- is that this is a panel dataset sorted by year by country - hence I was generating new variables rather than doing it within the regression itself as I was not sure how to get Stata to account for the groups.

      Comment


      • #4
        The xtset command already declares that the data is a panel to Stata. So Stata does take into account the panel structure of the data when creating lags. See

        Code:
        help xtset

        Comment


        • #5
          Ah yes, my mistake! Thank you so much for your help!

          Comment

          Working...
          X