Announcement

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

  • Can't Lag time series (error: time variable not set, time variable must contain only integer values)

    I am trying to lag a time series variable (spread_eur, the spread between two interest rates).

    I tried using:
    Code:
    gen spread_eur_lag5 = spread_eur
    replace spread_eur_lag5 = spread_eur_lag5[_n-5]
    but this shifts it the wrong direction. I then tried with [_n+5] and Stata returns missing value for every value of the variable.

    I then tried with:
    Code:
    tsset spread_eur_test
    generate eurL1=L1.spread_eur_test
    And I get error messages:
    Code:
     time variable must contain only integer values
    and then:
    Code:
     time variable not set
    Using tsset should fix the second error, but I am not sure about the first one?

    Thank you!

  • #2
    When using -tsset-, you do not specify the variable that you want to calculate lags (or leads, etc.) of. You specify the time variable. You don't say what that variable is. Let me assume it's called date, and that it is a numeric Stata internal format variable and that your observations are evenly spaced 1 time unit apart.. (If it isn't, you need to convert it to one.)
    Code:
    tsset date
    gen spread_eur_lag_5 = L5.spread_eur
    Now this is loosely analogous to what you did with spread_eur[_n-5], except that it is safer and will properly handle data gaps, etc. But you said that moved things the wrong way. In that case, what you want is the 5 period lead, not the 5 period lag. That would be gotten by using F5 instead of L5.

    Needless to say, you provided no example data, so this code, which relies on assumptions about your data that could be wildly off the mark, may not actually work for you. In the future, whenever you want help with code, you must show example data. Otherwise, it is like asking for driving directions to 9 East 57 Street, New York, NY and saying of your starting point only that it is in the western hemisphere.

    It is also critical that the example data you give be shown in a form that is usable by those who want to help you. You should use the -dataex- command to do this. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data. (This is especially critical when dates are involved as these tend to show up in a wide variety of formats, and often require specific code to convert them to the format that is useful for the problem at hand.)

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X