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.
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.
Comment