Hi there,
I have a dataset with patients having more than one visit. I need to define an episode and analyse the data based on the episode time frame. For this I had to create an episode variable based on the visiting time. For example if a person has multiple visits within 30 days that is considered as episode 1. If this person has another visit after the first 30 days that is considered episode 2 and so on and so forth.
I am doing it using this code below but I am wondering if there is a iterative way of doing it faster:
In my code below I identify the earliest visit date (visit_date) for each patient (patid) and define the lag period (diff_time1) between every other date and the first_time_visited). Then I include in episode 1 all those visits that happened within 30 days. The next step is I identify the earliest time for the second visit and similarly define the difference of every other visit with the earliest second time only for the visits that were not included in episode 1. Again every visit taken place within 30 days is considered so episode. I continue like this until I define every other episode.
Would appreciate any suggestion on making this faster.
*Define episode 1
egen first_time_visited=min(visit_date), by(patid)
gen diff_time1=.
by patid: replace diff_time=visit_date-first_time_visited
gen episode=.
replace episode=1 if diff_time1<=30
*Define episode 2
egen second_time_visited=min(visit_date) if episode=., by(patid)
gen diff_time2=.
by patid: replace diff_time2=visit_date-second_time_visited
replace episode=2 if diff_time2<=30
*Define episode 3
egen third_time_visited=min(visit_date) if episode=., by(patid)
gen diff_time3=.
by patid: replace diff_time3=visit_date-third_time_visited
replace episode=3 if diff_time3<=30
Thank you so much!
Adriana
I have a dataset with patients having more than one visit. I need to define an episode and analyse the data based on the episode time frame. For this I had to create an episode variable based on the visiting time. For example if a person has multiple visits within 30 days that is considered as episode 1. If this person has another visit after the first 30 days that is considered episode 2 and so on and so forth.
I am doing it using this code below but I am wondering if there is a iterative way of doing it faster:
In my code below I identify the earliest visit date (visit_date) for each patient (patid) and define the lag period (diff_time1) between every other date and the first_time_visited). Then I include in episode 1 all those visits that happened within 30 days. The next step is I identify the earliest time for the second visit and similarly define the difference of every other visit with the earliest second time only for the visits that were not included in episode 1. Again every visit taken place within 30 days is considered so episode. I continue like this until I define every other episode.
Would appreciate any suggestion on making this faster.
*Define episode 1
egen first_time_visited=min(visit_date), by(patid)
gen diff_time1=.
by patid: replace diff_time=visit_date-first_time_visited
gen episode=.
replace episode=1 if diff_time1<=30
*Define episode 2
egen second_time_visited=min(visit_date) if episode=., by(patid)
gen diff_time2=.
by patid: replace diff_time2=visit_date-second_time_visited
replace episode=2 if diff_time2<=30
*Define episode 3
egen third_time_visited=min(visit_date) if episode=., by(patid)
gen diff_time3=.
by patid: replace diff_time3=visit_date-third_time_visited
replace episode=3 if diff_time3<=30
Thank you so much!
Adriana
Comment