Announcement

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

  • Confusion over creating lagged and lead variables for unbalanced panel data

    I am having trouble making making lead and lagged variables that take into account missing waves of information.
    I am using STATA 15.0.

    I plan on making 5 leads and 5 lag variables. In my piece, pid = ID number of the individual, wave = time, M0 = if an individual migrated in that wave.
    The code I used is as follows:

    sort pid wave
    by pid: gen lag1 = M0[_n-1] if wave==wave[_n-1]+1
    by pid: gen lag2 = M0[_n-2] if wave==wave[_n-1]+1
    by pid: gen lag1 = M0[_n-3] if wave==wave[_n-1]+1
    by pid: gen lag1 = M0[_n-4] if wave==wave[_n-1]+1
    by pid: gen lag1 = M0[_n-5] if wave==wave[_n-1]+1
    by pid: gen lead1 = M0[_n+1] if wave==wave[_n-1]+1
    by pid: gen lead2 = M0[_n+2] if wave==wave[_n-1]+1
    by pid: gen lead3 = M0[_n+3] if wave==wave[_n-1]+1
    by pid: gen lead4 = M0[_n+4] if wave==wave[_n-1]+1
    by pid: gen lead5 = M0[_n+5] if wave==wave[_n-1]+1

    From the snippet, between line 8-10, there is a wave missing (wave11) but it doesn't take into account of this as intended. How would I stop from recording lags/leads if there is a wave missing beforehand. In this example, as there isn't a wave 11, then there shouldn't be a recording of a lead for wave 10.

    p.s Although I said i was using 5 leads and the snippet shows 6, this was to highlight the problem with the code.
    Last edited by James Glass; 09 Apr 2018, 06:47.

  • #2
    Your image doesn't show up as readable, so even if it shows data we don't get a picture that we can use.

    Please go back to https://www.statalist.org/forums/help#stata and give us a readable, copyable data example to make clear what you have.

    Beyond that your code is certainly confused: once, for example, you have first issued

    Code:
    by pid: gen lag1 = M0[_n-1] if wave==wave[_n-1]+1
    all commands with gen lag1 = will fail as that variable already exists.

    We can help, but there is only one good way to get our help: to follow the advice worked out over 24 years that Statalist has been running and which everyone is asked to read before posting.

    Within http://www.statalist.org/forums/help please make sure to go all the way down to #18, even if you skim and skip material that doesn't seem relevant to you right now.

    Comment

    Working...
    X