generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
101_drug_prep.do
633 lines (510 loc) · 19.7 KB
/
101_drug_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
/*==============================================================================
DO FILE NAME: drug_prep.do
DATE: 13/04/2023
AUTHOR: R Costello
DESCRIPTION OF FILE: Import UDCA drug data and clean and reformat to long form
==============================================================================*/
adopath + ./analysis/ado
* Open a log file
cap log using ./logs/drug_prep.log, replace
cap mkdir ./output/tables/
tempfile tempfile
import delimited using ./output/input_pbc.csv
save `tempfile'
** First import udca population
import delimited using ./output/input_additional.csv, clear
describe
*Some records identified in study_definition_additional
*missings report, minimum(12400)
missings dropvars udca_*, force
* Format dates
* First count number of variables for loop
describe
local var = r(k)-1
forvalues i=1/`var' {
gen udcaA`i' = date(udca_`i', "YMD")
format udcaA`i' %dD/N/CY
drop udca_`i'
}
* Reshape to long format
reshape long udcaA, i(patient_id) j(presc_number)
* Drop rows where there is no prescription
drop if udcaA==.
* Check that prescriptions are in order
gen order_flag = (udcaA<udcaA[_n-1] & patient_id==patient_id[_n-1])
tab order_flag
* Create flag if any out of order prescriptions in the data - should only be in dummy data
egen out_of_order = max(order_flag)
tab out_of_order
* Sort if out of order - should only apply to dummy data
sort patient_id udcaA
drop order_flag
* Check in order
gen order_flag = (udcaA<udcaA[_n-1] & patient_id==patient_id[_n-1])
tab order_flag
* Identify prescriptions with the same date
bys patient_id (udcaA): gen dup_presc = (udcaA==udcaA[_n-1])
tab dup_presc
* Drop them
drop if dup_presc
drop order_flag out_of_order dup_presc presc_number
* Re-count number of prescriptions
gen udca=1
bys patient_id: egen total_no_presc = total(udca)
sum total_no_presc, d
* Determine length of time from previous prescription
bys patient_id (udcaA): gen time_previous = udcaA - udcaA[_n-1]
sum time_previous, d
gen year = year(udcaA)
bys year: sum time_previous, d
* Drop records in 2023 as this is after end of study
drop if year==2023
* Loop through assuming end date is 60 up to 180 days after prescription generated
forvalues i = 60(30)180 {
preserve
gen stop_date = udcaA + `i'
format stop_date %dD/N/CY
* Drop rows where prescriptions ends prior to March 2020
count if stop_date <= date("01March2020", "DMY")
drop if stop_date <= date("01March2020", "DMY")
* Collapse adjacent prescriptions that are continuous prescribing into one period
* Flag if start date is greater than assumed stop date of previous prescription
bys patient_id (udcaA): gen spell = udcaA > stop_date[_n-1]
bys patient_id (udcaA): replace spell = 1 if _n==1
* Flag each row associated with each spell
bys patient_id (udcaA): gen spell_sum = sum(spell)
* Start date of spell
bys patient_id spell_sum (udcaA): gen start = udcaA[1]
bys patient_id spell_sum (udcaA): gen stop = stop_date[_N]
format start stop %dD/N/CY
keep patient_id start stop udca total_no_presc
duplicates drop
* Add in rows where there are gaps between prescriptions
bys patient_id (start): gen to_expand = 1 + (stop < start[_n+1])*(start[_n+1]~=.)
tab to_expand
expand to_expand
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab to_expand new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace start = stop if new_record==1
bys patient_id (start): replace stop = start[_n+1] if new_record==1
drop new_record to_expand
* Checks
count if stop<start
sort patient_id start
count if stop[_n-1]>start & patient_id==patient_id[_n-1]
* Update rows to match follow-up time
* merge variables from original study definition
merge m:1 patient_id using `tempfile', keepusing(dereg_date died_date_ons udca_count_bl)
tab _merge
* Update variables for people without any prescriptions
replace start = date("01/03/2020", "DMY") if _merge==2
replace udca = 0 if _merge==2
replace stop = date("31/12/2022", "DMY") if _merge==2
replace total_no_presc = 0 if _merge==2
* 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)
* 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_dateA
replace end_date_flag = 3 if end_date==end_study
* Count number of days of follow-up
gen total_fu = end_date - date("01/03/2020", "DMY")
** Update start and end of follow-up for cohort
* Identify prescriptions that start prior to March 2020
gen start_prior = (start < date("01/03/2020", "DMY"))
replace start = date("01/03/2020", "DMY") if start_prior==1
* Identify first prescriptions that start after March 2020
bys patient_id (start): gen start_after = 1 + (start > date("01/03/2020", "DMY") & _n==1)
tab start_after
expand start_after
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab start_after new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace stop = start if new_record==1
replace start = date("01/03/2020", "DMY") if new_record==1
drop new_record start_after
tab end_date_flag if start==date("01/03/2020", "DMY"), m
* Identify rows that end after study end date for person
gen end_after = (stop > end_date )
gen end_before = (start > end_date)
tab end_after
tab end_after end_date_flag
tab end_before
* Drop whole rows that are after end_date
drop if end_before
* Check years of stop dates
gen yr_stop = year(stop)
tab yr_stop end_after
* Check those flagged as ending after are last row only
bys patient_id (start): gen last = _n==_N
tab last end_after
* Check how many records end after end date - should just be one
bys patient_id: egen tot_end_after = total(end_after)
replace stop = end_date if end_after==1 & end_before==0
* Identify prescriptions that end prior to end of follow-up
bys patient_id (start): gen stop_prior = 1 + (stop < end_date & _n==_N)
tab stop_prior
expand stop_prior
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab stop_prior new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace start = stop if new_record==1
replace stop = end_date if new_record==1
drop new_record stop_prior
di "number where start after stop date"
count if start > stop
di "Number where start equals stop"
count if start==stop
drop if start==stop
* Check that time covered by prescriptions equals total follow-up
gen time = stop - start
sum time, d
bys patient_id: egen total_time = total(time)
gen total_time_unequal = total_time!=total_fu
tab total_time_unequal, m
save ./output/time_varying_udca_all_vars_`i'
* Drop unnecessary variables
keep patient_id start stop udca
save ./output/time_varying_udca_`i'
restore
}
* Sensitivity analysis - add on overlapping days to the next unexposed period up to 120 days. 120 exposure only.
gen stop_date = udcaA + 120
format stop_date %dD/N/CY
* Drop rows where prescriptions ends prior to March 2020
count if stop_date <= date("01March2020", "DMY")
drop if stop_date <= date("01March2020", "DMY")
* Collapse adjacent prescriptions that are continuous prescribing into one period
* Flag if start date is greater than assumed stop date of previous prescription
bys patient_id (udcaA): gen spell = udcaA > stop_date[_n-1]
bys patient_id (udcaA): replace spell = 1 if _n==1
* Flag each row associated with each spell
bys patient_id (udcaA): gen spell_sum = sum(spell)
* Start date of spell
bys patient_id spell_sum (udcaA): gen start = udcaA[1]
bys patient_id spell_sum (udcaA): gen stop = stop_date[_N]
format start stop %dD/N/CY
* Determine number of overlapping days
bys patient_id (udcaA): gen overlap = stop_date - udcaA[_n+1]
* Set to missing if no overlap
replace overlap=. if overlap<=0
bys patient_id spell_sum: egen total_overlap = total(overlap)
sum total_overlap, d
* Only want to add up to 120 days so create variable cut to 120 days if over 120 days overlap
gen overlap_add = total_overlap
replace overlap_add = 120 if total_overlap>120 & total_overlap!=.
keep patient_id start stop udca total_no_presc overlap_add
duplicates drop
sum overlap_add, d
* Add on overlapping days
* Generate new stop date with 120 days added
gen new_stop = stop + overlap_add
* Update stop date with this date if there is an overlap
replace stop = new_stop if overlap_add!=0
* Identify spells where prescriptions overlap and combine rows
bys patient_id (start): gen spell = start > stop[_n-1]
bys patient_id (start): replace spell = 1 if _n==1
bys patient_id (start): gen spell_sum = sum(spell)
sum spell_sum
* Start date of spell
bys patient_id spell_sum (start): gen start_n = start[1]
bys patient_id spell_sum (start): gen stop_n = stop[_N]
format start_n stop_n %dD/N/CY
keep patient_id start_n stop_n udca total_no_presc
rename start_n start
rename stop_n stop
duplicates drop
* Add in rows where there are gaps between prescriptions
bys patient_id (start): gen to_expand = 1 + (stop < start[_n+1])*(start[_n+1]~=.)
tab to_expand
expand to_expand
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab to_expand new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace start = stop if new_record==1
bys patient_id (start): replace stop = start[_n+1] if new_record==1
drop new_record to_expand
* Checks
count if stop<start
sort patient_id start
count if stop[_n-1]>start & patient_id==patient_id[_n-1]
* Update rows to match follow-up time
* merge variables from original study definition
merge m:1 patient_id using `tempfile', keepusing(dereg_date died_date_ons udca_count_bl)
tab _merge
* Update variables for people without any prescriptions
replace start = date("01/03/2020", "DMY") if _merge==2
replace udca = 0 if _merge==2
replace stop = date("31/12/2022", "DMY") if _merge==2
replace total_no_presc = 0 if _merge==2
* 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)
* 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_dateA
replace end_date_flag = 3 if end_date==end_study
* Count number of days of follow-up
gen total_fu = end_date - date("01/03/2020", "DMY")
** Update start and end of follow-up for cohort
* Identify prescriptions that start prior to March 2020
gen start_prior = (start < date("01/03/2020", "DMY"))
replace start = date("01/03/2020", "DMY") if start_prior==1
* Identify first prescriptions that start after March 2020
bys patient_id (start): gen start_after = 1 + (start > date("01/03/2020", "DMY") & _n==1)
tab start_after
expand start_after
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab start_after new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace stop = start if new_record==1
replace start = date("01/03/2020", "DMY") if new_record==1
drop new_record start_after
tab end_date_flag if start==date("01/03/2020", "DMY"), m
* Identify rows that end after study end date for person
gen end_after = (stop > end_date )
gen end_before = (start > end_date)
tab end_after
tab end_after end_date_flag
tab end_before
* Drop whole rows that are after end_date
drop if end_before
* Check years of stop dates
gen yr_stop = year(stop)
tab yr_stop end_after
* Check those flagged as ending after are last row only
bys patient_id (start): gen last = _n==_N
tab last end_after
* Check how many records end after end date - should just be one
bys patient_id: egen tot_end_after = total(end_after)
replace stop = end_date if end_after==1 & end_before==0
* Identify prescriptions that end prior to end of follow-up
bys patient_id (start): gen stop_prior = 1 + (stop < end_date & _n==_N)
tab stop_prior
expand stop_prior
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab stop_prior new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace start = stop if new_record==1
replace stop = end_date if new_record==1
drop new_record stop_prior
di "number where start after stop date"
count if start > stop
di "Number where start equals stop"
count if start==stop
drop if start==stop
* Check that time covered by prescriptions equals total follow-up
gen time = stop - start
sum time, d
bys patient_id: egen total_time = total(time)
gen total_time_unequal = total_time!=total_fu
tab total_time_unequal, m
save ./output/time_varying_udca_overlap_all_vars_120
* Drop unnecessary variables
keep patient_id start stop udca
save ./output/time_varying_udca_overlap_120
* Sensitivity analysis - March 2021 cohort - vaccinations
import delimited using ./output/input_vacc.csv, clear
save `tempfile', replace
** First import udca population
import delimited using ./output/input_addvacc.csv, clear
describe
*Some records identified in study_definition_additional
*missings report, minimum(12400)
missings dropvars udca_*, force
* Format dates
* First count number of variables for loop
describe
local var = r(k)-1
forvalues i=1/`var' {
gen udcaA`i' = date(udca_`i', "YMD")
format udcaA`i' %dD/N/CY
drop udca_`i'
}
* Reshape to long format
reshape long udcaA, i(patient_id) j(presc_number)
* Drop rows where there is no prescription
drop if udcaA==.
* Check that prescriptions are in order
gen order_flag = (udcaA<udcaA[_n-1] & patient_id==patient_id[_n-1])
tab order_flag
* Create flag if any out of order prescriptions in the data - should only be in dummy data
egen out_of_order = max(order_flag)
tab out_of_order
* Sort if out of order - should only apply to dummy data
sort patient_id udcaA
drop order_flag
* Check in order
gen order_flag = (udcaA<udcaA[_n-1] & patient_id==patient_id[_n-1])
tab order_flag
* Identify prescriptions with the same date
bys patient_id (udcaA): gen dup_presc = (udcaA==udcaA[_n-1])
tab dup_presc
* Drop them
drop if dup_presc
drop order_flag out_of_order dup_presc presc_number
* Re-count number of prescriptions
gen udca=1
bys patient_id: egen total_no_presc = total(udca)
sum total_no_presc, d
* Determine length of time from previous prescription
bys patient_id (udcaA): gen time_previous = udcaA - udcaA[_n-1]
sum time_previous, d
gen year = year(udcaA)
bys year: sum time_previous, d
* Drop records in 2023 as this is after end of study
drop if year==2023
* assume end date of prescription is 120 days to match primary analysis
gen stop_date = udcaA + 120
format stop_date %dD/N/CY
* Drop rows where prescriptions ends prior to March 2020
count if stop_date <= date("01March2021", "DMY")
drop if stop_date <= date("01March2021", "DMY")
* Collapse adjacent prescriptions that are continuous prescribing into one period
* Flag if start date is greater than assumed stop date of previous prescription
bys patient_id (udcaA): gen spell = udcaA > stop_date[_n-1]
bys patient_id (udcaA): replace spell = 1 if _n==1
* Flag each row associated with each spell
bys patient_id (udcaA): gen spell_sum = sum(spell)
* Start date of spell
bys patient_id spell_sum (udcaA): gen start = udcaA[1]
bys patient_id spell_sum (udcaA): gen stop = stop_date[_N]
format start stop %dD/N/CY
keep patient_id start stop udca total_no_presc
duplicates drop
* Add in rows where there are gaps between prescriptions
bys patient_id (start): gen to_expand = 1 + (stop < start[_n+1])*(start[_n+1]~=.)
tab to_expand
expand to_expand
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab to_expand new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace start = stop if new_record==1
bys patient_id (start): replace stop = start[_n+1] if new_record==1
drop new_record to_expand
* Checks
count if stop<start
sort patient_id start
count if stop[_n-1]>start & patient_id==patient_id[_n-1]
* Update rows to match follow-up time
* merge variables from original study definition
merge m:1 patient_id using `tempfile', keepusing(dereg_date died_date_ons udca_count_bl)
tab _merge
* Update variables for people without any prescriptions
replace start = date("01/03/2021", "DMY") if _merge==2
replace udca = 0 if _merge==2
replace stop = date("31/12/2022", "DMY") if _merge==2
replace total_no_presc = 0 if _merge==2
* 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)
* 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_dateA
replace end_date_flag = 3 if end_date==end_study
* Count number of days of follow-up
gen total_fu = end_date - date("01/03/2020", "DMY")
** Update start and end of follow-up for cohort
* Identify prescriptions that start prior to March 2020
gen start_prior = (start < date("01/03/2021", "DMY"))
replace start = date("01/03/2021", "DMY") if start_prior==1
* Identify first prescriptions that start after March 2021
bys patient_id (start): gen start_after = 1 + (start > date("01/03/2021", "DMY") & _n==1)
tab start_after
expand start_after
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab start_after new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace stop = start if new_record==1
replace start = date("01/03/2021", "DMY") if new_record==1
drop new_record start_after
tab end_date_flag if start==date("01/03/2021", "DMY"), m
* Identify rows that end after study end date for person
gen end_after = (stop > end_date )
gen end_before = (start > end_date)
tab end_after
tab end_after end_date_flag
tab end_before
* Drop whole rows that are after end_date
drop if end_before
* Check years of stop dates
gen yr_stop = year(stop)
tab yr_stop end_after
* Check those flagged as ending after are last row only
bys patient_id (start): gen last = _n==_N
tab last end_after
* Check how many records end after end date - should just be one
bys patient_id: egen tot_end_after = total(end_after)
replace stop = end_date if end_after==1 & end_before==0
* Identify prescriptions that end prior to end of follow-up
bys patient_id (start): gen stop_prior = 1 + (stop < end_date & _n==_N)
tab stop_prior
expand stop_prior
* Flag new rows
bys patient_id start: gen new_record = (_n==2)
tab stop_prior new_record
* Updating new rows
sort patient_id start
replace udca=0 if new_record==1
replace start = stop if new_record==1
replace stop = end_date if new_record==1
drop new_record stop_prior
di "number where start after stop date"
count if start > stop
di "Number where start equals stop"
count if start==stop
drop if start==stop
* Check that time covered by prescriptions equals total follow-up
gen time = stop - start
sum time, d
bys patient_id: egen total_time = total(time)
gen total_time_unequal = total_time!=total_fu
tab total_time_unequal, m
save ./output/time_varying_udca_all_vars_vacc
* Drop unnecessary variables
keep patient_id start stop udca
save ./output/time_varying_udca_vacc