generated from opensafely/research-template
/
002_create_ld_analysis_dataset.do
619 lines (465 loc) · 18.2 KB
/
002_create_ld_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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
********************************************************************************
*
* Do-file: 002_create_ld_analysis_dataset.do
*
* Programmed by: Fizz & Krishnan & John
*
* Data used: analysis/
* data_base_cohort1.dta
* data_base_cohort2.dta
*
* Data created: analysis/
* data_ldanalysis_cohort1.dta
* data_ldanalysis_cohort2.dta
*
* Other output: Log file: logs/002_create_ld_analysis_dataset.log
*
********************************************************************************
*
* Purpose: This do-file creates the variables required for the
* learning disability analysis and creates the survival
* settings required for Stata to analyse.
*
********************************************************************************
clear all
set more off
* Open a log file
cap log close
log using "logs/002_create_ld_analysis_dataset", replace t
* Wave 1: i=1 (1 Mar 20 - 31 Aug 20)
* Wave 2: i=2 (1 Sept 20 - 8 Feb 21)
forvalues i = 1 (1) 2 {
* Open data
use "analysis/data_base_cohort`i'.dta", clear
* Index date
if `i'==1 {
local index_date = "2020-03-01"
}
else if `i'==2 {
local index_date = "2020-09-01"
}
* Display the input parameter (index date for cohort)
noi di "`index_date'"
local index = date(subinstr("`index_date'", "-", "/", .), "YMD")
noi di `index'
**************************
* Categorise variables *
**************************
/* Age variables */
* Create categorised age
recode age 0/15.9999=1 ///
16/44.9999=2 ///
45/64.9999=3 ///
65/69.9999=4 ///
70/74.9999=5 ///
75/79.9999=6 ///
80/max=7, ///
gen(agegroup)
label define agegroup 1 "0-<16" ///
2 "16-<45" ///
3 "45-<65" ///
4 "65-<70" ///
5 "70-<75" ///
6 "75-<80" ///
7 "80+"
label values agegroup agegroup
* Check there are no missing ages
assert agegroup<.
* Broader age strata
recode agegroup 1=0 2/3=1 4/5=2 6/7=3, gen(agebroad)
label define agebroad 0 "<16" ///
1 "16-<65" ///
2 "65-<75" ///
3 "75+"
label values agebroad agebroad
* Age splines
qui summ age
mkspline age = age, cubic nknots(4)
order age1 age2 age3, after(age)
* Child indicator
recode age min/15.999999=1 16/max=0, gen(child)
/* Body Mass Index */
* Only include child BMI measurements within 2 years
replace bmi = . if age<16 & (`index' - bmi_child_date_measured_date) > 365.25*2
drop bmi_child_date_measured_date
recode bmi min/39.99999=0 40/max=1, gen(obese40)
replace obese40 = 0 if bmi>=.
order obese40, after(bmi)
/* IMD */
* Group into 5 groups
assert imd_order!=-1
egen imd = cut(imd_order), group(5) icodes
replace imd = imd + 1
replace imd = .u if imd_order>=.
drop imd_order
* Reverse the order (so high is more deprived)
recode imd 5=1 4=2 3=3 2=4 1=5 .u=.u
label define imd 1 "1 least deprived" ///
2 "2" ///
3 "3" ///
4 "4" ///
5 "5 most deprived" ///
.u "Unknown"
label values imd imd
/* Severe asthma */
recode asthmacat 3=1 1 2=0, gen(asthma_severe)
order asthma_severe, after(asthmacat)
***************************
* Grouped comorbidities *
***************************
/* Spleen */
* Spleen problems (dysplenia/splenectomy/etc and sickle cell disease)
egen spleen_date = rowmin(dysplenia_date sickle_cell_date)
format spleen_date %td
order spleen_date spleen, after(sickle_cell)
drop dysplenia_date sickle_cell_date
/* Non-haematological malignancies */
gen exhaem_cancer_date = min(lung_cancer_date, other_cancer_date)
format exhaem_cancer_date %td
order exhaem_cancer_date, after(other_cancer_date)
drop lung_cancer_date other_cancer_date
rename haem_cancer_date cancerHaem_date
rename exhaem_cancer_date cancerExhaem_date
* Only consider non-haematological malignancies if in previous year
gen cancerExhaem1yr = inrange(cancerExhaem_date, `index'- 365.25, `index')
drop cancerExhaem_date
/* Haematological malignancies */
gen cancerHaem = 4 if ///
inrange(cancerHaem_date, d(1/1/1900), `index' - 5*365.25)
replace cancerHaem = 3 if ///
inrange(cancerHaem_date, `index' - 5*365.25, `index' - 365.25)
replace cancerHaem = 2 if ///
inrange(cancerHaem_date, `index' - 365.25, `index')
recode cancerHaem . = 1
* Label cancer variables
capture label drop cancer
label define cancer 1 "Never" ///
2 "Last year" ///
3 "2-5 years ago" ///
4 "5+ years"
label values cancerHaem cancer
/* Immunosuppression */
* Temporary immunodeficiency or aplastic anaemia last year, HIV/permanent
* condition ever
gen immunosuppression = ///
(inrange(temp_immuno_date, `index' - 365.25, `index') | ///
inrange(aplastic_anaemia_date, `index' - 365.25, `index') | ///
(perm_immuno_date < `index') | ///
(hiv_date < `index'))
drop temp_immuno_date aplastic_anaemia_date perm_immuno_date hiv_date
/* Dialysis */
* If transplant since dialysis, set dialysis to no
gen dialysis = (dialysis_date <.)
gen transplant_kidney = (transplant_kidney_date <.)
replace dialysis = 0 if dialysis == 1 ///
& transplant_kidney == 1 ///
& transplant_kidney_date > dialysis_date
order dialysis, after(transplant_kidney_date)
drop dialysis_date
/* Transplant */
egen transplant_date = rowmin(transplant_kidney_date ///
transplant_notkidney_date)
drop transplant_kidney_date transplant_notkidney_date
format transplant_date %td
**************************
* "Ever" comorbidities *
**************************
* Replace dates with binary indicators
foreach var of varlist respiratory_date ///
cf_date ///
cardiac_date ///
diabetes_date ///
af_date ///
dvt_pe_date ///
tia_date ///
stroke_date ///
dementia_date ///
neuro_date ///
liver_date ///
transplant_date ///
spleen_date ///
autoimmune_date ///
ibd_date ///
smi_date ///
ldr_date ///
ld_profound_date ///
ds_date ///
cp_date ///
{
local newvar = substr("`var'", 1, length("`var'") - 5)
gen `newvar' = (`var'< `index')
order `newvar', after(`var')
drop `var'
}
************
* eGFR *
************
label define kidneyfn 1 "None" ///
2 "Stage 3a/3b egfr 30-60" ///
3 "Stage 4/5 egfr<30"
* 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
* Kidney function
recode egfr_cat 0=1 2/3=2 4/5=3, gen(kidneyfn)
replace kidneyfn = 1 if egfr==.
label values kidneyfn kidneyfn
* Delete variables no longer needed
drop egfr_cat
* If either dialysis or kidney transplant then set kidney function to the
* lowest level
replace kidneyfn = 3 if dialysis == 1
replace kidneyfn = 3 if transplant_kidney == 1
drop transplant_kidney
****************************************
* Hba1c: Level of diabetic control *
****************************************
label define hba1ccat 0 "<6.5%" ///
1">=6.5-7.4" ///
2">=7.5-7.9" ///
3">=8-8.9" ///
4">=9"
/* 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 values hba1ccat 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 diabetes 1 "No diabetes" ///
2 "Controlled diabetes" ///
3 "Uncontrolled diabetes" ///
4 "Diabetes, no hba1c measure"
label values diabcat diabetes
drop hba1ccat
************************************
* Exposures: learning disability *
************************************
* People coded as having a profound learning disability
* but not on the LDR: add to LDR group (TPP-specific code mapping issue)
noi recode ldr 0=1 if ld_profound==1 & ldr==0
* Split LDR into moderate-mild and severe-profound
noi tab ldr ld_profound, m
gen ldr_cat = ldr
recode ldr_cat 1=2 if ld_profound==1
label define ldprofound 0 "Not on LDR" ///
1 "LDR, mild" ///
2 "LDR, profound"
label values ldr_cat ldprofound
* Split into in residential care and not
noi tab ldr resid_care_ld, m
gen ldr_carecat = ldr
recode ldr_carecat 1=2 if resid_care_ld==1
label define ldcare 0 "Not on LDR" ///
1 "LDR, not in residential care" ///
2 "LDR, in residential care"
label values ldr_carecat ldcare
* Combined variable
gen ldr_group = 0
replace ldr_group = 1 if ds==1 & ldr==0
replace ldr_group = 2 if ds==1 & ldr==1
replace ldr_group = 3 if cp==1 & ldr==0
replace ldr_group = 4 if cp==1 & ldr==1
replace ldr_group = 5 if cp==0 & ds==0 & ldr==1
label define ldr_group 0 "No DS, no CP, no LDR" ///
1 "DS but not LDR" ///
2 "DS and LDR" ///
3 "CP but not LDR" ///
4 "CP and LDR" ///
5 "LD with no DS or CP"
label values ldr_group ldr_group
***************************************
* Binary outcomes and survival time *
***************************************
* Summarise data
noi summ coviddeath_date covidadmission_date, format
*** WAVE 1 CENSORING *** 31st August 2020
global coviddeathcensor1 = d(31Aug2020)
global covidadmissioncensor1 = d(31Aug2020)
*** WAVE 2 CENSORING *** 8 Feb 21
global coviddeathcensor2 = d(8Feb2021)
global covidadmissioncensor2 = d(8Feb2021)
gen coviddeathcensor1 = $coviddeathcensor1
gen covidadmissioncensor1 = $covidadmissioncensor1
gen coviddeathcensor2 = $coviddeathcensor2
gen covidadmissioncensor2 = $covidadmissioncensor2
* Composite outcome date (either COVID-19 death or hospitalisation)
egen composite_date = rowmin(coviddeath_date covidadmission_date)
/* Binary outcome and survival time */
* Events prior to index date (shouldn't happen in real data)
noi count if otherdeath_date < `index'
noi count if coviddeath_date < `index'
noi count if covidadmission_date < `index'
forvalues k = 1 (1) 2 {
* COVID-19 death
gen coviddeath`k' = (coviddeath_date<.)
replace coviddeath`k' = 0 if coviddeath_date > coviddeathcensor`k'
replace coviddeath`k' = 0 if coviddeath_date > otherdeath_date
* COVID-19 hospitalisation
gen covidadmission`k' = (covidadmission_date<.)
replace covidadmission`k' = 0 if covidadmission_date > covidadmissioncensor`k'
replace covidadmission`k' = 0 if covidadmission_date > coviddeathcensor`k'
replace covidadmission`k' = 0 if covidadmission_date > coviddeath_date
replace covidadmission`k' = 0 if covidadmission_date > otherdeath_date
* Composite (either COVID-19 death or hospitalisation)
gen composite`k' = (composite_date<.)
replace composite`k' = 0 if composite_date > covidadmissioncensor`k'
replace composite`k' = 0 if composite_date > coviddeathcensor`k'
format composite_date %td
* Non-COVID-19 death
gen noncoviddeath`k' = (otherdeath_date<.)
replace noncoviddeath`k' = 0 if otherdeath_date > coviddeathcensor`k'
replace noncoviddeath`k' = 0 if otherdeath_date > coviddeath_date
/* Calculate survival times (days until event/censoring) */
egen stime_coviddeath`k' = rowmin(coviddeath_date ///
otherdeath_date ///
coviddeathcensor`k')
egen stime_covidadmission`k' = rowmin(covidadmission_date ///
coviddeath_date ///
otherdeath_date ///
covidadmissioncensor`k' ///
coviddeathcensor`k')
egen stime_composite`k' = rowmin(composite_date ///
otherdeath_date ///
covidadmissioncensor`k' ///
coviddeathcensor`k')
egen stime_noncoviddeath`k' = rowmin(otherdeath_date ///
coviddeath_date ///
coviddeathcensor`k')
drop coviddeathcensor`k' covidadmissioncensor`k'
}
* Convert to days since index date
foreach var of varlist stime* {
replace `var' = `var' - `index' + 1
}
* Wave 1: Keep both outcomes (censored at Aug 31, and all time)
* Wave 2: Keep only outcome censored at end
if `i'==2 {
drop coviddeath1 covidadmission1 composite1 noncoviddeath1 ///
stime_coviddeath1 stime_covidadmission1 stime_composite1 ///
stime_noncoviddeath1
}
*********************
* Label variables *
*********************
* Demographics
label var patient_id "Patient ID"
label var age "Age (years)"
label var age1 "Age spline term 1"
label var age2 "Age spline term 2"
label var age3 "Age spline term 3"
label var agegroup "Grouped age"
label var agebroad "Broad age strata"
label var child "Child indicator (<16 years)"
label var male "Male"
label var imd "Index of Multiple Deprivation (IMD)"
label var ethnicity_5 "Ethnicity in 16 categories"
label var stp "Sustainability and Transformation Partnership"
label var stpcode "Sustainability and Transformation Partnership"
label var region_7 "Geographical region (7 England regions)"
label var household_id "Household ID"
label var resid_care_old "Residential care, elderly"
label var resid_care_ldr "Residential care, learning disability"
* Learning disabilities
label var ldr "Learning disability"
label var ld_profound "Severe-profound learning disability"
label var ldr_cat "Learning disability split into mild-moderate and severe-profound"
label var ldr_carecat "Learning disability split into residential vs non-residential setting"
label var ds "Down's Syndrome"
label var cp "Cerebral Palsy"
label var ldr_group "Grouping of Down's, Cerebral Palsy and learning disability register"
* Confounders and comorbidities
label var bmi "Body Mass Index (BMI, kg/m2)"
label var obese40 "Evidence of BMI>40"
label var asthma_severe "Severe asthma"
label var respiratory "Respiratory disease (excl. asthma)"
label var cardiac "Heart disease"
label var cf "Cystic Fibrosis (& related)"
label var af "Atrial fibrillation"
label var dvt "Deep vein thrombosis/pulmonary embolism"
label var diabcat "Diabetes"
label var hba1c_pct "HbA1c (%)"
label var tia "Transient ischemic attack"
label var stroke "Stroke"
label var dementia "Dementia"
label var neuro "Neuro condition other than stroke/dementia"
label var cancerExhaem1yr "Non haematological cancer"
label var cancerHaem "Haematological cancer"
label var liver "Liver disease"
label var kidneyfn "Kidney function"
label var egfr "Estimated GFR"
label var transplant "Organ transplant recipient"
label var dialysis "Dialysis"
label var spleen "Spleen problems (dysplenia, sickle cell)"
label var autoimmune "RA, SLE, Psoriasis (autoimmune disease)"
label var immunosuppression "Conditions causing permanent or temporary immunosuppression"
label var ibd "IBD"
label var smi "Serious mental illness"
* Outcomes
label var coviddeath_date "Date of ONS COVID-19 death"
label var otherdeath_date "Date of ONS non-COVID-19 death"
label var covidadmission_date "Date of COVID-19 hospital admission"
label var composite_date "Date of first of COVID-19 hospital admission or death"
local tag1 = "censored 31 Aug 20"
local tag2 = "censored 8 Feb 21"
forvalues k = 1 (1) 2 {
capture label var coviddeath`k' "COVID-19 death (ONS), `tag`k''"
capture label var covidadmission`k' "COVID-19 hospital admission, `tag`k''"
capture label var composite`k' "COVID-19 hospital admission or death, `tag`k''"
capture label var noncoviddeath`k' "Non COVID-19 death (ONS), `tag`k''"
capture label var stime_coviddeath`k' "Days from study entry until COVID-19 death or censoring, `tag`k''"
capture label var stime_covidadmission`k' "Days from study entry until COVID-19 hospital admission or censoring, `tag`k''"
capture label var stime_composite`k' "Days from study entry until COVID-19 hospital admission or death or censoring, `tag`k''"
capture label var stime_coviddeath`k' "Days from study entry until Non-COVID-19 death or censoring, `tag`k''"
}
*********************
* Order variables *
*********************
sort patient_id
order patient_id stp* region_7 imd ///
household* resid_care_old resid_care_ldr ///
ldr ldr_cat ld_profound ldr_carecat ds cp ldr_group ///
age age age1 age2 age3 agegroup agebroad child male ///
bmi* obese* ethnicity* ///
respiratory* asthma_severe* cf* cardiac* diabcat* hba1c ///
af* dvt_pe* ///
stroke* dementia* tia* ///
cancerExhaem* cancerHaem* ///
kidneyfn* egfr liver* transplant* ///
spleen* autoimmune* immunosuppression* ibd* ///
smi* dialysis neuro ///
coviddeath* otherdeath* covidadmission* composite* noncovid*
keep patient_id stp* region_7 imd ///
household* resid_care_old resid_care_ldr ///
ldr ldr_cat ld_profound ldr_carecat ds cp ldr_group ///
age age age1 age2 age3 agegroup agebroad child male ///
bmi* obese* ethnicity* ///
respiratory* asthma_severe cf cardiac diabcat hba1c ///
af dvt_pe stroke dementia tia ///
cancerExhaem* cancerHaem ///
kidneyfn egfr liver transplant ///
spleen autoimmune immunosuppression ibd ///
smi dialysis neuro ///
coviddeath* otherdeath* covidadmission* composite* ///
noncovid* ///
stime*
***************
* Save data *
***************
sort patient_id
if `i'==1 {
label data "Analysis dataset, wave 1 (1 Mar - 31 Aug 20), for learning disability work"
}
else if `i'==2 {
label data "Analysis dataset, wave 2 (1 Sept 20 - 8 Feb 21), for learning disability work"
}
* Save overall dataset
save "analysis/data_ldanalysis_cohort`i'.dta", replace
}
log close