All,

I've looked through the other topics regarding readmission and my question is slightly different. The variables are: unique id code, generic time (cannot be compared across patients), and case (based on diagnosis codes). The patients may have admissions prior to their first admission for the diagnosis of interest and I only want to keep the first 'case' admission and all subsequent admissions. These readmissions may be for the same or a different diagnosis.

I also want to identify next day admissions. I will drop the admit with next day admissions and keep the 2nd day hospital course - this particular disease often has same day and/or next day transfers. The database already collapses same-day, but not next-day admissions.

Below is an example dataset and the code that I *think* will work. I'm specifically unsure about step 4.

Thank you in advance for help and suggestions!

Rachel

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(visitlink daystoevent case)
1   4 0
1  10 1
1  16 0
1  25 1
1  40 0
1  55 0
1  79 0
1  80 0
2   2 0
2   3 0
2   8 0
2  25 1
2  66 0
2  67 0
2  90 0
2 110 1
3  25 0
3  45 1
3  79 1
3  90 1
4  95 0
4 100 0
4 150 0
4 151 0
end

Code:
*1* pt has a case admission
by visitlink(daystoevent): generate case_y = 1 if case[_n-1]==1
**will create a 1 for each admission for patients that have a case admission to first case admit

*drop all with blanks in case_y -> gets rid of admits prior to case admit
keep if case_y==1

*2***first observed case in each group
generate idx==0
by visitlink(daystoevent): replace idx=1 if case==1 = _n==1

*3****identify number of admits - gives total number of readmissions overall
by visitlink(daystoevent): gen n = _n 

*4*****carry over time of first for each readmission
by visitlink(daystoevent): egen first_daystoevent = min(cond(case_y == 1, daystoevent, .))

*5*****generate days from index event for each readmission
by visitlink(daystoevent): gen idx_days = daystoevent*idx
*idx=1 so idx_days = daystoevent (unique time identifier for each pt) others 0 
by visitlink(daystoevent): replace idx_days = daystoevent - first_daystoevent

*6**generate days from prior admit                              
by visitlink (daystoevent): gen: delta = idx_days - idx_days[_n-1] 
                                
*7**identify next day transfers
by visitlink(daystoevent): gen tnf_days = daystoevent + los if los==1
***gen discharge time for all admits with los= 1 rest are blank
*generate time from prior dc of admit with los==1
gen tnf_time = daystoevent - tnf_days[_n-1] if los[_n-1]==1
gen tnf = 1 if los[_n-1]==1 & tnf_time==0 if los[_n-1]==1