Hello,
I have a panel dataset at the year-quarter-id level. I would like to create an indicator variable that takes ranges from -3 to 3, with the event occurring at 0.
I have found an old solution here , and it almost works.
Below is the code, copied from the linked solution:
A limited example is below, with an event in 1974. As you can see, for some IDs there are missing variables. I think this is because there are some missing quarter-years for some variables. For example, I looked at id = 71 and id=97 for the two years, and while 71 had the full 8 quarter-year observations, 97 only had 6. Importantly, 97 also didn't have an observation in the event year.
Any suggestions welcome! Happy to use more concise code, as well. Thank you.
Edit: tried to fix title (was supposed to say "with unbalanced panel" but can no longer edit title)
I have a panel dataset at the year-quarter-id level. I would like to create an indicator variable that takes ranges from -3 to 3, with the event occurring at 0.
I have found an old solution here , and it almost works.
Below is the code, copied from the linked solution:
Code:
gen prev = year if event == 1 bysort id year (prev) : replace prev = prev[1] if prev[1] == 1 bysort id (year) : replace prev = prev[_n-1] if mi(prev) gen forward = year - prev gen negdate = -year gen next = year if event == 1 bysort id negdate (year) : replace next = next[1] if next[1] == 1 bysort id (negdate) : replace next = next[_n-1] if mi(next) gen backward = year - next gen timeline = cond(abs(forward) <= abs(backward), forward, backward)
A limited example is below, with an event in 1974. As you can see, for some IDs there are missing variables. I think this is because there are some missing quarter-years for some variables. For example, I looked at id = 71 and id=97 for the two years, and while 71 had the full 8 quarter-year observations, 97 only had 6. Importantly, 97 also didn't have an observation in the event year.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte(id event) str6 date int prev byte forward int(negdate next) byte(backward timeline) 71 0 "1970q1" . . -1970 1974 -4 -4 71 0 "1970q2" . . -1970 1974 -4 -4 71 0 "1970q3" . . -1970 1974 -4 -4 71 0 "1970q4" . . -1970 1974 -4 -4 71 1 "1974q1" 1974 0 -1970 1974 0 0 71 1 "1974q2" 1974 0 -1970 1974 0 0 71 1 "1974q3" 1974 0 -1970 1974 0 0 71 1 "1974q4" 1974 0 -1970 1974 0 0 97 0 "1969q1" . . -1969 . . . 97 0 "1969q2" . . -1969 . . . 97 0 "1969q3" . . -1969 . . . 97 0 "1969q4" . . -1969 . . . 97 0 "1970q1" . . -1970 . . . 97 0 "1970q1" . . 1970 . . . end
Any suggestions welcome! Happy to use more concise code, as well. Thank you.
Edit: tried to fix title (was supposed to say "with unbalanced panel" but can no longer edit title)
Comment