generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
103_time_varying_prep.do
1738 lines (1618 loc) · 76.6 KB
/
103_time_varying_prep.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: 103_time_varying_prep.do
DATE: 26/06/2023
AUTHOR: R Costello
DESCRIPTION OF FILE: Create time-varying covariates - this script updates time-
varying variables every 6 months or when exposure changes.
==============================================================================*/
adopath + ./analysis/ado
* Open a log file
cap log using ./logs/time_varying.log, replace
tempfile tempfile
import delimited using ./output/input_pbc.csv
describe
save `tempfile'
/* Time-varying covariates are assessed 6-monthly and at time of exposure switching.
As disease severity, covid vaccination and liver transplant will only switch once need to identify
nearest date after date identified in study definition */
* 120 day file: primary exposure definition
use ./output/time_varying_udca_120, clear
codebook patient_id
merge m:1 patient_id using `tempfile', keepusing(severe_disease_fu_date severe_disease_bl covid_vacc_first_date liver_transplant_fu_date dereg_date died_date_ons)
* Should only be required for dummy data
keep if _merge==3
* Only using start date as this is when udca exposure or 6 monthly check occurs
drop stop
* Add in rows with each of the 6 monthly assessment dates
gen expand=1
bys patient_id (start): replace expand=6 if _n==_N
expand expand, generate(newv)
bys patient_id newv: gen number = _n
replace start = date("01/09/2020", "DMY") if newv==1 & number==1
replace start = date("01/03/2021", "DMY") if newv==1 & number==2
replace start = date("01/09/2021", "DMY") if newv==1 & number==3
replace start = date("01/03/2022", "DMY") if newv==1 & number==4
replace start = date("01/09/2022", "DMY") if newv==1 & number==5
* This gives a file where start dates are either exposure switching or 6-montly assessment dates
* These will be used to determine the closest date to time-varying covariate dates identified in study definition
sort patient_id start
drop newv
* Determine end of follow-up date
gen dereg_dateA = date(dereg_date, "YMD")
format %dD/N/CY dereg_dateA
drop dereg_date
gen died_dateA = date(died_date_ons, "YMD")
format %dD/N/CY died_dateA
drop died_date_ons
gen end_study = date("31/12/2022", "DMY")
egen end_date = rowmin(dereg_dateA end_study died_dateA)
* Take out assessments after end_date
drop if start>end_date
* Determine number of days between each covariate and dates of UDCA change or 6 monthly check date
bys patient_id (start): egen last_assess = max(start)
* Format dates and identify date nearest after covariate updates
foreach var in covid_vacc_first_date severe_disease_fu_date liver_transplant_fu_date {
gen `var'A = date(`var', "YMD")
format `var'A %dD/N/CY
drop `var'
* time between date of record and change in covariate - positive values are those where record starts after
* covariate change date
gen time_`var' = start - `var'A
* Set time variable for records prior to change as missing
replace time_`var'=. if time_`var'<0
* Check if any are on index date (1st March) potentially severe disease - not others
di "Number where time-varying update same as assessment date"
count if time_`var'==0
di "Number where time-varying update is 1st March 2020"
count if `var'A==date("01/03/2020", "DMY")
* Find earliest start date after covariate update
* Note: there are some changes that occur after the last available 6 month assessment or exposure change
bys patient_id (time_`var'): gen `var'_updateN = start[1] if `var'A!=. & time_`var'!=.
* Spread to all rows for patient
bys patient_id: egen `var'_update = max(`var'_updateN)
format `var'_update %dD/N/CY
di "Number where new variable is prior to original variable (should be 0)"
count if `var'_update < `var'A
di "Number where time varying update is after last assessment date"
count if `var'A > last_assess & `var'A!=. & last_assess==start
di "Number of time-varying changes originally"
* Using last_assess==start to count patients rather than rows
count if `var'A!=. & last_assess==start
di "Number of dates for time-varying update"
count if `var'_update!=. & last_assess==start & `var'A!=.
}
* For severe disease only update variable if no severe disease at baseline
count if severe_disease_fu_date_update==date("01/03/2020", "DMY") & severe_disease_bl!=1
tab severe_disease_bl
replace severe_disease_fu_date_update = . if severe_disease_bl==1
* Keep only change dates to create time-varying dataset
keep patient_id covid_vacc_first_date_update severe_disease_fu_date_update liver_transplant_fu_date_update severe_disease_bl end_date
* Chercking number of patient_id's
codebook patient_id
duplicates drop
codebook patient_id
count
* Create file for each covariate to merge onto udca exposure file
* First covid vaccination and liver transplant as all people will begin at zero
rename liver_transplant_fu_date_update liver_trans_date_update
foreach var in covid_vacc_first liver_trans {
preserve
keep patient_id `var'_date_update end_date
* Drop updates after the end of follow-up
count if `var'_date>=end_date
replace `var'_date = . if `var'_date>=end_date
* Flag if variable is updated
gen `var'=(`var'_date_update!=.)
* Create extra row if variable is updated
gen expand = `var'+1
tab expand
expand expand, gen(newv)
* Update variables so row for when zero and row for when updated - if not updated whole time will be zero
gen start = date("01/03/2020", "DMY") if newv==0
replace start = `var'_date_update if newv==1
gen stop = `var'_date_update if newv==0
replace stop = end_date if stop==.
replace `var'=0 if newv==0
save ./output/tv_`var'_check, replace
keep patient_id `var' start stop
* Check data
count if start==stop
* drop where start is same as stop - should not drop any in real data
drop if start==stop
count if stop<start
codebook patient_id
save ./output/tv_`var', replace
restore
}
* Same for liver disease severity, but variable can start at 1 and remain for whole of follow-up
preserve
keep patient_id severe_disease_fu_date_update end_date severe_disease_bl
* Drop updates after the end of follow-up
replace severe_disease_fu_date_update=. if severe_disease_fu_date_update>=end_date
* Flag if variable is updated
gen severe_disease=(severe_disease_fu_date_update!=.)
tab severe_disease
replace severe_disease = 0 if severe_disease_bl==1
* Create extra row if variable is updated
gen expand = severe_disease + 1
tab expand
expand expand, gen(newv)
tab newv
* Update variables so row for when zero and row for when updated - if not updated whole time will be zero
gen start = date("01/03/2020", "DMY") if newv==0
replace start = severe_disease_fu_date_update if newv==1
gen stop = severe_disease_fu_date_update if newv==0 & severe_disease==1
count if stop!=.
replace stop = end_date if stop==.
count if stop==.
replace severe_disease=0 if newv==0 & severe_disease_bl==0
replace severe_disease=1 if newv==0 & severe_disease_bl==1
save ./output/tv_severe_disease_check, replace
keep patient_id severe_disease start stop
* Check data
count if start==stop
* drop if start is same as stop - should not drop any in real data
drop if start==stop
count if stop<start
codebook patient_id
save ./output/tv_severe_disease, replace
restore
* Make age time-varying i.e. updating on 1st January each year
use `tempfile', clear
* Determine end of follow-up date
gen dereg_dateA = date(dereg_date, "YMD")
format %dD/N/CY dereg_dateA
drop dereg_date
gen died_dateA = date(died_date_ons, "YMD")
format %dD/N/CY died_dateA
drop died_date_ons
* Determine end of follow-up then add rows to update age for each year of follow-up
gen end_study = date("31/12/2022", "DMY")
egen end_date = rowmin(dereg_dateA end_study died_dateA)
keep patient_id age end_date
gen yr_end = year(end_date)
* Add rows to update age
gen expand = 3 if yr_end==2022
replace expand = 2 if yr_end==2021
replace expand = 1 if yr_end==2020
tab expand, m
expand expand, gen(newv)
tab newv, nolabel
bys patient_id newv: gen number = _n
tab newv number, nolabel
gen start = date("01/03/2020", "DMY") if newv==0
replace start = date("01/01/2021", "DMY") if newv==1 & number==1
replace start = date("01/01/2022", "DMY") if newv==1 & number==2
gen stop = date("01/01/2021", "DMY") if newv==0
replace stop = end_date if newv==0 & end_date<stop
replace stop = date("01/01/2022", "DMY") if newv==1 & number==1
replace stop = end_date if newv==1 & number==1 & end_date<stop
replace stop = date("31/12/2022", "DMY") if newv==1 & number==2
replace stop = end_date if newv==1 & number==2 & end_date<stop
gen age_tv = age if newv==0
replace age_tv = age+1 if newv==1 & number==1
replace age_tv = age+2 if newv==1 & number==2
keep patient_id start stop age_tv
drop if start==stop
codebook patient_id
save ./output/tv_age, replace
* COVID waves
use `tempfile', clear
* Determine end of follow-up date
gen dereg_dateA = date(dereg_date, "YMD")
format %dD/N/CY dereg_dateA
drop dereg_date
gen died_date_onsA = date(died_date_ons, "YMD")
format %dD/N/CY died_date_onsA
drop died_date_ons
* Determine end of follow-up then add rows to update waves
gen end_study = date("31/12/2022", "DMY")
egen end_date = rowmin(dereg_dateA end_study died_date_onsA)
gen expand = 1 if end_date <= date("31/08/2020", "DMY")
replace expand = 2 if end_date <= date("30/06/2021", "DMY") & expand==.
replace expand = 3 if end_date <= date("30/11/2021", "DMY") & expand==.
replace expand = 4 if end_date <= date("31/12/2022", "DMY") & expand==.
tab expand
keep patient_id expand end_date
expand expand, gen(newv)
tab newv
bys patient_id newv: gen number = _n
* wave 1 row
gen start = date("01/03/2020", "DMY") if newv==0
gen wave = 1 if newv==0
gen stop = date("01/09/2020", "DMY") if newv==0
replace stop = end_date if end_date<stop & wave==1
tab expand if stop==end_date & wave==1
* wave 2 row
replace start = date("01/09/2020", "DMY") if newv==1 & number==1
replace wave = 2 if newv==1 & number==1
replace stop = date("01/07/2021", "DMY") if newv==1 & number==1
replace stop = end_date if end_date<stop & wave==2
* wave 3 row
replace start = date("01/07/2021", "DMY") if newv==1 & number==2
replace wave = 3 if newv==1 & number==2
replace stop = date("01/12/2021", "DMY") if newv==1 & number==2
replace stop = end_date if end_date<stop & wave==3
* wave 4 row
replace start = date("01/12/2021", "DMY") if newv==1 & number==3
replace wave = 4 if newv==1 & number==3
replace stop = date("31/12/2022", "DMY") if newv==1 & number==3
replace stop = end_date if end_date<stop & wave==4
keep patient_id start stop wave
drop if start==stop
codebook patient_id
save ./output/tv_waves, replace
* Create files for outcomes
use `tempfile', clear
* Determine end of follow-up date
foreach var in died_date_ons hosp_covid_primary hosp_covid_any dereg_date {
gen `var'A = date(`var', "YMD")
format `var'A %dD/N/CY
drop `var'
gen yr_`var' = year(`var'A)
tab yr_`var'
replace `var'A=. if yr_`var'==2023
}
gen end_study = date("31/12/2022", "DMY")
egen end_date = rowmin(dereg_dateA end_study died_date_onsA)
* Create flag indicating reason for end of follow-up
gen end_date_flag = (end_date==dereg_dateA)
replace end_date_flag = 2 if end_date==died_date_onsA
replace end_date_flag = 3 if end_date==end_study
replace died_date_onsA=. if end_date<died_date_onsA
replace died_ons_covid_flag_any=0 if end_date<died_date_onsA & died_ons_covid_flag_any==1
replace hosp_covid_anyA=. if end_date<hosp_covid_anyA
gen hosp_any_flag = hosp_covid_anyA!=.
* Composite outcome
gen hosp_died_composite = (died_ons_covid_flag_any==1 | hosp_any_flag==1)
egen hosp_died_dateA = rowmin(died_date_onsA hosp_covid_anyA)
replace hosp_died_dateA=. if hosp_died_composite==0
* Check how composite is made up
tab died_ons_covid_flag_any hosp_any_flag
di "Number where composite date = death date only"
count if hosp_died_dateA==died_date_onsA & hosp_died_dateA!=. & hosp_died_dateA!=hosp_covid_anyA
di "Number where composite = hospitalisation date only"
count if hosp_died_dateA==hosp_covid_anyA & hosp_died_dateA!=. & hosp_died_dateA!=died_date_onsA
di "Number where composite = both death and hospitalisation date
count if hosp_died_dateA==died_date_onsA & hosp_died_dateA!=. & hosp_died_dateA==hosp_covid_anyA
* Make file for each outcome: covid death, hospitalisation and composite
* Files contain patient ID, start = start study, stop = either end date for patient or date of outcome
* and flag for outcome
* COVID death - covid code in any position
preserve
keep patient_id end_date died_ons_covid_flag_any died_date_onsA
gen start = date("01/03/2020", "DMY")
egen stop = rowmin(died_date_onsA end_date)
count if died_ons_covid_flag_any ==1 & end_date<died_date_onsA
replace died_ons_covid_flag_any=0 if end_date<died_date_onsA
keep patient_id start stop died_ons_covid_flag_any
rename died_ons_covid_flag_any died_covid_any_flag
save ./output/tv_outcome_died_covid_any, replace
tab died_covid_any_flag, m
codebook patient_id
restore
* Hospitalisation - covid code in any position
preserve
keep patient_id end_date hosp_any_flag hosp_covid_anyA end_date_flag
gen start = date("01/03/2020", "DMY")
egen stop = rowmin(hosp_covid_anyA end_date)
count if hosp_any_flag ==1 & end_date<hosp_covid_anyA
tab end_date_flag if hosp_any_flag ==1 & end_date<hosp_covid_anyA
replace hosp_any_flag=0 if end_date<hosp_covid_anyA
keep patient_id start stop hosp_any_flag
save ./output/tv_outcome_hosp_any, replace
tab hosp_any_flag, m
codebook patient_id
count
bys patient_id: egen total_hosp = total(hosp_any_flag)
tab total_hosp
restore
* Composite
preserve
keep patient_id hosp_died* end_date
gen start = date("01/03/2020", "DMY")
egen stop = rowmin(hosp_died_dateA end_date)
count if hosp_died_composite==1 & end_date<hosp_died_dateA
replace hosp_died_composite=0 if end_date<hosp_died_dateA
keep patient_id start stop hosp_died_composite
rename hosp_died_composite composite_any_flag
save ./output/tv_outcome_composite_any, replace
tab composite_any_flag, m
codebook patient_id
restore
* Merge files together for each outcome
* Composite
use ./output/tv_severe_disease, clear
tvc_merge start stop using ./output/tv_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_120, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_composite_any, id(patient_id) failure(composite_any_flag)
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge - there will be missings for rows after event
tab composite_any_flag, m
missings report
drop if composite_any_flag==.
save ./output/tv_vars_composite_any, replace
* COVID death - covid code in any position
use ./output/tv_severe_disease, clear
tvc_merge start stop using ./output/tv_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_120, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_died_covid_any, id(patient_id) failure(died_covid_any_flag)
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge
tab died_covid_any_flag, m
missings report
save ./output/tv_vars_died_covid_any, replace
* COVID hospitalisation - covid code in any position
use ./output/tv_severe_disease, clear
tvc_merge start stop using ./output/tv_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_120, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_hosp_any, id(patient_id) failure(hosp_any_flag)
* Check number of outcomes after merge
tab hosp_any_flag, m
bys patient_id: egen total_hosp = total(hosp_any_flag)
tab total_hosp
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge - there will be missings for rows after event
tab hosp_any_flag, m
missings report
drop if hosp_any_flag==.
save ./output/tv_vars_hosp_any, replace
/* Format file with static covariates: sex, region, covid high risk conditions,
ethnicity, imd, bmi, smoking. */
** Need to decide on second line therapies
use `tempfile', clear
describe
* Format variables
* Sex
gen male = 1 if sex == "M"
replace male = 0 if sex == "F"
* Ethnicity
replace ethnicity=6 if ethnicity==0
label define eth5 1 "White" ///
2 "Mixed" ///
3 "Asian" ///
4 "Black" ///
5 "Other" ///
6 "Unknown"
label values ethnicity eth5
safetab ethnicity, m
* Create White vs non-White ethnicity variable
gen eth_bin = (ethnicity!=1)
replace eth_bin = 2 if ethnicity==6
label define eth_3 0 "White" 1 "Non-White" 2 "Unknown"
label values eth_bin eth_3
tab eth_bin ethnicity, m
* IMD - should not be missing (i.e. 0) in real data
replace imd=6 if imd==0
* BMI categories
* assume BMI's less than 10 are incorrect and set to missing
sum bmi, d
replace bmi = . if bmi<10
egen bmi_cat = cut(bmi), at(0, 1, 18.5, 24.9, 29.9, 39.9, 100) icodes
bys bmi_cat: sum bmi
* assume missing . is healthy range BMI
replace bmi_cat = 2 if bmi_cat==.
label define bmi 1 "Underweight" 2 "Healthy range" 3 "Overweight" 4 "Obese" 5 "Morbidly obese"
label values bmi_cat bmi
tab bmi_cat
* Smoking status - assume missings are non-smokers
gen smoking = 0 if smoking_status=="N"
replace smoking = 1 if smoking_status=="S"
replace smoking = 2 if smoking_status=="E"
replace smoking = 1 if smoking==.
label define smok 1 "Current smoker" 2 "Ex-smoker" 0 "Never smoked"
label values smoking smok
tab smoking
* High risk covid conditions
replace oral_steroid_drugs_nhsd=. if oral_steroid_drug_nhsd_3m_count < 2 & oral_steroid_drug_nhsd_12m_count < 4
gen imid_nhsd=max(oral_steroid_drugs_nhsd, immunosuppresant_drugs_nhsd)
gen rare_neuro_nhsd = max(multiple_sclerosis_nhsd, motor_neurone_disease_nhsd, myasthenia_gravis_nhsd, huntingtons_disease_nhsd)
gen solid_organ_transplant_bin = solid_organ_transplant_nhsd_new!=""
gen any_high_risk_condition = max(learning_disability_nhsd_snomed, cancer_opensafely_snomed_new, haematological_disease_nhsd, ///
ckd_stage_5_nhsd, imid_nhsd, immunosupression_nhsd_new, hiv_aids_nhsd, solid_organ_transplant_bin, rare_neuro_nhsd)
* Time from most recent vaccination on 1st March 2021
gen date_most_recent_cov_vacA = date(date_most_recent_cov_vac, "YMD")
gen time_vacc = date("01Mar2021", "DMY") - date_most_recent_cov_vacA
sum time_vacc, d
xtile time_vacc_cat = time_vacc, nq(4)
replace time_vacc_cat = 5 if time_vacc_cat == .
bys time_vacc_cat: sum time_vacc
label define vacc_t 1 "Q1 closest vaccination" 2 "Q2" 3 "Q3" 4 "Q4 furthest vaccination" 5 "No vaccination"
label values time_vacc_cat vacc_t
* Exploring death by liver disease
gen died_liver_any = died_ons_liver_flag_any==1 & died_ons_covid_flag_any!=1
gen died_liver_underlying = died_ons_liver_flag_underlying==1 & died_ons_covid_flag_any!=1
keep patient_id male stp any_high_risk_condition ethnicity imd bmi_cat smoking eth_bin time_vacc_cat has_pbc oca_bl died_liver_any died_liver_underlying
tempfile basefile
save `basefile'
foreach var in died_covid_any hosp_any composite_any {
preserve
merge 1:m patient_id using ./output/tv_vars_`var'
drop _merge
save ./output/an_dataset_`var', replace
restore
}
*****************************************
* 90 day file: sensitivity analysis
use ./output/time_varying_udca_90, clear
codebook patient_id
merge m:1 patient_id using `tempfile', keepusing(severe_disease_fu_date severe_disease_bl covid_vacc_first_date liver_transplant_fu_date dereg_date died_date_ons)
* Should only be required for dummy data
keep if _merge==3
* Only using start date as this is when udca exposure or 6 monthly check occurs
drop stop
* Add in rows with each of the 6 monthly assessment dates
gen expand=1
bys patient_id (start): replace expand=6 if _n==_N
expand expand, generate(newv)
bys patient_id newv: gen number = _n
replace start = date("01/09/2020", "DMY") if newv==1 & number==1
replace start = date("01/03/2021", "DMY") if newv==1 & number==2
replace start = date("01/09/2021", "DMY") if newv==1 & number==3
replace start = date("01/03/2022", "DMY") if newv==1 & number==4
replace start = date("01/09/2022", "DMY") if newv==1 & number==5
* This gives a file where start dates are either exposure switching or 6-montly assessment dates
* These will be used to determine the closest date to time-varying covariate dates identified in study definition
sort patient_id start
drop newv
* Determine end of follow-up date
gen dereg_dateA = date(dereg_date, "YMD")
format %dD/N/CY dereg_dateA
drop dereg_date
gen died_dateA = date(died_date_ons, "YMD")
format %dD/N/CY died_dateA
drop died_date_ons
gen end_study = date("31/12/2022", "DMY")
egen end_date = rowmin(dereg_dateA end_study died_dateA)
* Take out assessments after end_date
drop if start>end_date
* Determine number of days between each covariate and dates of UDCA change or 6 monthly check date
bys patient_id (start): egen last_assess = max(start)
* Format dates and identify date nearest after covariate updates
foreach var in covid_vacc_first_date severe_disease_fu_date liver_transplant_fu_date {
gen `var'A = date(`var', "YMD")
format `var'A %dD/N/CY
drop `var'
* time between date of record and change in covariate - positive values are those where record starts after
* covariate change date
gen time_`var' = start - `var'A
* Set time variable for records prior to change as missing
replace time_`var'=. if time_`var'<0
* Check if any are on index date (1st March) potentially severe disease - not others
di "Number where time-varying update same as assessment date"
count if time_`var'==0
di "Number where time-varying update is 1st March 2020"
count if `var'A==date("01/03/2020", "DMY")
* Find earliest start date after covariate update
* Note: there are some changes that occur after the last available 6 month assessment or exposure change
bys patient_id (time_`var'): gen `var'_updateN = start[1] if `var'A!=. & time_`var'!=.
* Spread to all rows for patient
bys patient_id: egen `var'_update = max(`var'_updateN)
format `var'_update %dD/N/CY
di "Number where new variable is prior to original variable (should be 0)"
count if `var'_update < `var'A
di "Number where time varying update is after last assessment date"
count if `var'A > last_assess & `var'A!=. & last_assess==start
di "Number of time-varying changes originally"
* Using last_assess==start to count patients rather than rows
count if `var'A!=. & last_assess==start
di "Number of dates for time-varying update"
count if `var'_update!=. & last_assess==start & `var'A!=.
}
* For severe disease only update variable if no severe disease at baseline
count if severe_disease_fu_date_update==date("01/03/2020", "DMY") & severe_disease_bl!=1
tab severe_disease_bl
replace severe_disease_fu_date_update = . if severe_disease_bl==1
* Keep only change dates to create time-varying dataset
keep patient_id covid_vacc_first_date_update severe_disease_fu_date_update liver_transplant_fu_date_update severe_disease_bl end_date
* Chercking number of patient_id's
codebook patient_id
duplicates drop
codebook patient_id
count
* Create file for each covariate to merge onto udca exposure file
* First covid vaccination and liver transplant as all people will begin at zero
rename liver_transplant_fu_date_update liver_trans_date_update
foreach var in covid_vacc_first liver_trans {
preserve
keep patient_id `var'_date_update end_date
* Drop updates after the end of follow-up
count if `var'_date>=end_date
replace `var'_date = . if `var'_date>=end_date
* Flag if variable is updated
gen `var'=(`var'_date_update!=.)
* Create extra row if variable is updated
gen expand = `var'+1
tab expand
expand expand, gen(newv)
* Update variables so row for when zero and row for when updated - if not updated whole time will be zero
gen start = date("01/03/2020", "DMY") if newv==0
replace start = `var'_date_update if newv==1
gen stop = `var'_date_update if newv==0
replace stop = end_date if stop==.
replace `var'=0 if newv==0
save ./output/tv_`var'_check, replace
keep patient_id `var' start stop
* Check data
count if start==stop
* drop where start is same as stop - should not drop any in real data
drop if start==stop
count if stop<start
codebook patient_id
save ./output/tv_90_`var', replace
restore
}
* Same for liver disease severity, but variable can start at 1 and remain for whole of follow-up
preserve
keep patient_id severe_disease_fu_date_update end_date severe_disease_bl
* Drop updates after the end of follow-up
replace severe_disease_fu_date_update=. if severe_disease_fu_date_update>=end_date
* Flag if variable is updated
gen severe_disease=(severe_disease_fu_date_update!=.)
tab severe_disease
replace severe_disease = 0 if severe_disease_bl==1
* Create extra row if variable is updated
gen expand = severe_disease + 1
tab expand
expand expand, gen(newv)
tab newv
* Update variables so row for when zero and row for when updated - if not updated whole time will be zero
gen start = date("01/03/2020", "DMY") if newv==0
replace start = severe_disease_fu_date_update if newv==1
gen stop = severe_disease_fu_date_update if newv==0 & severe_disease==1
count if stop!=.
replace stop = end_date if stop==.
count if stop==.
replace severe_disease=0 if newv==0 & severe_disease_bl==0
replace severe_disease=1 if newv==0 & severe_disease_bl==1
save ./output/tv_severe_disease_check, replace
keep patient_id severe_disease start stop
* Check data
count if start==stop
* drop if start is same as stop - should not drop any in real data
drop if start==stop
count if stop<start
codebook patient_id
save ./output/tv_90_severe_disease, replace
restore
* Merge files together for each outcome
* Composite
use ./output/tv_90_severe_disease, clear
tvc_merge start stop using ./output/tv_90_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_90_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_90, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_composite_any, id(patient_id) failure(composite_any_flag)
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge - there will be missings for rows after event
tab composite_any_flag, m
missings report
drop if composite_any_flag==.
save ./output/tv_vars_90_composite_any, replace
* COVID death - covid code in any position
use ./output/tv_90_severe_disease, clear
tvc_merge start stop using ./output/tv_90_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_90_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_90, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_died_covid_any, id(patient_id) failure(died_covid_any_flag)
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge
tab died_covid_any_flag, m
missings report
save ./output/tv_vars_90_died_covid_any, replace
* COVID hospitalisation - covid code in any position
use ./output/tv_90_severe_disease, clear
tvc_merge start stop using ./output/tv_90_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_90_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_90, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_hosp_any, id(patient_id) failure(hosp_any_flag)
* Check number of outcomes after merge
tab hosp_any_flag, m
bys patient_id: egen total_hosp = total(hosp_any_flag)
tab total_hosp
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge - there will be missings for rows after event
tab hosp_any_flag, m
missings report
drop if hosp_any_flag==.
save ./output/tv_vars_90_hosp_any, replace
use `basefile', clear
foreach var in died_covid_any hosp_any composite_any {
preserve
merge 1:m patient_id using ./output/tv_vars_90_`var'
drop _merge
save ./output/an_dataset_90_`var', replace
restore
}
*** 120 day overlap file: sensitivity analysis
use ./output/time_varying_udca_overlap_120, clear
codebook patient_id
merge m:1 patient_id using `tempfile', keepusing(severe_disease_fu_date severe_disease_bl covid_vacc_first_date liver_transplant_fu_date dereg_date died_date_ons)
* Should only be required for dummy data
keep if _merge==3
* Only using start date as this is when udca exposure or 6 monthly check occurs
drop stop
* Add in rows with each of the 6 monthly assessment dates
gen expand=1
bys patient_id (start): replace expand=6 if _n==_N
expand expand, generate(newv)
bys patient_id newv: gen number = _n
replace start = date("01/09/2020", "DMY") if newv==1 & number==1
replace start = date("01/03/2021", "DMY") if newv==1 & number==2
replace start = date("01/09/2021", "DMY") if newv==1 & number==3
replace start = date("01/03/2022", "DMY") if newv==1 & number==4
replace start = date("01/09/2022", "DMY") if newv==1 & number==5
* This gives a file where start dates are either exposure switching or 6-montly assessment dates
* These will be used to determine the closest date to time-varying covariate dates identified in study definition
sort patient_id start
drop newv
* Determine end of follow-up date
gen dereg_dateA = date(dereg_date, "YMD")
format %dD/N/CY dereg_dateA
drop dereg_date
gen died_dateA = date(died_date_ons, "YMD")
format %dD/N/CY died_dateA
drop died_date_ons
gen end_study = date("31/12/2022", "DMY")
egen end_date = rowmin(dereg_dateA end_study died_dateA)
* Take out assessments after end_date
drop if start>end_date
* Determine number of days between each covariate and dates of UDCA change or 6 monthly check date
bys patient_id (start): egen last_assess = max(start)
* Format dates and identify date nearest after covariate updates
foreach var in covid_vacc_first_date severe_disease_fu_date liver_transplant_fu_date {
gen `var'A = date(`var', "YMD")
format `var'A %dD/N/CY
drop `var'
* time between date of record and change in covariate - positive values are those where record starts after
* covariate change date
gen time_`var' = start - `var'A
* Set time variable for records prior to change as missing
replace time_`var'=. if time_`var'<0
* Check if any are on index date (1st March) potentially severe disease - not others
di "Number where time-varying update same as assessment date"
count if time_`var'==0
di "Number where time-varying update is 1st March 2020"
count if `var'A==date("01/03/2020", "DMY")
* Find earliest start date after covariate update
* Note: there are some changes that occur after the last available 6 month assessment or exposure change
bys patient_id (time_`var'): gen `var'_updateN = start[1] if `var'A!=. & time_`var'!=.
* Spread to all rows for patient
bys patient_id: egen `var'_update = max(`var'_updateN)
format `var'_update %dD/N/CY
di "Number where new variable is prior to original variable (should be 0)"
count if `var'_update < `var'A
di "Number where time varying update is after last assessment date"
count if `var'A > last_assess & `var'A!=. & last_assess==start
di "Number of time-varying changes originally"
* Using last_assess==start to count patients rather than rows
count if `var'A!=. & last_assess==start
di "Number of dates for time-varying update"
count if `var'_update!=. & last_assess==start & `var'A!=.
}
* For severe disease only update variable if no severe disease at baseline
count if severe_disease_fu_date_update==date("01/03/2020", "DMY") & severe_disease_bl!=1
tab severe_disease_bl
replace severe_disease_fu_date_update = . if severe_disease_bl==1
* Keep only change dates to create time-varying dataset
keep patient_id covid_vacc_first_date_update severe_disease_fu_date_update liver_transplant_fu_date_update severe_disease_bl end_date
* Chercking number of patient_id's
codebook patient_id
duplicates drop
codebook patient_id
count
* Create file for each covariate to merge onto udca exposure file
* First covid vaccination and liver transplant as all people will begin at zero
rename liver_transplant_fu_date_update liver_trans_date_update
foreach var in covid_vacc_first liver_trans {
preserve
keep patient_id `var'_date_update end_date
* Drop updates after the end of follow-up
count if `var'_date>=end_date
replace `var'_date = . if `var'_date>=end_date
* Flag if variable is updated
gen `var'=(`var'_date_update!=.)
* Create extra row if variable is updated
gen expand = `var'+1
tab expand
expand expand, gen(newv)
* Update variables so row for when zero and row for when updated - if not updated whole time will be zero
gen start = date("01/03/2020", "DMY") if newv==0
replace start = `var'_date_update if newv==1
gen stop = `var'_date_update if newv==0
replace stop = end_date if stop==.
replace `var'=0 if newv==0
save ./output/tv_`var'_check, replace
keep patient_id `var' start stop
* Check data
count if start==stop
* drop where start is same as stop - should not drop any in real data
drop if start==stop
count if stop<start
codebook patient_id
save ./output/tv_overlap_120_`var', replace
restore
}
* Same for liver disease severity, but variable can start at 1 and remain for whole of follow-up
preserve
keep patient_id severe_disease_fu_date_update end_date severe_disease_bl
* Drop updates after the end of follow-up
replace severe_disease_fu_date_update=. if severe_disease_fu_date_update>=end_date
* Flag if variable is updated
gen severe_disease=(severe_disease_fu_date_update!=.)
tab severe_disease
replace severe_disease = 0 if severe_disease_bl==1
* Create extra row if variable is updated
gen expand = severe_disease + 1
tab expand
expand expand, gen(newv)
tab newv
* Update variables so row for when zero and row for when updated - if not updated whole time will be zero
gen start = date("01/03/2020", "DMY") if newv==0
replace start = severe_disease_fu_date_update if newv==1
gen stop = severe_disease_fu_date_update if newv==0 & severe_disease==1
count if stop!=.
replace stop = end_date if stop==.
count if stop==.
replace severe_disease=0 if newv==0 & severe_disease_bl==0
replace severe_disease=1 if newv==0 & severe_disease_bl==1
save ./output/tv_severe_disease_check, replace
keep patient_id severe_disease start stop
* Check data
count if start==stop
* drop if start is same as stop - should not drop any in real data
drop if start==stop
count if stop<start
codebook patient_id
save ./output/tv_overlap_120_severe_disease, replace
restore
* Merge files together for each outcome
* Composite
use ./output/tv_overlap_120_severe_disease, clear
tvc_merge start stop using ./output/tv_overlap_120_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_overlap_120_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_overlap_120, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_composite_any, id(patient_id) failure(composite_any_flag)
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge - there will be missings for rows after event
tab composite_any_flag, m
missings report
drop if composite_any_flag==.
save ./output/tv_vars_overlap_120_composite_any, replace
* COVID death - covid code in any position
use ./output/tv_overlap_120_severe_disease, clear
tvc_merge start stop using ./output/tv_overlap_120_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_overlap_120_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_overlap_120, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_died_covid_any, id(patient_id) failure(died_covid_any_flag)
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge
tab died_covid_any_flag, m
missings report
save ./output/tv_vars_overlap_120_died_covid_any, replace
* COVID hospitalisation - covid code in any position
use ./output/tv_overlap_120_severe_disease, clear
tvc_merge start stop using ./output/tv_overlap_120_covid_vacc_first, id(patient_id)
tvc_merge start stop using ./output/tv_overlap_120_liver_trans, id(patient_id)
tvc_merge start stop using ./output/time_varying_udca_overlap_120, id(patient_id)
tvc_merge start stop using ./output/tv_age, id(patient_id)
tvc_merge start stop using ./output/tv_outcome_hosp_any, id(patient_id) failure(hosp_any_flag)
* Check number of outcomes after merge
tab hosp_any_flag, m
bys patient_id: egen total_hosp = total(hosp_any_flag)
tab total_hosp
* Dummy drug data includes people not in cohort, so drop these - should not be any in real data
drop if age_tv==.
codebook patient_id
* Check number of outcomes after merge - there will be missings for rows after event
tab hosp_any_flag, m
missings report
drop if hosp_any_flag==.
save ./output/tv_vars_overlap_120_hosp_any, replace
use `basefile', clear
foreach var in died_covid_any hosp_any composite_any {
preserve
merge 1:m patient_id using ./output/tv_vars_overlap_120_`var'
drop _merge
save ./output/an_dataset_overlap_120_`var', replace
restore
}
*** vaccination file: sensitivity analysis
** Creating file where count of vaccinations updates over time
use ./output/time_varying_udca_120, clear
codebook patient_id
merge m:1 patient_id using `tempfile', keepusing(covid_vacc* dereg_date died_date_ons date_most_recent_cov_vac)
* Should only be required for dummy data
keep if _merge==3
* Only using start date as this is when udca exposure or 6 monthly check occurs
drop stop
* Add in rows with each of the 6 monthly assessment dates
gen expand=1
bys patient_id (start): replace expand=6 if _n==_N
expand expand, generate(newv)
bys patient_id newv: gen number = _n
replace start = date("01/09/2020", "DMY") if newv==1 & number==1
replace start = date("01/03/2021", "DMY") if newv==1 & number==2
replace start = date("01/09/2021", "DMY") if newv==1 & number==3
replace start = date("01/03/2022", "DMY") if newv==1 & number==4
replace start = date("01/09/2022", "DMY") if newv==1 & number==5
* This gives a file where start dates are either exposure switching or 6-montly assessment dates
* These will be used to determine the closest date to time-varying covariate dates identified in study definition
sort patient_id start
drop newv
* Determine end of follow-up date
gen dereg_dateA = date(dereg_date, "YMD")
format %dD/N/CY dereg_dateA
drop dereg_date
gen died_dateA = date(died_date_ons, "YMD")
format %dD/N/CY died_dateA
drop died_date_ons
gen end_study = date("31/12/2022", "DMY")
egen end_date = rowmin(dereg_dateA end_study died_dateA)
* Take out assessments after end_date
drop if start>end_date
* Determine number of days between each covariate and dates of UDCA change or 6 monthly check date
bys patient_id (start): egen last_assess = max(start)
* Format dates and identify date nearest after covariate updates
foreach var in covid_vacc_first_date covid_vacc_second_date covid_vacc_third_date covid_vacc_fourth_date covid_vacc_fifth_date {
gen `var'A = date(`var', "YMD")
format `var'A %dD/N/CY
drop `var'
* time between date of record and change in covariate - positive values are those where record starts after
* covariate change date
gen time_`var' = start - `var'A
* Set time variable for records prior to change as missing
replace time_`var'=. if time_`var'<0
* Check if any are on index date (1st March) potentially severe disease - not others
di "Number where time-varying update same as assessment date"
count if time_`var'==0
di "Number where time-varying update is 1st March 2020"
count if `var'A==date("01/03/2020", "DMY")
* Find earliest start date after covariate update
* Note: there are some changes that occur after the last available 6 month assessment or exposure change
bys patient_id (time_`var'): gen `var'_updateN = start[1] if `var'A!=. & time_`var'!=.
* Spread to all rows for patient
bys patient_id: egen `var'_update = max(`var'_updateN)
format `var'_update %dD/N/CY
di "Number where new variable is prior to original variable (should be 0)"
count if `var'_update < `var'A
di "Number where time varying update is after last assessment date"
count if `var'A > last_assess & `var'A!=. & last_assess==start
di "Number of time-varying changes originally"
* Using last_assess==start to count patients rather than rows
count if `var'A!=. & last_assess==start
di "Number of dates for time-varying update"
count if `var'_update!=. & last_assess==start & `var'A!=.
}
* Keep only change dates to create time-varying dataset
keep patient_id covid_vacc_first_date_update covid_vacc_second_date_update covid_vacc_third_date_update covid_vacc_fourth_date_update covid_vacc_fifth_date_update end_date date_most_recent_cov_vac
* Chercking number of patient_id's
codebook patient_id
duplicates drop
codebook patient_id
count
* Create dataset where rows are updated when person is vaccinated/re-vaccinated
foreach var in covid_vacc_first covid_vacc_second covid_vacc_third covid_vacc_fourth covid_vacc_fifth {
count if `var'_date_update>=end_date & `var'_date_update!=.
replace `var'_date_update = . if `var'_date_update>=end_date
* Create flag for each vaccination date
gen `var'_flag = (`var'_date_update!=.)
* Create flag for if date is prior to 1st March 2021
gen `var'_prior = `var'_date_update <= date("01Mar2021", "DMY")
tab `var'_prior, m
}
* Check not date where prior vaccination is missing
count if covid_vacc_first_flag==0 & covid_vacc_second_flag==1
count if covid_vacc_second_flag==0 & covid_vacc_third_flag==1
count if covid_vacc_third_flag==0 & covid_vacc_fourth_flag==1
count if covid_vacc_fourth_flag==0 & covid_vacc_fifth_flag==1
* Determine total number of vaccinations
egen total_vaccs = rowtotal(covid_vacc_first_flag covid_vacc_second_flag covid_vacc_third_flag covid_vacc_fourth_flag covid_vacc_fifth_flag)
tab total_vaccs
egen total_vaccs_prior = rowtotal(covid_vacc_first_prior covid_vacc_second_prior covid_vacc_third_prior covid_vacc_fourth_prior covid_vacc_fifth_prior)