-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path05_DE_ID_script.sql
298 lines (276 loc) · 11.1 KB
/
05_DE_ID_script.sql
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Filename:
05_DE_ID_script.sql
Purpose:
This script creates a copy of the Cohort and removes identifying characteristics
to prepare the data for sharing with the VIRUS registry.
Description:
Run this file to generate a deidentified copy of your target data. Insert your data
into the OMOP tables, and de-identify person_id, and date fields using date.shift.
If a person is 90 years of age or older, assign a random age between 90-99 years.
Dependencies:
01_CURE_ID_Cohort.sql
02_CURE_ID_All_Tables.sql
03_CURE_ID_replace_rare_conditions_with_parents.sql
04_DE_ID_CDM_Table_ddl.sql
*/
/******* VARIABLES *******/
--SOURCE_SCHEMA: Results
--TARGET_SCHEMA: [Results]
DECLARE @START_DATE DATE = CAST('2016-01-01' AS DATE)
DECLARE @END_DATE DATE = CAST('2029-12-31' AS DATE)
/******* GENERATE MAP TABLES *******/
--Tables are dropped and created in a separate script.
--Please run that script first.
USE YOUR_DATABASE;
/******* GENERATE MAP TABLES *******/
INSERT INTO [Results].[source_id_person]
SELECT
p.person_id AS sourceKey,
ROW_NUMBER() OVER (ORDER BY p.gender_concept_id DESC, p.person_id DESC) AS id,
(FLOOR(RAND(CAST(NEWID() AS VARBINARY)) * 367)) - 183 AS date_shift,
CAST((DATEPART(YEAR, GETDATE()) - 90 - (FLOOR(RAND(CAST(NEWID() AS VARBINARY)) * 10))) AS INT) AS over_89_birth_year --If a person is > 89, then assign them a random age between 90 - 99
FROM [Results].[CURE_ID_Person] AS p;
INSERT INTO [Results].[source_id_visit]
SELECT
p.visit_occurrence_id AS sourceKey,
ROW_NUMBER() OVER (ORDER BY p.visit_occurrence_id) AS new_id
FROM [Results].[CURE_ID_Visit_Occurrence] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id --Ask Ben about this self join?
WHERE v.new_id IS NULL AND (
DATEADD(DAY, s.date_shift, p.visit_start_date) >= @START_DATE
AND DATEADD(DAY, s.date_shift, p.visit_end_date) <= @END_DATE
) ORDER BY p.person_id, p.visit_start_date;
/******* PERSON *******/
INSERT INTO [Results].[deident_CURE_ID_Person]
SELECT
s.id AS person_id,
p.gender_concept_id,
CASE
WHEN DATEDIFF(DAY, p.birth_datetime, GETDATE()) / 365.25 > 89 THEN s.over_89_birth_year
ELSE DATEPART(YEAR, DATEADD(DAY, s.date_shift, p.birth_datetime))
END AS year_of_birth,
DATEPART(MONTH, DATEADD(DAY, s.date_shift, p.birth_datetime)) AS month_of_birth,
1 AS day_of_birth,
DATEFROMPARTS(
CASE WHEN DATEDIFF(DAY, p.birth_datetime, GETDATE()) / 365.25 > 89 THEN s.over_89_birth_year ELSE DATEPART(YEAR, DATEADD(DAY, s.date_shift, p.birth_datetime)) END,
DATEPART(MONTH, DATEADD(DAY, s.date_shift, p.birth_datetime)),
1
) AS birth_datetime,
p.race_concept_id,
p.ethnicity_concept_id,
1 AS location_id,
1 AS provider_id,
1 AS care_site_id,
0 AS person_source_value,
0 AS gender_source_value,
0 AS gender_source_concept_id,
0 AS race_source_value,
0 AS race_source_concept_id,
0 AS ethnicity_source_value,
0 AS ethnicity_source_concept_id
FROM [Results].[CURE_ID_Person] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id;
/******* VISIT *******/
INSERT INTO [Results].[deident_CURE_ID_Visit_Occurrence]
SELECT
v.new_id AS visit_occurrence_id,
s.id AS person_id,
p.visit_concept_id,
DATEADD(DAY, s.date_shift, p.visit_start_date) AS visit_start_date,
DATEADD(DAY, s.date_shift, p.visit_start_datetime) AS visit_start_datetime,
DATEADD(DAY, s.date_shift, p.visit_end_date) AS visit_end_date,
DATEADD(DAY, s.date_shift, p.visit_end_datetime) AS visit_end_datetime,
p.visit_type_concept_id,
1 AS provider_id,
1 AS care_site_id,
NULL AS visit_source_value,
p.visit_source_concept_id,
p.admitted_from_concept_id,
NULL AS admitted_from_source_value,
p.discharged_to_concept_id,
NULL AS discharged_to_source_value,
p.preceding_visit_occurrence_id
FROM [Results].[CURE_ID_Visit_Occurrence] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id
WHERE (DATEADD(DAY, s.date_shift, visit_start_date) >= @START_DATE AND DATEADD(DAY, s.date_shift, visit_end_date) <= @END_DATE);
/******* CONDITION OCCURENCE *******/
INSERT INTO [Results].[deident_CURE_ID_Condition_Occurrence]
SELECT
p.condition_occurrence_id,
s.id AS person_id,
p.condition_concept_id,
DATEADD(DAY, s.date_shift, p.condition_start_date) AS condition_start_date,
DATEADD(DAY, s.date_shift, p.condition_start_datetime) AS condition_start_datetime,
DATEADD(DAY, s.date_shift, p.condition_end_date) AS condition_end_date,
DATEADD(DAY, s.date_shift, p.condition_end_datetime) AS condition_end_datetime,
p.condition_type_concept_id,
p.stop_reason,
1 AS provider_id,
v.new_id AS visit_occurrence_id,
p.visit_detail_id,
p.condition_source_value,
p.condition_source_concept_id,
p.condition_status_source_value,
p.condition_status_concept_id
FROM [Results].[CURE_ID_Condition_Occurrence_Rare_Removed] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id
WHERE (
DATEADD(DAY, s.date_shift, condition_start_date) < @END_DATE
AND DATEADD(DAY, s.date_shift, COALESCE(condition_end_date, condition_start_date)) > @START_DATE
);
/******* PROCEDURE OCCURENCE *******/
INSERT INTO [Results].[deident_CURE_ID_Procedure_Occurrence]
SELECT
p.procedure_occurrence_id,
s.id AS person_id,
p.procedure_concept_id,
DATEADD(DAY, s.date_shift, p.procedure_date) AS procedure_date,
DATEADD(DAY, s.date_shift, p.procedure_date) AS procedure_datetime,
p.procedure_type_concept_id,
p.modifier_concept_id,
p.quantity,
1 AS provider_id,
v.new_id AS visit_occurrence_id,
p.visit_detail_id,
p.procedure_source_value,
p.procedure_source_concept_id,
p.modifier_source_value
FROM [Results].[CURE_ID_Procedure_Occurrence] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id
WHERE (
DATEADD(DAY, s.date_shift, procedure_date) < @END_DATE
AND DATEADD(DAY, s.date_shift, procedure_date) > @START_DATE
);
/******* DRUG EXPOSURE *******/
INSERT INTO [Results].[deident_CURE_ID_Drug_Exposure]
SELECT
p.drug_exposure_id,
s.id AS person_id,
p.drug_concept_id,
DATEADD(DAY, s.date_shift, p.drug_exposure_start_date) AS drug_exposure_start_date,
DATEADD(DAY, s.date_shift, p.drug_exposure_start_date) AS drug_exposure_start_datetime,
DATEADD(DAY, s.date_shift, p.drug_exposure_end_date) AS drug_exposure_end_date,
DATEADD(DAY, s.date_shift, p.drug_exposure_end_date) AS drug_exposure_end_datetime,
DATEADD(DAY, s.date_shift, p.verbatim_end_date) AS verbatim_end_date,
p.drug_type_concept_id,
p.stop_reason,
p.refills,
p.quantity,
p.days_supply,
p.sig,
p.route_concept_id,
p.lot_number,
1 AS provider_id,
v.new_id AS visit_occurrence_id,
p.visit_detail_id,
p.drug_source_value,
p.drug_source_concept_id,
p.route_source_value,
p.dose_unit_source_value
FROM [Results].[CURE_ID_Drug_Exposure] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id
WHERE (
DATEADD(DAY, s.date_shift, drug_exposure_start_date) < @END_DATE
AND DATEADD(DAY, s.date_shift, drug_exposure_end_date) > @START_DATE
);
/******* OBSERVATION *******/
INSERT INTO [Results].[deident_CURE_ID_Observation]
SELECT
p.observation_id,
s.id AS person_id,
p.observation_concept_id,
DATEADD(DAY, s.date_shift, p.observation_date) AS observation_date,
DATEADD(DAY, s.date_shift, p.observation_date) AS observation_datetime,
p.observation_type_concept_id,
p.value_as_number,
p.value_as_string,
p.value_as_concept_id,
p.qualifier_concept_id,
p.unit_concept_id,
1 AS provider_id,
v.new_id AS visit_occurrence_id,
p.visit_detail_id,
p.observation_source_value,
p.observation_source_concept_id,
p.unit_source_value,
p.qualifier_source_value
FROM [Results].[CURE_ID_Observation] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id
WHERE (
DATEADD(DAY, s.date_shift, observation_date) < @END_DATE
AND DATEADD(DAY, s.date_shift, observation_date) > @START_DATE
);
/******* DEATH *******/
INSERT INTO [Results].[deident_CURE_ID_Death]
SELECT
s.id AS person_id,
DATEADD(DAY, s.date_shift, p.death_date) AS death_date,
DATEADD(DAY, s.date_shift, p.death_date) AS death_datetime,
p.death_type_concept_id,
p.cause_concept_id,
p.cause_source_value,
p.cause_source_concept_id
FROM [Results].[CURE_ID_Death] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id;
/******* DEVICE EXPOSURE *******/
INSERT INTO [Results].[deident_CURE_ID_Device_Exposure]
SELECT
p.device_exposure_id,
s.id AS person_id,
p.device_concept_id,
DATEADD(DAY, s.date_shift, p.device_exposure_start_date) AS device_exposure_start_date,
DATEADD(DAY, s.date_shift, p.device_exposure_start_date) AS device_exposure_start_datetime,
DATEADD(DAY, s.date_shift, p.device_exposure_end_date) AS device_exposure_end_date,
DATEADD(DAY, s.date_shift, p.device_exposure_end_date) AS device_exposure_end_datetime,
p.device_type_concept_id,
p.unique_device_id,
p.quantity,
1 AS provider_id,
v.new_id AS visit_occurrence_id,
p.visit_detail_id,
p.device_source_value,
p.device_source_concept_id
FROM [Results].[CURE_ID_Device_Exposure] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id
WHERE (
DATEADD(DAY, s.date_shift, device_exposure_start_date) < @END_DATE
AND DATEADD(DAY, s.date_shift, COALESCE(device_exposure_end_date, device_exposure_start_date)) > @START_DATE
);
/******* MEASUREMENT *******/
INSERT INTO [Results].[deident_CURE_ID_Measurement]
SELECT
p.measurement_id,
s.id AS person_id,
p.measurement_concept_id,
DATEADD(DAY, s.date_shift, p.measurement_date) AS measurement_date,
DATEADD(DAY, s.date_shift, p.measurement_date) AS measurement_datetime,
p.measurement_time,
p.measurement_type_concept_id,
p.operator_concept_id,
p.value_as_number,
p.value_as_concept_id,
p.unit_concept_id,
p.range_low,
p.range_high,
1 AS provider_id,
v.new_id AS visit_occurrence_id,
p.visit_detail_id,
p.measurement_source_value,
p.measurement_source_concept_id,
p.unit_source_value,
p.value_source_value
FROM [Results].[CURE_ID_Measurement] AS p
INNER JOIN [Results].[source_id_person] AS s ON s.sourceKey = p.person_id
LEFT JOIN [Results].[source_id_visit] AS v ON v.sourceKey = p.visit_occurrence_id
WHERE (
DATEADD(DAY, s.date_shift, measurement_date) < @END_DATE
AND DATEADD(DAY, s.date_shift, measurement_date) > @START_DATE
);