I have a dataset of individuals that has a variable for the date on which they were interviewed (interview_date) and their county of residence. I'll call this Dataset A.
I have a second dataset of incidents that includes the date of the incident (inc_date) and the county in which the incident occurred. I'll call this Dataset B.
For each individual in dataset A, I want to calculate the total number of incidents from dataset B that occurred in their county of residence over the five years prior to the interview.
So if individual 1001 lives in Los Angeles County and was interviewed on March 1, 2020, I want to sum all incidents in dataset B in Los Angeles County between the dates of 3/1/2015 and 3/1/2020.
If I were doing this in Python, I would loop through the individuals in A, grab the date, sum the variables in B, and enter the totals in a new variable in A. I think I could do this by having each dataset open in a different frame, looping through records in A, and then in the frame with B subset the records to the date range in the county and then collapse. But looping is wonky in Stata and I have a feeling that there should be an easier way to do this. Any suggestions would be greatly appreciated.
I have a second dataset of incidents that includes the date of the incident (inc_date) and the county in which the incident occurred. I'll call this Dataset B.
For each individual in dataset A, I want to calculate the total number of incidents from dataset B that occurred in their county of residence over the five years prior to the interview.
So if individual 1001 lives in Los Angeles County and was interviewed on March 1, 2020, I want to sum all incidents in dataset B in Los Angeles County between the dates of 3/1/2015 and 3/1/2020.
If I were doing this in Python, I would loop through the individuals in A, grab the date, sum the variables in B, and enter the totals in a new variable in A. I think I could do this by having each dataset open in a different frame, looping through records in A, and then in the frame with B subset the records to the date range in the county and then collapse. But looping is wonky in Stata and I have a feeling that there should be an easier way to do this. Any suggestions would be greatly appreciated.
Comment