I have a dataset with many subjects (patient-IDs) with many observations per patient. I would like to find out how many patients that got medicine 1 (med1) each year. In the example dataset med1 is given three times in 2012, but only to two different patients.
I tried
bys PatientID (med1): gen med1pat=!mi(med1[1])
to first identify all patients that got med1 at any time.
Then
by PatientID: gen med1pat_2012=1 if med1==1 & year==2012
by PatientID: gen uniqu_med1pat_2012=1 if _n==1 & med1pat_2012==1
by PatientID: gen med1pat_2013=1 if med1==1 & year==2013
by PatientID: gen uniqu_med1pat_2013=1 if _n==1 & med1pat_2013==1
This gave me the right answer for 2012, med1 given three times (tab med1pat_2012: 3) and to two specific patients (tab unique_med1pat_2012: 2)
But for 2013 it got wrong...
Thank you for helping.
I tried
bys PatientID (med1): gen med1pat=!mi(med1[1])
to first identify all patients that got med1 at any time.
Then
by PatientID: gen med1pat_2012=1 if med1==1 & year==2012
by PatientID: gen uniqu_med1pat_2012=1 if _n==1 & med1pat_2012==1
by PatientID: gen med1pat_2013=1 if med1==1 & year==2013
by PatientID: gen uniqu_med1pat_2013=1 if _n==1 & med1pat_2013==1
This gave me the right answer for 2012, med1 given three times (tab med1pat_2012: 3) and to two specific patients (tab unique_med1pat_2012: 2)
But for 2013 it got wrong...
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte PatientID int year byte med1 float(med1pat uniqu_med1pat med1pat_2012 uniqu_med1pat_2012 med1pat_2013 uniqu_med1pat_2013) 1 2012 1 1 1 1 1 . . 1 2014 1 1 . . . . . 1 2012 1 1 . 1 . . . 1 2013 . 1 . . . 1 . 1 2015 . 1 . . . . . 2 2013 1 1 1 . . 1 1 2 2014 1 1 . . . . . 2 2014 1 1 . . . . . 2 2015 . 1 . . . . . 2 2012 . 1 . 1 . . . 3 2012 . 0 . . . . . 3 2014 . 0 . . . . . 3 2015 . 0 . . . . . 3 2013 . 0 . . . . . 4 2012 1 1 1 1 1 . . 4 2013 1 1 . . . 1 . 4 2015 1 1 . . . . . 4 2015 1 1 . . . . . 4 2014 . 1 . . . . . end
Comment