Using the data below, how can I create lag and lead variables that simply count up or down by month (starting at 0) between incidents? I know the simple version to get a lag or lead of 1 would be:
But I haven't seen technique to simply count by increment.
Additionally, it'd be helpful to do this in 2 separate variables (one lag and one lead) to deal with the instances where a given person has multiple incidents, and the lag/lead stops wherever the next incident begins.
Data:
Code:
tsset person mdate by person: gen lead=incident[_n+1] by person: gen lag=incident[_n-1]
Additionally, it'd be helpful to do this in 2 separate variables (one lag and one lead) to deal with the instances where a given person has multiple incidents, and the lag/lead stops wherever the next incident begins.
Data:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte person str7 yrmth byte incident float mdate 1 "2010m1" 0 600 1 "2010m2" 0 601 1 "2010m3" 0 602 1 "2010m4" 0 603 1 "2010m5" 0 604 1 "2010m6" 0 605 1 "2010m7" 1 606 1 "2010m8" 0 607 1 "2010m9" 0 608 1 "2010m10" 0 609 1 "2010m11" 0 610 1 "2010m12" 0 611 1 "2011m1" 0 612 1 "2011m2" 0 613 1 "2011m3" 1 614 1 "2011m4" 0 615 1 "2011m5" 1 616 1 "2011m6" 0 617 2 "2010m1" 0 600 2 "2010m2" 1 601 2 "2010m3" 0 602 2 "2010m4" 0 603 2 "2010m5" 0 604 2 "2010m6" 0 605 2 "2010m7" 0 606 2 "2010m8" 0 607 2 "2010m9" 0 608 2 "2010m10" 0 609 2 "2010m11" 0 610 2 "2010m12" 0 611 2 "2011m1" 0 612 2 "2011m2" 0 613 2 "2011m3" 0 614 2 "2011m4" 0 615 2 "2011m5" 0 616 2 "2011m6" 0 617 3 "2010m1" 0 600 3 "2010m2" 0 601 3 "2010m3" 0 602 3 "2010m4" 1 603 3 "2010m5" 0 604 3 "2010m6" 1 605 3 "2010m7" 0 606 3 "2010m8" 0 607 3 "2010m9" 1 608 3 "2010m10" 0 609 3 "2010m11" 0 610 3 "2010m12" 0 611 3 "2011m1" 0 612 3 "2011m2" 0 613 3 "2011m3" 0 614 3 "2011m4" 1 615 3 "2011m5" 0 616 3 "2011m6" 0 617 end format %tm mdate
Comment