forked from Black-Dog-Institute/Data-Files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAS CODE_ Transfers.sas
152 lines (130 loc) · 4.28 KB
/
SAS CODE_ Transfers.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
* Author: Timothy Dobbins and updated by Patrick Kelly
Purpose: Create code to identify transfers, including nested transfers
Date: June, 2012
Updated June 2014 to create stayseq, and to populate final length of stay to all records within a stay
Updated June 2016 to reflect name changes in APDC
Updated July 2020 to ensure that all nested episode are correctly included in the same stayseq;
DATA testtrans;
INPUT ppn episode_start_date ddmmyy9. episode_end_date ddmmyy9. mode_of_separation_recode;
FORMAT episode_start_date episode_end_date ddmmyy10.;
DATALINES;
1 02012004 04012004 1
1 06012004 20012004 1
1 08012004 08012004 1
1 10012004 12012004 1
1 14012004 15012004 1
1 20012004 20012004 1
1 23012004 25012004 1
2 02012004 02012004 1
2 05012004 06012004 5
2 06012004 07012004 9
2 07012004 18012004 1
2 10012004 11012004 1
2 13012004 13012004 1
3 31102004 13112004 5
3 04112004 04112004 1
3 07112004 07112004 5
3 13112004 14112004 1
;
PROC SORT DATA=testtrans;
BY ppn episode_start_date episode_end_date;
RUN;
DATA testtrans_seq;
SET testtrans;
BY ppn;
RETAIN morbseq stayseq;
* Generate FILESEQ and MORBSEQ;
fileseq=_n_;
IF first.ppn THEN morbseq=0;
morbseq=morbseq+1;
* Identify nested transfers and populate the start date, end date and final mode_of_separation_recode of nested transfers;
RETAIN nest_start nest_end nest_mode nested;
IF morbseq=1 THEN DO;
nest_start=episode_start_date;
nest_end=episode_end_date;
nest_mode=mode_of_separation_recode;
nested=0;
stayseq=0;
END;
IF morbseq>1 AND (episode_start_date <= nest_end & episode_end_date <= nest_end) THEN nested=nested+1;
ELSE DO;
nest_start=episode_start_date;
nest_end=episode_end_date;
nest_mode=mode_of_separation_recode;
nested=0;
END;
* Create lag variables;
lagsep = LAG(nest_end);
lagmode = LAG(nest_mode);
IF morbseq=1 THEN DO;
lagsep=.; lagmode=.;
END;
* Identify transfers as those with multiple records
(i.e. morbseq>1)
AND admission date before previous separation date
(i.e. overlapping transfer)
OR admission date after previous admission date AND separation date is before the previous record's separation date
(i.e. nested transfer)
OR transferred to another hospital (mode_of_separation_recode=5) AND
episode_start_date=previous episode_end_date (i.e. serial transfer)
OR type-change separation (mode_of_separation_recode=9) AND
episode_start_date = previous episode_end_date (i.e. type-change);
IF morbseq>1 AND ((nested>0) OR (episode_start_date < lagsep)
OR (lagmode IN (5,9) and (episode_start_date = lagsep)))
THEN transseq+1;
ELSE DO;
transseq=0;
stayseq=stayseq+1;
END;
DROP nest_start nest_end lagsep lagmode nest_mode;
RUN;
PROC FREQ DATA=testtrans_seq;
TABLE transseq;
RUN;
* Create SEPDATE_FIN, and populate it to the all admissions in a transfer set;
* Use upside-down data;
PROC SORT DATA=testtrans_seq;
BY DESCENDING ppn DESCENDING fileseq;
RUN;
DATA length;
SET testtrans_seq;
BY DESCENDING ppn DESCENDING fileseq;
RETAIN date_tmp flag;
IF first.ppn THEN DO;
date_tmp=.; flag=.;
END;
IF transseq NE 0 THEN DO;
IF date_tmp=. THEN DO;
flag=1;
date_tmp=episode_end_date;
END;
ELSE date_tmp=max(episode_end_date, date_tmp);
END;
ELSE IF transseq=0 AND flag=1 THEN DO;
sepdate_fin_tmp=max(episode_end_date, date_tmp); flag=.; date_tmp=.;
END;
ELSE IF transseq=0 THEN sepdate_fin_tmp=episode_end_date;
DROP flag date_tmp;
RUN;
* Turn data right-way up;
PROC SORT DATA=length;
BY ppn fileseq stayseq;
RUN;
* Populate sepdate_fin to all records within a stay;
DATA los;
SET length;
BY ppn stayseq;
RETAIN sepdate_fin;
IF first.stayseq THEN sepdate_fin=sepdate_fin_tmp;
FORMAT sepdate_fin ddmmyy10.;
* Construct LOS variables *;
losd = episode_end_date - episode_start_date;
IF losd = 0 THEN losd = 1;
totlos = sepdate_fin - episode_start_date;
IF totlos = 0 THEN totlos = 1;
DROP sepdate_fin_tmp;
RUN;
* Examine data;
PROC PRINT DATA=los NOOBS;
BY ppn;
RUN;