generated from opensafely/research-template
/
cr_create_analysis_dataset.do
604 lines (442 loc) · 18.5 KB
/
cr_create_analysis_dataset.do
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
********************************************************************************
*
* Do-file: cr_create_analysis_dataset.do
*
* Project: Post COVID-admission admissions
*
* Programmed by: Krishnan, Fizz
*
* Data used: Data in memory (from input.csv)
*
* Data created: cr_create_analysis_dataset.dta
*
* Other output: None
*
********************************************************************************
*
* Purpose: This do-file creates the variables required for the
* main analysis and saves into Stata datasets.
*
********************************************************************************
local cpf "`1'"
* Open a log file
cap log close
log using ./analysis/output/cr_create_analysis_dataset_`cpf', replace t
clear
noi di "importing csv"
if "`cpf'"=="COVID" import delimited ./output/input_covdischarged.csv
else if "`cpf'"=="PNEUM" import delimited ./output/input_pneum2019.csv
else if "`cpf'"=="FLU" import delimited ./output/input_flu2017_19.csv
if "`cpf'"=="FLU" local diedsource "1ocare"
else local diedsource "ons"
di "STARTING COUNT FROM IMPORT:"
cou
****************************
* Create required cohort *
****************************
/* DROP IF DIED ON/BEFORE DISCHARGE DATE
noi di "DIED ON/BEFORE DISCHARGE DATE :"
drop if date(died_date_ons, "YMD")<=date(discharged1_date, "YMD")
else drop if date(died_date_ons, "YMD")<=d(1/2/2020)
*/
/*
* Age: Exclude children
noi di "DROPPING AGE<18:"
drop if age<18
*/
* Age: Exclude those with implausible ages
assert age<.
noi di "DROPPING AGE<105:"
drop if age>105
* Sex: Exclude categories other than M and F
assert inlist(sex, "M", "F", "I", "U")
noi di "DROPPING GENDER NOT M/F:"
drop if inlist(sex, "I", "U")
/* IMD */
* Group into 5 groups
rename imd imd_o
egen imd = cut(imd_o), group(5) icodes
replace imd = imd + 1
replace imd = . if imd_o==-1
drop imd_o
* Reverse the order (so high is more deprived)
recode imd 5=1 4=2 3=3 2=4 1=5 .=.
label define imd 1 "1 least deprived" 2 "2" 3 "3" 4 "4" 5 "5 most deprived"
label values imd imd
noi di "DROPPING IF NO IMD"
drop if imd>=.
******************************
* Convert strings to dates *
******************************
capture confirm var died_date_1ocare
if _rc==0 local died_date_1ocare "died_date_1ocare"
* Process variables with exact dates (admissions, deaths)
foreach var of varlist admitted1_date ///
discharged1_date ///
admitted2_date ///
discharged2_date ///
admitted3_date ///
discharged3_date ///
admitted4_date ///
discharged4_date ///
admitted5_date ///
discharged5_date ///
died_date_ons ///
`died_date_1ocare' ///
patient_index_date ///
admitted_*_date {
confirm string variable `var'
rename `var' _tmp
gen `var' = date(_tmp, "YMD")
drop _tmp
format %d `var'
}
* Process variables with nearest month dates only
foreach var of varlist bmi_date_measured ///
creatinine_date ///
bp_sys_date_measured ///
hba1c_mmol_per_mol_date ///
hba1c_percentage_date ///
haem_cancer ///
lung_cancer ///
other_cancer ///
temporary_immunodeficiency ///
aplastic_anaemia {
confirm string variable `var'
replace `var' = `var' + "-15"
rename `var' `var'_dstr
replace `var'_dstr = " " if `var'_dstr == "-15"
gen `var'_date = date(`var'_dstr, "YMD")
order `var'_date, after(`var'_dstr)
drop `var'_dstr
format `var'_date %td
}
rename bmi_date_measured_date bmi_date_measured
rename bp_sys_date_measured_date bp_sys_date
rename creatinine_date_date creatinine_date
rename hba1c_percentage_date_date hba1c_percentage_date
rename hba1c_mmol_per_mol_date_date hba1c_mmol_per_mol_date
********* DEFAULT CENSORING IS MAX OUTCOME DATE MINUS 7 **********
summ died_date_ons
global deathcensor = r(max)-7
*******************************
* Recode implausible values *
*******************************
* BMI
* Only keep if within certain time period? using bmi_date_measured ?
* NB: Some BMI dates in future or after cohort entry
* Set implausible BMIs to missing:
replace bmi = . if !inrange(bmi, 15, 50)
**********************
* Recode variables *
**********************
* Sex
assert inlist(sex, "M", "F")
gen male = (sex=="M")
drop sex
* Smoking
label define smoke 1 "Never" 2 "Former" 3 "Current"
gen smoke = 1 if smoking_status=="N"
replace smoke = 2 if smoking_status=="E"
replace smoke = 3 if smoking_status=="S"
replace smoke = . if smoking_status=="M"
label values smoke smoke
drop smoking_status
* Ethnicity (5 category)
replace ethnicity = . if ethnicity==.
label define ethnicity 1 "White" ///
2 "Mixed" ///
3 "Asian or Asian British" ///
4 "Black" ///
5 "Other"
label values ethnicity ethnicity
* Ethnicity (16 category)
replace ethnicity_16 = . if ethnicity==.
label define ethnicity_16 ///
1 "British or Mixed British" ///
2 "Irish" ///
3 "Other White" ///
4 "White + Black Caribbean" ///
5 "White + Black African" ///
6 "White + Asian" ///
7 "Other mixed" ///
8 "Indian or British Indian" ///
9 "Pakistani or British Pakistani" ///
10 "Bangladeshi or British Bangladeshi" ///
11 "Other Asian" ///
12 "Caribbean" ///
13 "African" ///
14 "Other Black" ///
15 "Chinese" ///
16 "Other"
label values ethnicity_16 ethnicity_16
* STP
rename stp stp_old
bysort stp_old: gen stp = 1 if _n==1
replace stp = sum(stp)
drop stp_old
**************************
* Categorise variables *
**************************
/* Age variables */
* Create categorised age
recode age 18/39.9999=1 40/49.9999=2 50/59.9999=3 ///
60/69.9999=4 70/79.9999=5 80/max=6, gen(agegroup)
label define agegroup 1 "18-<40" ///
2 "40-<50" ///
3 "50-<60" ///
4 "60-<70" ///
5 "70-<80" ///
6 "80+"
label values agegroup agegroup
* Create restricted cubic splines fir age
mkspline age = age, cubic nknots(4)
/* Body Mass Index */
* BMI (NB: watch for missingness)
gen bmicat = .
recode bmicat . = 1 if bmi<18.5
recode bmicat . = 2 if bmi<25
recode bmicat . = 3 if bmi<30
recode bmicat . = 4 if bmi<35
recode bmicat . = 5 if bmi<40
recode bmicat . = 6 if bmi<.
replace bmicat = . if bmi>=.
label define bmicat 1 "Underweight (<18.5)" ///
2 "Normal (18.5-24.9)" ///
3 "Overweight (25-29.9)" ///
4 "Obese I (30-34.9)" ///
5 "Obese II (35-39.9)" ///
6 "Obese III (40+)"
label values bmicat bmicat
* Create more granular categorisation
recode bmicat 1/3 . = 1 4=2 5=3 6=4, gen(obese4cat)
label define obese4cat 1 "No record of obesity" ///
2 "Obese I (30-34.9)" ///
3 "Obese II (35-39.9)" ///
4 "Obese III (40+)"
label values obese4cat obese4cat
order obese4cat, after(bmicat)
/* Smoking */
* Create non-missing 3-category variable for current smoking
recode smoke .=1, gen(smoke_nomiss)
order smoke_nomiss, after(smoke)
label values smoke_nomiss smoke
/* Asthma */
* Asthma (coded: 0 No, 1 Yes no OCS, 2 Yes with OCS)
rename asthma asthmacat
recode asthmacat 0=1 1=2 2=3 .=1
label define asthmacat 1 "No" 2 "Yes, no OCS" 3 "Yes with OCS"
label values asthmacat asthmacat
gen asthma = (asthmacat==2|asthmacat==3)
/* Blood pressure */
* Categorise
gen bpcat = 1 if bp_sys < 120 & bp_dias < 80
replace bpcat = 2 if inrange(bp_sys, 120, 130) & bp_dias<80
replace bpcat = 3 if inrange(bp_sys, 130, 140) | inrange(bp_dias, 80, 90)
replace bpcat = 4 if (bp_sys>=140 & bp_sys<.) | (bp_dias>=90 & bp_dias<.)
replace bpcat = . if bp_sys>=. | bp_dias>=. | bp_sys==0 | bp_dias==0
label define bpcat 1 "Normal" 2 "Elevated" 3 "High, stage I" ///
4 "High, stage II"
label values bpcat bpcat
recode bpcat .=1, gen(bpcat_nomiss)
label values bpcat_nomiss bpcat
* Create non-missing indicator of known high blood pressure
gen bphigh = (bpcat==4)
order bpcat bphigh, after(bp_sys_date)
***************************
* Grouped comorbidities *
***************************
/* Spleen */
* Spleen problems (dysplenia/splenectomy/etc and sickle cell disease)
egen spleen = rowmax(dysplenia sickle_cell)
order spleen, after(sickle_cell)
/* Cancer */
label define cancer 1 "Never" 2 "Last year" 3 "2-5 years ago" 4 "5+ years"
gen fiveybefore = patient_index_date-5*365.25
gen oneybefore = patient_index_date-365.25
* Haematological malignancies
gen cancer_haem_cat = 4 if inrange(haem_cancer_date, d(1/1/1900), fiveybefore)
replace cancer_haem_cat = 3 if inrange(haem_cancer_date, fiveybefore, oneybefore)
replace cancer_haem_cat = 2 if inrange(haem_cancer_date, oneybefore, patient_index_date)
recode cancer_haem_cat . = 1
label values cancer_haem_cat cancer
* All other cancers
gen cancer_exhaem_cat = 4 if inrange(lung_cancer_date, d(1/1/1900), fiveybefore) | ///
inrange(other_cancer_date, d(1/1/1900), fiveybefore)
replace cancer_exhaem_cat = 3 if inrange(lung_cancer_date, fiveybefore, oneybefore) | ///
inrange(other_cancer_date, fiveybefore, oneybefore)
replace cancer_exhaem_cat = 2 if inrange(lung_cancer_date, oneybefore, patient_index_date) | ///
inrange(other_cancer_date, oneybefore, patient_index_date)
recode cancer_exhaem_cat . = 1
label values cancer_exhaem_cat cancer
* Put variables together
order cancer_exhaem_cat cancer_haem_cat, after(other_cancer_date)
/* Immunosuppression */
* Immunosuppressed:
* HIV, permanent immunodeficiency ever, OR
* temporary immunodeficiency or aplastic anaemia last year
gen temp1 = max(hiv, permanent_immunodeficiency)
gen temp2 = inrange(temporary_immunodeficiency_date, oneybefore, patient_index_date)
gen temp3 = inrange(aplastic_anaemia_date, oneybefore, patient_index_date)
egen other_immunosuppression = rowmax(temp1 temp2 temp3)
drop temp1 temp2 temp3
order other_immunosuppression, after(temporary_immunodeficiency)
/* Hypertension */
gen htdiag_or_highbp = bphigh
recode htdiag_or_highbp 0 = 1 if hypertension==1
************
* eGFR *
************
* Set implausible creatinine values to missing (Note: zero changed to missing)
replace creatinine = . if !inrange(creatinine, 20, 3000)
* Divide by 88.4 (to convert umol/l to mg/dl)
gen SCr_adj = creatinine/88.4
gen min=.
replace min = SCr_adj/0.7 if male==0
replace min = SCr_adj/0.9 if male==1
replace min = min^-0.329 if male==0
replace min = min^-0.411 if male==1
replace min = 1 if min<1
gen max=.
replace max=SCr_adj/0.7 if male==0
replace max=SCr_adj/0.9 if male==1
replace max=max^-1.209
replace max=1 if max>1
gen egfr=min*max*141
replace egfr=egfr*(0.993^age)
replace egfr=egfr*1.018 if male==0
label var egfr "egfr calculated using CKD-EPI formula with no eth"
* Categorise into ckd stages
egen egfr_cat = cut(egfr), at(0, 15, 30, 45, 60, 5000)
recode egfr_cat 0=5 15=4 30=3 45=2 60=0, generate(ckd)
* 0 = "No CKD" 2 "stage 3a" 3 "stage 3b" 4 "stage 4" 5 "stage 5"
label define ckd 0 "No CKD" 1 "CKD"
label values ckd ckd
label var ckd "CKD stage calc without eth"
* Convert into CKD group
*recode ckd 2/5=1, gen(chronic_kidney_disease)
*replace chronic_kidney_disease = 0 if creatinine==.
recode ckd 0=1 2/3=2 4/5=3, gen(reduced_kidney_function_cat)
replace reduced_kidney_function_cat = 1 if creatinine==.
label define reduced_kidney_function_catlab ///
1 "None" 2 "Stage 3a/3b egfr 30-60 " 3 "Stage 4/5 egfr<30"
label values reduced_kidney_function_cat reduced_kidney_function_catlab
*More detailed version incorporating stage 5 or dialysis as a separate category
recode ckd 0=1 2/3=2 4=3 5=4, gen(reduced_kidney_function_cat2)
replace reduced_kidney_function_cat2 = 1 if creatinine==.
replace reduced_kidney_function_cat2 = 4 if dialysis==1
label define reduced_kidney_function_cat2lab ///
1 "None" 2 "Stage 3a/3b egfr 30-60 " 3 "Stage 4 egfr 15-<30" 4 "Stage 5 egfr <15 or dialysis"
label values reduced_kidney_function_cat2 reduced_kidney_function_cat2lab
************
* Hba1c *
************
/* Diabetes severity */
* Set zero or negative to missing
replace hba1c_percentage = . if hba1c_percentage<=0
replace hba1c_mmol_per_mol = . if hba1c_mmol_per_mol<=0
local fifteenmbefore = `studystart'-15*(365.25/12)
* Only consider measurements in last 15 months
replace hba1c_percentage = . if hba1c_percentage_date < `fifteenmbefore'
replace hba1c_mmol_per_mol = . if hba1c_mmol_per_mol_date < `fifteenmbefore'
/* Express HbA1c as percentage */
* Express all values as perecentage
noi summ hba1c_percentage hba1c_mmol_per_mol
gen hba1c_pct = hba1c_percentage
replace hba1c_pct = (hba1c_mmol_per_mol/10.929)+2.15 if hba1c_mmol_per_mol<.
* Valid % range between 0-20
replace hba1c_pct = . if !inrange(hba1c_pct, 0, 20)
replace hba1c_pct = round(hba1c_pct, 0.1)
/* Categorise hba1c and diabetes */
* Group hba1c
gen hba1ccat = 0 if hba1c_pct < 6.5
replace hba1ccat = 1 if hba1c_pct >= 6.5 & hba1c_pct < 7.5
replace hba1ccat = 2 if hba1c_pct >= 7.5 & hba1c_pct < 8
replace hba1ccat = 3 if hba1c_pct >= 8 & hba1c_pct < 9
replace hba1ccat = 4 if hba1c_pct >= 9 & hba1c_pct !=.
label define hba1ccat 0 "<6.5%" 1">=6.5-7.4" 2">=7.5-7.9" 3">=8-8.9" 4">=9"
label values hba1ccat hba1ccat
tab hba1ccat
* Create diabetes, split by control/not
gen diabcat = 1 if diabetes==0
replace diabcat = 2 if diabetes==1 & inlist(hba1ccat, 0, 1)
replace diabcat = 3 if diabetes==1 & inlist(hba1ccat, 2, 3, 4)
replace diabcat = 4 if diabetes==1 & !inlist(hba1ccat, 0, 1, 2, 3, 4)
label define diabcat 1 "No diabetes" ///
2 "Controlled diabetes" ///
3 "Uncontrolled diabetes" ///
4 "Diabetes, no hba1c measure"
label values diabcat diabcat
* Delete unneeded variables
drop hba1c_percentage* hba1c_mmol_per_mol* bmi_date_measured creatinine_date bp_sys_date *cancer_date aplastic_anaemia_date temporary_immunodeficiency_date SCr_adj min max egfr egfr_cat ckd hba1c_pct hba1ccat asthma diabetes ethnicity_16_date reduced_kidney_function_cat bphigh dysplenia sickle_cell permanent_immunodeficiency hiv creatinine
order patient_id region stp imd age agegroup male ethnicity ethnicity_16 bmi bmicat obese4cat smoke_nomiss patient_index_date admitted1_date admitted1_reason discharged1_date admitted2_date admitted2_reason discharged2_date died_date_ons died_cause_ons died_ons_covid_flag
*FURTHER EXCLUSIONS AND DATE PROCESSING:
*drop if initial admission/discharge are on the same day
drop if admitted1_date==discharged1_date
*tie together admissions that are within 1 week of previous discharge
gen finaldischargedate = discharged1_date
replace finaldischargedate = discharged2_date if admitted2_date<=(discharged1_date+7)
replace finaldischargedate = discharged3_date if admitted2_date<=(discharged1_date+7) & admitted3_date<=(discharged2_date+7)
replace finaldischargedate = discharged4_date if admitted2_date<=(discharged1_date+7) & admitted3_date<=(discharged2_date+7) & admitted4_date<=(discharged3_date+7)
format %d finaldischargedate
*drop if there is a single day admission in the chain of admissions, as cannot then get f-up:
drop if finaldischargedate==discharged4_date & discharged4_date == admitted4_date
*drop if we get to the 5th readmission with all short gaps
drop if admitted2_date<=(discharged1_date+7) & admitted3_date<=(discharged2_date+7) & admitted4_date<=(discharged3_date+7) & admitted5_date<=(discharged4_date+7)
gen entrydate = finaldischargedate+8
format %d entrydate
*get total days in critical care, and binary flag
generate totaldayscriticalcare = admitted1_dayscritical
replace totaldayscriticalcare = totaldayscriticalcare + admitted2_dayscritical if discharged2_date<=finaldischargedate
replace totaldayscriticalcare = totaldayscriticalcare + admitted3_dayscritical if discharged3_date<=finaldischargedate
replace totaldayscriticalcare = totaldayscriticalcare + admitted4_dayscritical if discharged4_date<=finaldischargedate
gen byte anycriticalcare = totaldayscriticalcare>0 & totaldayscriticalcare<.
*drop if died date on/before discharge date
cou if died_date_`diedsource'==finaldischargedate
cou if died_date_`diedsource'<finaldischargedate
cou if died_date_`diedsource'>finaldischargedate & died_date_`diedsource'<entrydate
drop if died_date_`diedsource'<entrydate
cou
*drop if later than 60d before latest sus data
summ discharged1_date, f d
scalar censordate = r(max) - 60
drop if entrydate>=censordate
if ("`cpf'"=="PNEUM"|"`cpf'"=="FLU") drop if entrydate>d(1/11/2019)
*Only keep if COVID/FLU was the primary reason for hospitalisation
if ("`cpf'"=="COVID") keep if admitted1_reason=="U071"|admitted1_reason=="U072"
if ("`cpf'"=="FLU") keep if (admitted1_reason=="J090"|admitted1_reason=="J100"|admitted1_reason=="J101"|admitted1_reason=="J108"|admitted1_reason=="J110"|admitted1_reason=="J111"|admitted1_reason=="J118")
*get readmission date
gen readmission_date = admitted2_date if finaldischargedate==discharged1_date
gen readmission_reason = admitted2_reason if finaldischargedate==discharged1_date
replace readmission_date = admitted3_date if finaldischargedate==discharged2_date
replace readmission_reason = admitted3_reason if finaldischargedate==discharged2_date
replace readmission_date = admitted4_date if finaldischargedate==discharged3_date
replace readmission_reason = admitted4_reason if finaldischargedate==discharged3_date
replace readmission_date = admitted5_date if finaldischargedate==discharged4_date
replace readmission_reason = admitted5_reason if finaldischargedate==discharged4_date
format %d readmission_date
*get exit dates
summ readmission_date, f d
if "`cpf'"=="COVID" scalar censordate = r(max)-60
else if "`cpf'"=="PNEUM" scalar censordate = r(max)-60-365
else if "`cpf'"=="FLU" {
gen _overallcensor = r(max)-60-365
gen _latestfupdate = finaldischargedate + r(max)-60 - d(1/2/2020)
gen censordate = min(_overallcensor, _latestfupdate)
}
gen exitdate = readmission_date if readmission_date<=censordate
gen readmission = (exitdate<.)
replace exitdate = died_date_`diedsource' if died_date_`diedsource'<. & died_date_`diedsource'<=exitdate & died_date_`diedsource'<=censordate
format %d exitdate
if "`cpf'"=="FLU" assert died_date_1ocare<. if readmission ==0 & exitdate<.
else assert died_date_ons<. if readmission ==0 & exitdate<.
replace readmission = 3 if readmission ==0 & exitdate<.
replace exitdate = censordate if exitdate==.
replace readmission = 2 if readmission==1 & (readmission_reason!="U071"&readmission_reason!="U072")
replace exitdate = exitdate+0.5 if exitdate==entrydate
***************
* Save data *
***************
sort patient_id
save ./analysis/cr_create_analysis_dataset_`cpf', replace
log close