I have an issue of not accurately reflecting the lag/lead variables if an individual migrates multiple times during the observational period.
I am using stata 15.0
pid = ID number of the individual, wave = time, M0 = If an individual migrated in that wave
I have made dummy variables for 3 lags and 3 leads for the act of migration, this is the code that I used.
This is it working as intended:
But when there are multiple acts of migration, it only takes account of the last migration of the individual
Is there a way to change the priority or should my code change in general?
I am using stata 15.0
pid = ID number of the individual, wave = time, M0 = If an individual migrated in that wave
I have made dummy variables for 3 lags and 3 leads for the act of migration, this is the code that I used.
Code:
tsset pid wave gen p = wave if M0 ==1 bys pid: egen time = max(p) gen timelength = time - wave gen lead1=1 if timelength ==1 replace lead1=0 if lead1 != 1 gen lead2=1 if timelength == 2 replace lead2=0 if lead2 != 1 gen lead3=1 if timelength == 3 replace lead3=0 if lead3 != 1 gen lead4=1 if timelength == 4 replace lead4=0 if lead4 != 1 gen lead5=1 if timelength == 5 replace lead5=0 if lead5 != 1 gen lead6=1 if timelength == 2 replace lead6=0 if lead6 != 1 gen lead7=1 if timelength == 2 replace lead7=0 if lead7 != 1 gen lag1= 1 if timelength == -1 replace lag1 = 0 if lag1 != 1 gen lag2= 1 if timelength == -2 replace lag2 = 0 if lag2 != 1 gen lag3= 1 if timelength == -3 replace lag3 = 0 if lag3 != 1 gen lag4= 1 if timelength == -4 replace lag4 = 0 if lag4 != 1 gen lag5= 1 if timelength == -5 replace lag5 = 0 if lag5 != 1
This is it working as intended:
HTML Code:
+-----------------------------------------------------------------------------------------+ | pid wave M0 p time timele~h lead1 lead2 lead3 lag1 lag2 lag3 | |-----------------------------------------------------------------------------------------| 131. | 10029133 12 0 . . . 0 0 0 0 0 0 | 132. | 10040331 6 0 . 7 1 1 0 0 0 0 0 | 133. | 10040331 7 1 7 7 0 0 0 0 0 0 0 | 134. | 10040331 8 0 . 7 -1 0 0 0 1 0 0 | 135. | 10040331 9 0 . 7 -2 0 0 0 0 1 0 | |-----------------------------------------------------------------------------------------| 136. | 10040331 10 0 . 7 -3 0 0 0 0 0 1 | 137. | 10040331 11 0 . 7 -4 0 0 0 0 0 0 | 138. | 10040331 12 0 . 7 -5 0 0 0 0 0 0 | 139. | 10040331 13 0 . 7 -6 0 0 0 0 0 0 | 140. | 10040331 14 0 . 7 -7 0 0 0 0 0 0 | |-----------------------------------------------------------------------------------------| 141. | 10040331 15 0 . 7 -8 0 0 0 0 0 0 | 142. | 10040331 16 0 . 7 -9 0 0 0 0 0 0 | 143. | 10040331 17 0 . 7 -10 0 0 0 0 0 0 | 144. | 10040331 18 0 . 7 -11 0 0 0 0 0 0 | 145. | 10040366 6 0 . . . 0 0 0 0 0 0 | |-----------------------------------------------------------------------------------------| 146. | 10040366 7 0 . . . 0 0 0 0 0 0 | 147. | 10040366 8 0 . . . 0 0 0 0 0 0 | 148. | 10040366 10 0 . . . 0 0 0 0 0 0 | 149. | 10040366 11 0 . . . 0 0 0 0 0 0 |
But when there are multiple acts of migration, it only takes account of the last migration of the individual
HTML Code:
+------------------------------------------------------------------------------------------+ | pid wave M0 p time timele~h lead1 lead2 lead3 lag1 lag2 lag3 | |------------------------------------------------------------------------------------------| 210. | 10048243 6 0 . 15 9 0 0 0 0 0 0 | 211. | 10048243 7 0 . 15 8 0 0 0 0 0 0 | 212. | 10048243 8 0 . 15 7 0 0 0 0 0 0 | 213. | 10048243 10 0 . 15 5 0 0 0 0 0 0 | 214. | 10048243 11 0 . 15 4 0 0 0 0 0 0 | |------------------------------------------------------------------------------------------| 215. | 10048243 12 0 . 15 3 0 0 1 0 0 0 | 216. | 10048243 13 1 13 15 2 0 1 0 0 0 0 | 217. | 10048243 14 0 . 15 1 1 0 0 0 0 0 | 218. | 10048243 15 1 15 15 0 0 0 0 0 0 0 | 219. | 10048243 16 0 . 15 -1 0 0 0 1 0 0 | |------------------------------------------------------------------------------------------| 220. | 10048243 17 0 . 15 -2 0 0 0 0 1 0 | 221. | 10048243 18 0 . 15 -3 0 0 0 0 0 1 |
Comment