Thank you very much for answering.
I have also tried to use the rangejoin adapting from the above version. But I also did not quite get what I wish.
I did
and I got:
But what I really want to get is for example, person ID 1 should receive a count of crime committed after disease in the year 2000 = 0. The ID 1 should receive
So, I want to create a variable to calculate the number of crimes committed after illdate per year per person. If the person never got ill, but she appears in the ill2.dta, then I count the number of crimes in that yearexam anyway. That's is my focus is to exclude crimes that happened before the illdate (per person). I want to look just to crimes after the illdate. Another variable criminal takes 1 if the person committed any crime after the illdate (or if not ill any crime in that yearexam), 0 otherwise. And another variable with the total crimes committed after the illdate for all years. If the person appears as committing two crimes in exact same date, I am counting as just 1 crime. And I also would like to create these counts per type of crime (and in this case, I would count all crimes even if they happened in the same crimedate). Please, could you help me?
I have also tried to use the rangejoin adapting from the above version. But I also did not quite get what I wish.
I did
Code:
clear all use ill2, clear gen `c(obs_t)' obs_no = _n frame put _all if !missing(illdate), into(working) frame working { gen lower = illdate + 1 gen upper = illdate + 2900 replace lower = 1 if missing(illdate) replace upper = 0 if missing(illdate) format lower upper %td rangejoin crimedate lower upper using crimes2, by(id) by obs_no, sort: egen crimes_365 = total(crimedate > illdate + 1) by obs_no: egen crimes_2900 = total(crimedate > illdate + 2900) gen byte any_crime_365 = crimes_365 > 0 & !missing(crimes_365) gen byte any_crime_2900 = crimes_2900 > 0 & !missing(crimes_2900) by obs_no, sort: keep if _n == 1 } frlink 1:1 obs_no, frame(working) frget crimes_* any_crime_*, from(working)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(id yearexam yearill monthill dayill score illdate) byte(obs_no working) float(crimes_365 crimes_2190) byte(any_crime_365 any_crime_2190) 1 2000 2000 11 3 100 14917 1 1 3 0 1 0 1 2001 . . . 150 . 2 . . . . . 1 2002 . . . 111 . 3 . . . . . 2 2000 . . . 123 . 4 . . . . . 2 2001 2001 5 2 200 15097 5 2 2 0 1 0 2 2002 . . . 214 . 6 . . . . . 2 2003 . . . 203 . 7 . . . . . 2 2004 . . . 302 . 8 . . . . . 2 2005 . . . 136 . 9 . . . . . end format %td illdate
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(id yearexam yearill monthill dayill score illdate ncrimesinyear criminal) byte totalcrimes 1 2000 2000 11 3 100 14917 1 1 3 1 2001 . . . 150 . 0 0 3 1 2002 . . . 111 . 1 1 3 2 2000 . . . 123 . 0 0 2 2 2001 2001 5 2 200 15097 1 1 2 2 2002 . . . 214 . 1 1 2 2 2003 . . . 203 . 0 0 2 2 2004 . . . 302 . 0 0 2 2 2005 . . . 136 . 0 0 2 end format %td illdate
Comment