Announcement

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

  • Creating leading variable

    Dear all,

    I have a question regarding creating a leading variable.
    In my data, I have some missing years from different companies and I only want to lead successive year. So I know this code for creating a lagged variable:
    by state: gen lag1 = x[_n-1] if year==year[_n-1]+1

    For my dataset, the code would be:

    by gvkey: gen tobinsqlead = tobinsq_w[_n-1] if year==year[_n-1]+1

    Only thing is, this is the code for creating a lagged variable if you have missing years, but I want to create a leading variable.
    I am not able to rewrite the code into a leading one. When I "play" with the - and + I constantly get the message "weights not allowed"
    Can someone help me with this?

    Regards,

  • #2
    I could show you how to fix up that code. But, why go to all this trouble? It is unlikely you really need a variable that contains the lead (or lag) of another variable in your data set.

    Code:
    xtset gvkey year
    will lay the groundwork for using time-series operators (-help tsvarlist- for more information). Then if you need to use the lead of tobinsq in some command just write F.tobinsq where you would otherwise write the name of such a variable. Stata will do the calculation of the lead values on the fly for you without cluttering up your dataset with a new "variable" that contains no new information.

    In the unlikely event you need to use leads or lags in a command that does not allow time-series operators, then you can go ahead and create your lead variable, before that command, with
    Code:
    gen tobinsqlead = F.tobinsq
    and use that.

    Nearly all Stata commands for which the use of lagged or leading values would be relevant allow time-series operators. Working with them is very easy and avoids the potential errors of "rolling your own."

    Comment


    • #3
      Thank you very much for your help!

      Comment

      Working...
      X