In the dataset below there are 5 variables: studyid (individual person identifier), offencedate (date an offence was committed), startdate (date service provided) and mode (type of service). modeorder is created by the code below
For each individual I want to find the startdate closest to each offence. Ideally I would do this for each mode and all modes combined.
The code below sorts the data but the mode order only lists the closest service date to the last offence, not to each offence for each studyid.
The output I am hoping for would be by studyid. For studyid =1, there are 7 offence dates, so for all modes combined there would be 7 rows, service closest to 16/06/2011 would be 3/10/11 In person, while for the last offence on 8/11/2011 In person
Any assistance is appreciated.
Data
For each individual I want to find the startdate closest to each offence. Ideally I would do this for each mode and all modes combined.
The code below sorts the data but the mode order only lists the closest service date to the last offence, not to each offence for each studyid.
The output I am hoping for would be by studyid. For studyid =1, there are 7 offence dates, so for all modes combined there would be 7 rows, service closest to 16/06/2011 would be 3/10/11 In person, while for the last offence on 8/11/2011 In person
Any assistance is appreciated.
Code:
by studyid (offencedate startdate mode), sort: generate modeorder= _n
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte studyid float(offencedate startdate) str9 mode float modeorder 1 18794 18652 "In Person" 1 1 18794 18653 "In Person" 2 1 18794 18696 "In Person" 3 1 19075 18859 "Telephone" 4 1 19086 18911 "In Person" 5 1 19086 18939 "In Person" 6 1 19086 18939 "In Person" 7 1 19086 18941 "Other" 8 1 19093 18933 "Other" 9 1 19093 18934 "In Person" 10 1 19130 18884 "In Person" 11 1 19130 18911 "In Person" 12 1 19130 19012 "Telephone" 13 1 19130 19044 "In Person" 14 1 19172 18859 "Telephone" 15 1 19172 18878 "In Person" 16 1 19172 18884 "In Person" 17 1 19172 18911 "In Person" 18 1 19172 18911 "In Person" 19 1 19172 18911 "In Person" 20 1 19172 18925 "In Person" 21 1 19172 18933 "Other" 22 1 19172 18934 "In Person" 23 1 19177 18939 "In Person" 24 1 19177 18939 "In Person" 25 2 19086 18925 "In Person" 1 2 19086 18933 "Other" 2 2 19086 18934 "In Person" 3 2 19086 18948 "Other" 4 2 19086 18953 "Other" 5 2 19093 18911 "In Person" 6 2 19093 19044 "In Person" 7 2 19130 18859 "Telephone" 8 2 19130 18878 "In Person" 9 2 19177 18925 "In Person" 10 2 19177 18933 "Other" 11 2 19177 18934 "In Person" 12 3 19093 18925 "In Person" 1 3 19093 19012 "Telephone" 2 3 19130 18911 "In Person" 3 3 19130 18911 "In Person" 4 3 19130 18955 "In Person" 5 3 19130 18976 "In Person" 6 3 19172 18939 "In Person" 7 3 19177 18911 "In Person" 8 3 19177 18911 "In Person" 9 end format %td offencedate format %td startdate
Comment