generated from opensafely/research-template
/
01_hhClassif_cr_analysis_dataset.do
1678 lines (1329 loc) · 56.9 KB
/
01_hhClassif_cr_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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*==============================================================================
DO FILE NAME: 00_hhClassif_cr_analysis_dataset
PROJECT: Classification of hh into risk groups
DATE: 12th August 2020
AUTHOR: Kevin Wing adapted from R Mathur H Forbes, A Wong, A Schultze, C Rentsch,K Baskharan, E Williamson
DESCRIPTION OF FILE: program 00, data management for project
reformat variables
categorise variables
label variables
apply exclusion criteria
DATASETS USED: data in memory (from output/inputWithHHDependencies.csv)
DATASETS CREATED: none
OTHER OUTPUT: logfiles, printed to folder analysis/$logdir
sysdir set PLUS "/Users/kw/Documents/GitHub/households-research/analysis/adofiles"
sysdir set PERSONAL "/Users/kw/Documents/GitHub/households-research/analysis/adofiles"
==============================================================================*/
sysdir set PLUS ./analysis/adofiles
sysdir set PERSONAL ./analysis/adofiles
pwd
*first argument main W2
local dataset `1'
if "`dataset'"=="MAIN" local fileextension
else local fileextension "_`1'"
local inputfile "input`fileextension'.csv"
*Start dates
if "`dataset'"=="MAIN" global indexdate = "1/2/2020"
else if "`dataset'"=="W2" global indexdate = "1/9/2020"
*Censor dates
if "`dataset'"=="MAIN" global study_end_censor = "31/08/2020"
else if "`dataset'"=="W2" global study_end_censor = "18/12/2020"
* Open a log file
cap log close
log using ./logs/01_hhClassif_cr_analysis_dataset`fileextension'.log, replace t
*import delimited ./output/input.csv, clear
import delimited ./output/`inputfile', clear
*merge with msoa data (copied from DGrint SGTF repo)
merge m:1 msoa using ./lookups/MSOA_lookup
drop if _merge==2
drop _merge
**********for debugging only************
/*
global indexdate = "1/2/2020"
global study_end_censor = "31/08/2020"
import delimited ./output/input.csv, clear
*/
**********for debugging only************
di "STARTING safecount FROM IMPORT:"
safecount
*Start dates - already created above
*gen index = "01/02/2020"
* Date of cohort entry, 1 Feb 2020
*gen indexdate = date("$index", "DMY")
*format indexdate %d
****UP TO HERE THU NIGHT - COMPARING WITH HARRIET FILE******
*******************************************************************************
/* CREATE VARIABLES===========================================================*/
/* DEMOGRAPHICS */
* 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
safetab ethnicity
*re-order ethnicity
gen eth5=1 if ethnicity==1
replace eth5=2 if ethnicity==3
replace eth5=3 if ethnicity==4
replace eth5=4 if ethnicity==2
replace eth5=5 if ethnicity==5
replace eth5=. if ethnicity==.
label define eth5Label 1 "White" ///
2 "South Asian" ///
3 "Black" ///
4 "Mixed" ///
5 "Other"
label values eth5 eth5Label
safetab eth5, m
* 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
safetab ethnicity_16,m
* Ethnicity (16 category grouped further)
* Generate a version of the full breakdown with mixed in one group
gen eth16 = ethnicity_16
recode eth16 4/7 = 99
recode eth16 11 = 16
recode eth16 14 = 16
recode eth16 8 = 4
recode eth16 9 = 5
recode eth16 10 = 6
recode eth16 12 = 7
recode eth16 13 = 8
recode eth16 15 = 9
recode eth16 99 = 10
recode eth16 16 = 11
label define eth16 ///
1 "British" ///
2 "Irish" ///
3 "Other White" ///
4 "Indian" ///
5 "Pakistani" ///
6 "Bangladeshi" ///
7 "Caribbean" ///
8 "African" ///
9 "Chinese" ///
10 "All mixed" ///
11 "All Other"
label values eth16 eth16
safetab eth16,m
* STP
rename stp stp_old
bysort stp_old: gen stp = 1 if _n==1
replace stp = sum(stp)
drop stp_old
* MSOA/UTLA
egen n_msoa = tag(msoa)
count if n_msoa
bysort msoa: gen count1 = _N
summ count1, d
egen n_utla = tag(utla)
count if n_utla
bysort utla: gen count2 = _N
summ count2, d
* Regroup UTLAs with small case numbers
gen utla_group = utla_name
tab utla_group
replace utla_group = "Redbridge, Barking and Dagenham" if utla_name == "Barking and Dagenham"
replace utla_group = "Redbridge, Barking and Dagenham" if utla_name == "Redbridge"
replace utla_group = "Bucks/Ox/West. Berks/Swindon" if utla_name == "Buckinghamshire"
replace utla_group = "Bucks/Ox/West. Berks/Swindon" if utla_name == "Oxfordshire"
replace utla_group = "Bucks/Ox/West. Berks/Swindon" if utla_name == "Swindon"
replace utla_group = "Bucks/Ox/West. Berks/Swindon" if utla_name == "West Berkshire"
replace utla_group = "Camden and Westminster" if utla_name == "Camden"
replace utla_group = "Camden and Westminster" if utla_name == "Westminster"
replace utla_group = "" if utla_name == "Isles of Scilly"
replace utla_group = "Richmond and Hounslow" if utla_name == "Richmond upon Thames"
replace utla_group = "Richmond and Hounslow" if utla_name == "Hounslow"
replace utla_group = "Rutland and Lincoln" if utla_name == "Rutland"
replace utla_group = "Rutland and Lincoln" if utla_name == "Lincolnshire"
replace utla_group = "Bolton and Tameside" if utla_name == "Bolton"
replace utla_group = "Bolton and Tameside" if utla_name == "Tameside"
tab utla_group, m
la var utla_group "Upper Tier Local Authority"
/* IMD */
* Group into 5 groups
rename imd imd_o
egen imd = cut(imd_o), group(5) icodes
* add one to create groups 1 - 5
replace imd = imd + 1
* - 1 is missing, should be excluded from population
replace imd = .u 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 .u = .u
label define imdLabel 1 "1 least deprived" 2 "2" 3 "3" 4 "4" 5 "5 most deprived" .u "Unknown"
label values imd imdLabel
**************************** HOUSEHOLD VARS*******************************************
*update with UPRN data
*sum hh_total hh_size
rename household_id hh_id
rename household_size hh_size
*gen categories of household size - KW will use actual household sizes in analysis but will leave this in so easy to find where it is used in Rohini analysis files.
gen hh_total_cat=.
replace hh_total_cat=1 if hh_size >=1 & hh_size<=2
replace hh_total_cat=2 if hh_size >=3 & hh_size<=5
replace hh_total_cat=3 if hh_size >=6 & hh_size<=10
replace hh_total_cat=4 if hh_size >=11
/* Rohini code - I think I want to drop them completely rather than just not include in a derived household variable
Note that U=private home, PC=care home, PN=nursing home, PS=care or nursing home, ""=unknown
*remove people from hh_cat if they live in a care home
replace hh_total_cat=. if care_home_type!="U"
*/
*keep only private homes
drop if care_home_type!="U"
label define hh_total_catLabel 1 "1-2" ///
2 "3-5" ///
3 "6-10" ///
4 "11+"
label values hh_total_cat hh_total_catLabel
safetab hh_total_cat,m
safetab hh_total_cat care_home_type,m
safetab hh_size hh_total_cat,m
*save a file here for looking at the distribution of household sizes AFTER carehomes have been dropped
save ./output/allHH_beforeDropping_largerThan10_`dataset'.dta, replace
*drop households we don't need i.e. 1 or smaller or larger than 10
*note originally dropped at 2, but after discussion with Daniel and Roz decided to keep size 1 hh in
drop if hh_size>10
safetab hh_size
******Create a household composition variable for the hh risk classification study (might be useful for snotty noses? - takes 5 minutes to run up to line 363)
/*test how to create a variable with the following categories (see protocol safetable 1)
1 SG1 - hh has only 18-29 year olds in it
2 SG2 - hh has only 30-66 year olds in it
3 SG3 - hh has only 67+ in it
4 2G1 - hh has 0-17 and 18-29 in it
5 2G2 - hh has 0-17 and 30-66 in it
6 2G3 - hh has 0-17 and 67+ in it
7 2G4 - hh has 18-29 and 67+ in it
8 2G5 - hh has 30-66 and 67+ in it
9 2G6 - hh has 18-29 and 67+ in it
10 MG1 - hh has 0-17, 18-29 and 30-66 in it
11 MG2 - hh has 0-17, 18-29 and 67+ in it
12 MG3 - hh has 0-17, 30-66 and 67+ in it
13 MG4 - hh has 18-29, 30-66 and 67+ in it
14 MG5 - hh has 0-17, 18-29, 30-66 and 67+ in it
*/
*first of all, create age bands that I need for this
egen ageCatHHRisk=cut(age), at (0, 18, 30, 67, 200)
recode ageCatHHRisk 0=0 18=1 30=2 67=3
label define ageCatHHRiskLabel 0 "0-17" 1 "18-29" 2 "30-66" 3 "67+"
label values ageCatHHRisk ageCatHHRiskLabel
safetab ageCatHHRisk, miss
la var ageCatHHRisk "Age categorised for HH risk analysis"
preserve
*keep only the variables I need to work this out
keep hh_id patient_id ageCatHHRisk
sort hh_id ageCatHHRisk
*mark whether hh has each age category using egen max which returns true (1) or false (0) (see https://www.stata.com/support/faqs/data-management/create-variable-recording/)
egen hasUnder18=max(ageCatHHRisk==0), by(hh_id)
egen has18_29=max(ageCatHHRisk==1), by(hh_id)
egen has30_66=max(ageCatHHRisk==2), by(hh_id)
egen has67Plus=max(ageCatHHRisk==3), by(hh_id)
*now generate the hhRiskCat variable for each person
generate hhRiskCat=.
la var hhRiskCat "Household risk category"
*Key:
/*test how to create a variable with the following categories:
1 SG1 - hh has only 18-29 year olds in it
2 SG2 - hh has only 30-66 year olds in it
3 SG3 - hh has only 67+ in it
4 2G1 - hh has 0-17 and 18-29 in it
5 2G2 - hh has 0-17 and 30-66 in it
6 2G3 - hh has 0-17 and 67+ in it
7 2G4 - hh has 18-29 and 67+ in it
8 2G5 - hh has 30-66 and 67+ in it
9 2G6 - hh has 18-29 and 67+ in it
10 MG1 - hh has 0-17, 18-29 and 30-66 in it
11 MG2 - hh has 0-17, 18-29 and 67+ in it
12 MG3 - hh has 0-17, 30-66 and 67+ in it
13 MG4 - hh has 18-29, 30-66 and 67+ in it
14 MG5 - hh has 0-17, 18-29, 30-66 and 67+ in it
*/
replace hhRiskCat=0 if hasUnder18==1 & has18_29==0 & has30_66==0 & has67Plus==0
replace hhRiskCat=1 if hasUnder18==0 & has18_29==1 & has30_66==0 & has67Plus==0
replace hhRiskCat=2 if hasUnder18==0 & has18_29==0 & has30_66==1 & has67Plus==0
replace hhRiskCat=3 if hasUnder18==0 & has18_29==0 & has30_66==0 & has67Plus==1
replace hhRiskCat=4 if hasUnder18==1 & has18_29==1 & has30_66==0 & has67Plus==0
replace hhRiskCat=5 if hasUnder18==1 & has18_29==0 & has30_66==1 & has67Plus==0
replace hhRiskCat=6 if hasUnder18==1 & has18_29==0 & has30_66==0 & has67Plus==1
replace hhRiskCat=7 if hasUnder18==0 & has18_29==1 & has30_66==1 & has67Plus==0
replace hhRiskCat=8 if hasUnder18==0 & has18_29==1 & has30_66==0 & has67Plus==1
replace hhRiskCat=9 if hasUnder18==0 & has18_29==0 & has30_66==1 & has67Plus==1
replace hhRiskCat=10 if hasUnder18==1 & has18_29==1 & has30_66==1 & has67Plus==0
replace hhRiskCat=11 if hasUnder18==1 & has18_29==1 & has30_66==0 & has67Plus==1
replace hhRiskCat=12 if hasUnder18==1 & has18_29==0 & has30_66==1 & has67Plus==1
replace hhRiskCat=13 if hasUnder18==0 & has18_29==1 & has30_66==1 & has67Plus==1
replace hhRiskCat=14 if hasUnder18==1 & has18_29==1 & has30_66==1 & has67Plus==1
*label variable
label define hhRiskCatLabel 0 "Only <18" 1 "Only 18-29" 2 "Only 30-66" 3 "Only 67+" 4 "0-17 & 18-29" 5 "0-17 & 30-66" 6 "0-17 & 67+" 7 "18-29 & 30-66" 8 "18-29 & 67+" 9 "30-66 & 67+" 10 "0-17, 18-29 & 30-66" 11 "0-17, 18-29 & 67+" 12 "0-17, 30-66 & 67+" 13 "18-29, 30-66 & 67+" 14 "0-17, 18-29, 30-66 & 67+"
label values hhRiskCat hhRiskCatLabel
la var hhRiskCat "Age group(s) of hh occupants"
safetab hhRiskCat, miss
keep hh_id hhRiskCat
duplicates drop hh_id, force
tempfile hhRiskCat
save `hhRiskCat', replace
restore
merge m:1 hh_id using `hhRiskCat'
drop _merge
safetab hhRiskCat, miss
*missing here are likely to be people living in households made up of only under 18 year olds
*now create other exposure variables related to this i.e. (a) the high level broad categories for descriptive analysis and (b) the age-stratified categories
*(a) high level broad categories from protocol
generate hhRiskCatBROAD=.
la var hhRiskCatBROAD "hhRiskCat in three categories (for descriptive work)"
replace hhRiskCatBROAD=1 if hhRiskCat>=1 & hhRiskCat<=3
replace hhRiskCatBROAD=2 if hhRiskCat>=4 & hhRiskCat<=9
replace hhRiskCatBROAD=3 if hhRiskCat>=10 & hhRiskCat<=14
*label variable
label define hhRiskCatBROADLabel 1 "1 gen" 2 "2 gens" 3 "3+ gens"
label values hhRiskCatBROAD hhRiskCatBROADLabel
safetab hhRiskCat hhRiskCatBROAD
*(b) variable for stratifying by the oldest age group (67+)
generate hhRiskCat67PLUS=.
la var hhRiskCat67PLUS "hhRiskCat for the over 67 year old age group"
replace hhRiskCat67PLUS=1 if hhRiskCat==3
replace hhRiskCat67PLUS=2 if hhRiskCat==6
replace hhRiskCat67PLUS=3 if hhRiskCat==8
replace hhRiskCat67PLUS=4 if hhRiskCat==9
replace hhRiskCat67PLUS=5 if hhRiskCat==11
replace hhRiskCat67PLUS=6 if hhRiskCat==12
replace hhRiskCat67PLUS=7 if hhRiskCat==13
replace hhRiskCat67PLUS=8 if hhRiskCat==14
*label variable
label define hhRiskCat67PLUS 1 "Only 67+" 2 "0-17 & 67+" 3 "18-29 & 67+" 4 "30-66 & 67+" 5 "0-17, 18-29 & 67+" 6 "0-17, 30-66 & 67+" 7 "18-29, 30-66 & 67+" 8 "0-17, 18-29, 30-66 & 67+"
label values hhRiskCat67PLUS hhRiskCat67PLUS
safetab hhRiskCat hhRiskCat67PLUS, miss
*(b) variable for stratifying by the 30-66 year olds
generate hhRiskCat33TO66=.
la var hhRiskCat33TO66 "hhRiskCat for the 30-66 year old age group"
replace hhRiskCat33TO66=1 if hhRiskCat==2
replace hhRiskCat33TO66=2 if hhRiskCat==5
replace hhRiskCat33TO66=3 if hhRiskCat==7
replace hhRiskCat33TO66=4 if hhRiskCat==9
replace hhRiskCat33TO66=5 if hhRiskCat==10
replace hhRiskCat33TO66=6 if hhRiskCat==12
replace hhRiskCat33TO66=7 if hhRiskCat==13
replace hhRiskCat33TO66=8 if hhRiskCat==14
*label variable
label define hhRiskCat33TO66 1 "Only 30-66" 2 "0-17 & 30-66" 3 "18-29 & 30-66" 4 "30-66 & 67+" 5 "0-17, 18-29 & 30-66" 6 "0-17, 30-66 & 67+" 7 "18-29, 30-66 & 67+" 8 "0-17, 18-29, 30-66 & 67+"
label values hhRiskCat33TO66 hhRiskCat33TO66
safetab hhRiskCat hhRiskCat33TO66, miss
*(c) variable for stratifying by the 18-29 year olds
generate hhRiskCat18TO29=.
la var hhRiskCat18TO29 "hhRiskCat for the 18-29 year old age group"
replace hhRiskCat18TO29=1 if hhRiskCat==1
replace hhRiskCat18TO29=2 if hhRiskCat==4
replace hhRiskCat18TO29=3 if hhRiskCat==7
replace hhRiskCat18TO29=4 if hhRiskCat==8
replace hhRiskCat18TO29=5 if hhRiskCat==10
replace hhRiskCat18TO29=6 if hhRiskCat==11
replace hhRiskCat18TO29=7 if hhRiskCat==13
replace hhRiskCat18TO29=8 if hhRiskCat==14
*label variable
label define hhRiskCat18TO29 1 "Only 18-29" 2 "0-17 & 18-29" 3 "18-29 & 30-66" 4 "18-29 & 67+" 5 "0-17, 18-29 & 30-66" 6 "0-17, 18-29 & 67+" 7 "18-29, 30-66 & 67+" 8 "0-17, 18-29, 30-66 & 67+"
label values hhRiskCat18TO29 hhRiskCat18TO29
safetab hhRiskCat hhRiskCat18TO29, miss
****************************
* Create required cohort *
****************************
* Age: Exclude those with implausible ages
cap assert age<.
noi di "DROPPING AGE<105:"
drop if age>105
safecount
* Sex: Exclude categories other than M and F
cap assert inlist(sex, "M", "F", "I", "U")
noi di "DROPPING GENDER NOT M/F:"
drop if inlist(sex, "I", "U")
safecount
gen male = 1 if sex == "M"
replace male = 0 if sex == "F"
label define male 0"Female" 1"Male"
label values male male
safetab male
* Create binary age (for age stratification)
*recode age min/65.999999999 = 0 ///
* 66/max = 1, gen(age66)
* Check there are no missing ages
*cap assert age < .
*cap assert agegroup < .
*cap assert age66 < .
* Create restricted cubic splines for age
*mkspline age = age, cubic nknots(4)
/* CONVERT STRINGS TO DATE====================================================*/
/* Comorb dates dates are given with month only, so adding day
15 to enable them to be processed as dates */
*cr date for diabetes based on adjudicated type
gen diabetes=type1_diabetes if diabetes_type=="T1DM"
replace diabetes=type2_diabetes if diabetes_type=="T2DM"
replace diabetes=unknown_diabetes if diabetes_type=="UNKNOWN_DM"
drop type1_diabetes type2_diabetes unknown_diabetes
foreach var of varlist chronic_respiratory_disease ///
chronic_cardiac_disease ///
cancer_haem ///
cancer_nonhaem ///
permanent_immunodeficiency ///
temporary_immunodeficiency ///
chronic_liver_disease ///
other_neuro ///
stroke_dementia ///
esrf ///
hypertension ///
asthma ///
ra_sle_psoriasis ///
diabetes ///
bmi_date_measured ///
bp_sys_date_measured ///
bp_dias_date_measured ///
creatinine_date ///
hba1c_mmol_per_mol_date ///
hba1c_percentage_date ///
smoking_status_date ///
{
capture confirm string variable `var'
if _rc!=0 {
cap assert `var'==.
rename `var' `var'_date
}
else {
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
}
* Note - outcome dates are handled separtely below
* Some names too long for loops below, shorten
rename permanent_immunodeficiency_date perm_immunodef_date
rename temporary_immunodeficiency_date temp_immunodef_date
rename bmi_date_measured_date bmi_measured_date
/* CREATE BINARY VARIABLES====================================================*/
* Make indicator variables for all conditions where relevant
foreach var of varlist chronic_respiratory_disease ///
chronic_cardiac_disease ///
cancer_haem ///
cancer_nonhaem ///
perm_immunodef ///
temp_immunodef ///
chronic_liver_disease ///
other_neuro ///
stroke_dementia ///
esrf ///
hypertension ///
ra_sle_psoriasis ///
bmi_measured_date ///
bp_sys_date_measured ///
bp_dias_date_measured ///
creatinine_date ///
hba1c_mmol_per_mol_date ///
hba1c_percentage_date ///
smoking_status_date ///
{
/* date ranges are applied in python, so presence of date indicates presence of
disease in the correct time frame */
local newvar = substr("`var'", 1, length("`var'") - 5)
gen `newvar' = (`var'!=. )
order `newvar', after(`var')
safetab `newvar'
}
/* Body Mass Index */
* NB: watch for missingness
* Recode strange values
replace bmi = . if bmi == 0
replace bmi = . if !inrange(bmi, 15, 50)
* Restrict to within 10 years of index and aged > 16
gen bmi_time = (date("$indexdate", "DMY") - bmi_measured_date)/365.25
gen bmi_age = age - bmi_time
replace bmi = . if bmi_age < 16
replace bmi = . if bmi_time > 10 & bmi_time != .
* Set to missing if no date, and vice versa
replace bmi = . if bmi_measured_date == .
replace bmi_measured_date = . if bmi == .
replace bmi_measured = . if bmi == .
* 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 = .u if bmi>=.
label define bmicatLabel 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+)" ///
.u "Unknown (.u)"
label values bmicat bmicatLabel
* Create more granular categorisation
recode bmicat 1/3 .u = 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)
**generate BMI categories for south asians
*https://www.nice.org.uk/guidance/ph46/chapter/1-Recommendations#recommendation-2-bmi-assessment-multi-component-interventions-and-best-practice-standards
gen bmicat_sa=bmicat
replace bmicat_sa = 2 if bmi>=18.5 & bmi <23 & ethnicity ==3
replace bmicat_sa = 3 if bmi>=23 & bmi < 27.5 & ethnicity ==3
replace bmicat_sa = 4 if bmi>=27.5 & bmi < 32.5 & ethnicity ==3
replace bmicat_sa = 5 if bmi>=32.5 & bmi < 37.5 & ethnicity ==3
replace bmicat_sa = 6 if bmi>=37.5 & bmi < . & ethnicity ==3
safetab bmicat_sa
label define bmicat_saLabel 1 "Underweight (<18.5)" ///
2 "Normal (18.5-24.9 / 22.9)" ///
3 "Overweight (25-29.9 / 23-27.4)" ///
4 "Obese I (30-34.9 / 27.4-32.4)" ///
5 "Obese II (35-39.9 / 32.5- 37.4)" ///
6 "Obese III (40+ / 37.5+)" ///
.u "Unknown (.u)"
label values bmicat_sa bmicat_saLabel
* Create more granular categorisation
recode bmicat_sa 1/3 .u = 1 4=2 5=3 6=4, gen(obese4cat_sa)
label define obese4cat_sa 1 "No record of obesity" ///
2 "Obese I (30-34.9 / 27.5-32.5)" ///
3 "Obese II (35-39.9 / 32.5- 37.4)" ///
4 "Obese III (40+ / 37.5+)"
label values obese4cat_sa obese4cat_sa
order obese4cat_sa, after(bmicat_sa)
/* Smoking */
* Smoking
label define smoke 1 "Never" 2 "Former" 3 "Current" .u "Unknown (.u)"
gen smoke = 1 if smoking_status == "N"
replace smoke = 2 if smoking_status == "E"
replace smoke = 3 if smoking_status == "S"
replace smoke = .u if smoking_status == "M"
replace smoke = .u if smoking_status == ""
label values smoke smoke
drop smoking_status
* Create non-missing 3-category variable for current smoking
* Assumes missing smoking is never smoking
recode smoke .u = 1, gen(smoke_nomiss)
order smoke_nomiss, after(smoke)
label values smoke_nomiss smoke
/* CLINICAL COMORBIDITIES */
/* Cancer */
label define cancer 1 "Never" 2 "Last year" 3 "2-5 years ago" 4 "5+ years"
* malignancies
gen cancer_cat = 4 if inrange(cancer_haem_date, d(1/1/1900), d(1/2/2015))|inrange(cancer_nonhaem_date, d(1/1/1900), d(1/2/2015))
replace cancer_cat = 3 if inrange(cancer_haem_date, d(1/2/2015), d(1/2/2019))|inrange(cancer_nonhaem_date, d(1/2/2015), d(1/2/2019))
replace cancer_cat = 2 if inrange(cancer_haem_date, d(1/2/2019), d(1/2/2020))|inrange(cancer_nonhaem_date, d(1/2/2019), d(1/2/2020))
recode cancer_cat . = 1
label values cancer_cat cancer
/* Immunosuppression */
* Immunosuppressed:
* Permanent immunodeficiency ever, OR
* Temporary immunodeficiency last year
gen temp1 = 1 if perm_immunodef_date!=.
gen temp2 = inrange(temp_immunodef_date, (date("$indexdate", "DMY") - 365), date("$indexdate", "DMY"))
egen other_immuno = rowmax(temp1 temp2)
drop temp1 temp2
order other_immuno, after(temp_immunodef)
/* 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 = .u 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" .u "Unknown"
label values bpcat bpcat
recode bpcat .u=1, gen(bpcat_nomiss)
label values bpcat_nomiss bpcat
* Create non-missing indicator of known high blood pressure
gen bphigh = (bpcat==4)
/* 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, 30, 60, 5000)
label define egfr_cat 5000 "None" 60 "Stage 3 egfr 30-6" 30 "Stage 4/5 egfr<30"
label values egfr_cat egfr_cat
lab var egfr_cat "CKD category"
safetab egfr_cat
gen egfr60=0
replace egfr60=1 if egfr<60
lab define egfr60 0"egfr >=60" 1"eGFR <60"
label values egfr60 egfr60
safetab egfr60
/* Hb1AC */
/* 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
/* 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 /195 mmol/mol
replace hba1c_pct = . if !inrange(hba1c_pct, 0, 20)
replace hba1c_pct = round(hba1c_pct, 0.1)
/* Categorise hba1c and diabetes */
/* Diabetes type */
gen dm_type=1 if diabetes_type=="T1DM"
replace dm_type=2 if diabetes_type=="T2DM"
replace dm_type=3 if diabetes_type=="UNKNOWN_DM"
replace dm_type=0 if diabetes_type=="NO_DM"
safetab dm_type diabetes_type
label define dm_type 0"No DM" 1"T1DM" 2"T2DM" 3"UNKNOWN_DM"
label values dm_type dm_type
*Open safely diabetes codes with exeter algorithm
gen dm_type_exeter_os=1 if diabetes_exeter_os=="T1DM_EX_OS"
replace dm_type_exeter_os=2 if diabetes_exeter_os=="T2DM_EX_OS"
replace dm_type_exeter_os=0 if diabetes_exeter_os=="NO_DM"
label values dm_type_exeter_os dm_type
* 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
safetab hba1ccat
gen hba1c75=0 if hba1c_pct<7.5
replace hba1c75=1 if hba1c_pct>=7.5 & hba1c_pct!=.
label define hba1c75 0"<7.5" 1">=7.5"
safetab hba1c75, m
* Create diabetes, split by control/not
gen diabcat = 1 if dm_type==0
replace diabcat = 2 if dm_type==1 & inlist(hba1ccat, 0, 1)
replace diabcat = 3 if dm_type==1 & inlist(hba1ccat, 2, 3, 4)
replace diabcat = 4 if dm_type==2 & inlist(hba1ccat, 0, 1)
replace diabcat = 5 if dm_type==2 & inlist(hba1ccat, 2, 3, 4)
replace diabcat = 6 if dm_type==1 & hba1c_pct==. | dm_type==2 & hba1c_pct==.
label define diabcat 1 "No diabetes" ///
2 "T1DM, controlled" ///
3 "T1DM, uncontrolled" ///
4 "T2DM, controlled" ///
5 "T2DM, uncontrolled" ///
6 "Diabetes, no HbA1c"
label values diabcat diabcat
safetab diabcat, m
/* Asthma */
* Asthma (coded: 0 No, 1 Yes no OCS, 2 Yes with OCS)
rename asthma asthmacat
recode asthmacat 0=1 1=2 2=3
label define asthmacat 1 "No" 2 "Yes, no OCS" 3 "Yes with OCS"
label values asthmacat asthmacat
gen asthma = (asthmacat==2|asthmacat==3)
safetab asthma
safetab asthmacat
/*
**care home
encode care_home_type, gen(carehometype)
drop care_home_type
gen carehome=0
replace carehome=1 if carehometype<4
safetab carehometype carehome
*/
/* OUTCOME (AND SURVIVAL TIME)==================================================*/
/*
Outcome summary:
*/
*Think we only need the outcome that is the 3 primary types of probable primary care codes
/*
*UP TO HERE WED EVENING - NEED TO UPDATE THE CASE SECTION SO IT REFLECTS THE CASE DEFINITIONS THAT I NEED IE:
1. COVID death
2. COVID hospitalisation
3. non-COVID death
4. (Fracture)
*/
/* CONVERT STRINGS TO DATE FOR OUTCOME VARIABLES =============================*/
* Recode to dates from the strings
order first_tested_for_covid first_positive_test_date died_date_ons died_date_cpns covid_tpp_probable covid_tpp_probableclindiag covid_tpp_probabletest covid_tpp_probableseq covid_admission_date positive_covid_test_ever
foreach var of varlist first_tested_for_covid - covid_admission_date {
confirm string variable `var'
rename `var' `var'_dstr
gen `var' = date(`var'_dstr, "YMD")
drop `var'_dstr
format `var' %td
}
*1. COVID death outcome
generate covidDeathCase=0
replace covidDeathCase=1 if died_ons_covid_flag_any==1|died_ons_covid_flag_underlying==1|died_date_cpns!=.
la var covidDeathCase "Case based on ONS or CPNS covid death record"
generate covidDeathCaseDate=.
replace covidDeathCaseDate=min(died_date_ons, died_date_cpns) if covidDeathCase==1
la var covidDeathCaseDate "Date of case based on ONS or CPNS death record"
format covidDeathCaseDate %td
tab covidDeathCase
*2. COVID hospitalisation outcome
generate covidHospCaseDate=.
replace covidHospCaseDate=covid_admission_date if covid_admission_date!=.
la var covidHospCaseDate "Date of case based COVID admission date"
format covidHospCaseDate %td
generate covidHospCase=0
replace covidHospCase=1 if covidHospCaseDate!=.
la var covidHospCase "Case based on hospitalisation with COVID"
*3. COVID hospitalisation or death outcome
generate covidHospOrDeathCase=0
replace covidHospOrDeathCase=1 if covidHospCase==1|covidDeathCase==1
generate covidHospOrDeathCaseDate=.
replace covidHospOrDeathCaseDate=min(covidHospCaseDate, covidDeathCaseDate) if covidHospOrDeathCase==1
la var covidHospOrDeathCaseDate "Date of case based on earliest of COVID hosp or COVID death date"
format covidHospOrDeathCaseDate %td
la var covidHospOrDeathCase "Case based on either hospitalisation with or death from COVID"
*4. Non-COVID death outcome
gen nonCOVIDDeathCaseDate = died_date_ons if died_ons_covid_flag_any != 1
la var nonCOVIDDeathCaseDate "Date of non-COVID death"
format nonCOVIDDeathCaseDate %td
generate nonCOVIDDeathCase=0
replace nonCOVIDDeathCase=1 if nonCOVIDDeathCaseDate!=.
la var nonCOVIDDeathCase "Died from non-COVID causes"
tab nonCOVIDDeathCase
*create a list of the outcomes for reuse
global outcomes covidDeathCase covidHospCase covidHospOrDeathCase nonCOVIDDeathCase
/*
*create a total number of cases in the household variable
bysort hh_id:egen totCasesInHH=total(case)
la var totCasesInHH "Total number of cases in a specific household"
*/
/*
* Date of Covid death in ONS
gen onscoviddeath_date = onsdeath_date if died_ons_covid_flag_any == 1
gen onsconfirmeddeath_date = onsdeath_date if died_ons_confirmedcovid_flag_any ==1
gen onssuspecteddeath_date = onsdeath_date if died_ons_suspectedcovid_flag_any ==1
*/
/*
* Date of non-COVID death in ONS
* If missing date of death resulting died_date will also be missing
gen ons_noncoviddeath_date = onsdeath_date if died_ons_covid_flag_any != 1
*/
*Date of first severe outcome
*replace severe_date = min(ae_date, icu_date, onscoviddeath_date)
*If outcome occurs on the first day of follow-up add one day
foreach i of global outcomes {
di "`i'"
count if `i'Date==date("$indexdate", "DMY")
replace `i'Date=`i'Date+1 if `i'Date==date("$indexdate", "DMY")
}
*date of deregistration
rename dereg_date dereg_dstr
gen dereg_date = date(dereg_dstr, "YMD")
drop dereg_dstr
format dereg_date %td
/*
* Binary indicators for outcomes - have these already
foreach i of global outcomes {
gen `i'=0
replace `i'=1 if `i'_date < .
safetab `i'
}
*/
*order patient_id age hh_id hh_size case case_date ethnicity
*update case variable so that those wwho died of confirmed covid are also considered cases
*drop severe
*gen severe=1 if ae==1 | icu==1 | onscoviddeath==1
*******************************
* Recode implausible values *
*******************************
* BMI
* Set implausible BMIs to missing:
replace bmi = . if !inrange(bmi, 15, 50)