forked from MIT-LCP/mimic-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcs_first_day.sql
More file actions
141 lines (126 loc) · 4.73 KB
/
gcs_first_day.sql
File metadata and controls
141 lines (126 loc) · 4.73 KB
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
-- ITEMIDs used:
-- CAREVUE
-- 723 as GCSVerbal
-- 454 as GCSMotor
-- 184 as GCSEyes
-- METAVISION
-- 223900 GCS - Verbal Response
-- 223901 GCS - Motor Response
-- 220739 GCS - Eye Opening
-- The code combines the ITEMIDs into the carevue itemids, then pivots those
-- So 223900 is changed to 723, then the ITEMID 723 is pivoted to form GCSVerbal
-- Note:
-- The GCS for sedated patients is defaulted to 15 in this code.
-- This is in line with how the data is meant to be collected.
-- e.g., from the SAPS II publication:
-- For sedated patients, the Glasgow Coma Score before sedation was used.
-- This was ascertained either from interviewing the physician who ordered the sedation,
-- or by reviewing the patient's medical record.
with base as
(
SELECT pvt.ICUSTAY_ID
, pvt.charttime
-- Easier names - note we coalesced Metavision and CareVue IDs below
, max(case when pvt.itemid = 454 then pvt.valuenum else null end) as GCSMotor
, max(case when pvt.itemid = 723 then pvt.valuenum else null end) as GCSVerbal
, max(case when pvt.itemid = 184 then pvt.valuenum else null end) as GCSEyes
-- If verbal was set to 0 in the below select, then this is an intubated patient
, case
when max(case when pvt.itemid = 723 then pvt.valuenum else null end) = 0
then 1
else 0
end as EndoTrachFlag
, ROW_NUMBER ()
OVER (PARTITION BY pvt.ICUSTAY_ID ORDER BY pvt.charttime ASC) as rn
FROM (
select l.ICUSTAY_ID
-- merge the ITEMIDs so that the pivot applies to both metavision/carevue data
, case
when l.ITEMID in (723,223900) then 723
when l.ITEMID in (454,223901) then 454
when l.ITEMID in (184,220739) then 184
else l.ITEMID end
as ITEMID
-- convert the data into a number, reserving a value of 0 for ET/Trach
, case
-- endotrach/vent is assigned a value of 0, later parsed specially
when l.ITEMID = 723 and l.VALUE = '1.0 ET/Trach' then 0 -- carevue
when l.ITEMID = 223900 and l.VALUE = 'No Response-ETT' then 0 -- metavision
else VALUENUM
end
as VALUENUM
, l.CHARTTIME
FROM `physionet-data.mimiciii_clinical.chartevents` l
-- get intime for charttime subselection
inner join `physionet-data.mimiciii_clinical.icustays` b
on l.icustay_id = b.icustay_id
-- Isolate the desired GCS variables
where l.ITEMID in
(
-- 198 -- GCS
-- GCS components, CareVue
184, 454, 723
-- GCS components, Metavision
, 223900, 223901, 220739
)
-- Only get data for the first 24 hours
and l.charttime between b.intime and DATETIME_ADD(b.intime, INTERVAL '1' DAY)
-- exclude rows marked as error
AND (l.error IS NULL OR l.error = 0)
) pvt
group by pvt.ICUSTAY_ID, pvt.charttime
)
, gcs as (
select b.*
, b2.GCSVerbal as GCSVerbalPrev
, b2.GCSMotor as GCSMotorPrev
, b2.GCSEyes as GCSEyesPrev
-- Calculate GCS, factoring in special case when they are intubated and prev vals
-- note that the coalesce are used to implement the following if:
-- if current value exists, use it
-- if previous value exists, use it
-- otherwise, default to normal
, case
-- replace GCS during sedation with 15
when b.GCSVerbal = 0
then 15
when b.GCSVerbal is null and b2.GCSVerbal = 0
then 15
-- if previously they were intub, but they aren't now, do not use previous GCS values
when b2.GCSVerbal = 0
then
coalesce(b.GCSMotor,6)
+ coalesce(b.GCSVerbal,5)
+ coalesce(b.GCSEyes,4)
-- otherwise, add up score normally, imputing previous value if none available at current time
else
coalesce(b.GCSMotor,coalesce(b2.GCSMotor,6))
+ coalesce(b.GCSVerbal,coalesce(b2.GCSVerbal,5))
+ coalesce(b.GCSEyes,coalesce(b2.GCSEyes,4))
end as GCS
from base b
-- join to itself within 6 hours to get previous value
left join base b2
on b.ICUSTAY_ID = b2.ICUSTAY_ID and b.rn = b2.rn+1 and b2.charttime > DATETIME_SUB(b.charttime, INTERVAL '6' HOUR)
)
, gcs_final as (
select gcs.*
-- This sorts the data by GCS, so rn=1 is the the lowest GCS values to keep
, ROW_NUMBER ()
OVER (PARTITION BY gcs.ICUSTAY_ID
ORDER BY gcs.GCS
) as IsMinGCS
from gcs
)
select ie.subject_id, ie.hadm_id, ie.icustay_id
-- The minimum GCS is determined by the above row partition, we only join if IsMinGCS=1
, GCS as mingcs
, coalesce(GCSMotor,GCSMotorPrev) as gcsmotor
, coalesce(GCSVerbal,GCSVerbalPrev) as gcsverbal
, coalesce(GCSEyes,GCSEyesPrev) as gcseyes
, EndoTrachFlag as endotrachflag
-- subselect down to the cohort of eligible patients
FROM `physionet-data.mimiciii_clinical.icustays` ie
left join gcs_final gs
on ie.icustay_id = gs.icustay_id and gs.IsMinGCS = 1
ORDER BY ie.icustay_id;