generated from opensafely/research-template
/
an_deaths_table1.do
633 lines (448 loc) · 16.5 KB
/
an_deaths_table1.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
********************************************************************************
*
* Do-file: an_deaths_table1.do
*
* Project: SGTF CFR
*
* Programmed by: Daniel Grint
* Adapted from household classification (Kevin)
*
* Data used: output/cr_analysis_new.dta
*
* Data created:
*
* Other output: Table 1 Summary of deaths
*
********************************************************************************
*
* Purpose: This do-file creates table 1 for patients who die
*
********************************************************************************
* Open a log file
cap log close
log using ./logs/an_deaths_table1, replace t
clear
use ./output/cr_analysis_new.dta
********************************************************************************
* PROGRAMS TO AUTOMATE TABULATIONS
*
********************************************************************************
* All below code from K Baskharan
* Generic code to output one row of table
cap prog drop generaterow
program define generaterow
syntax, variable(varname) condition(string)
cou
local overalldenom=r(N)
sum `variable' if `variable' `condition'
**K Wing additional code to aoutput variable category labels**
local level=substr("`condition'",3,.)
local lab: label `variable'Lab `level'
file write tablecontent (" `lab'") _tab
/*this is the overall column*/
cou if `variable' `condition'
local rowdenom = r(N)
local colpct = 100*(r(N)/`overalldenom')
file write tablecontent %9.0gc (`rowdenom') (" (") %3.1f (`colpct') (")") _tab
/*this loops through groups*/
forvalues i=0/1{
cou if sgtf == `i'
local rowdenom = r(N)
cou if sgtf == `i' & `variable' `condition'
local pct = 100*(r(N)/`rowdenom')
file write tablecontent %9.0gc (r(N)) (" (") %3.1f (`pct') (")") _tab
}
file write tablecontent _n
end
* Output one row of table for co-morbidities and meds
* This puts it all on the same row, is rohini's edit
cap prog drop generaterow2
program define generaterow2
syntax, variable(varname) condition(string)
cou
local overalldenom=r(N)5
cou if `variable' `condition'
local rowdenom = r(N)
local colpct = 100*(r(N)/`overalldenom')
file write tablecontent %9.0gc (`rowdenom') (" (") %3.1f (`colpct') (")") _tab
forvalues i=0/1{
cou if sgtf == `i'
local rowdenom = r(N)
cou if sgtf == `i' & `variable' `condition'
local pct = 100*(r(N)/`rowdenom')
file write tablecontent %9.0gc (r(N)) (" (") %3.1f (`pct') (")") _tab
}
file write tablecontent _n
end
********************************************************************************
/* Explanatory Notes
defines a program (SAS macro/R function equivalent), generate row
the syntax row specifies two inputs for the program:
a VARNAME which is your variable
a CONDITION which is a string of some condition you impose
the program counts if variable and condition and returns the counts
column percentages are then automatically generated
this is then written to the text file 'tablecontent'
the number followed by space, brackets, formatted pct, end bracket and then tab
the format %3.1f specifies length of 3, followed by 1 dp.
*/
********************************************************************************
* Generic code to output one section (varible) within table (calls above)
cap prog drop tabulatevariable
prog define tabulatevariable
syntax, variable(varname) min(real) max(real) [missing]
local lab: variable label `variable'
file write tablecontent ("`lab'") _n
forvalues varlevel = `min'/`max'{
generaterow, variable(`variable') condition("==`varlevel'")
}
if "`missing'"!="" generaterow, variable(`variable') condition("== 12")
end
********************************************************************************
/* Explanatory Notes
defines program tabulate variable
syntax is :
- a VARNAME which you stick in variable
- a numeric minimum
- a numeric maximum
- optional missing option, default value is .
forvalues lowest to highest of the variable, manually set for each var
run the generate row program for the level of the variable
if there is a missing specified, then run the generate row for missing vals
*/
********************************************************************************
* Generic code to qui summarize a continous variable
cap prog drop summarizevariable
prog define summarizevariable
syntax, variable(varname)
local lab: variable label `variable'
file write tablecontent ("`lab'") _n
qui summarize `variable', d
file write tablecontent ("Mean (SD)") _tab
file write tablecontent %3.1f (r(mean)) (" (") %3.1f (r(sd)) (")") _tab
forvalues i=0/1{
qui summarize `variable' if sgtf == `i', d
file write tablecontent %3.1f (r(mean)) (" (") %3.1f (r(sd)) (")") _tab
}
file write tablecontent _n
qui summarize `variable', d
file write tablecontent ("Median (IQR)") _tab
file write tablecontent %3.1f (r(p50)) (" (") %3.1f (r(p25)) ("-") %3.1f (r(p75)) (")") _tab
forvalues i=0/1{
qui summarize `variable' if sgtf == `i', d
file write tablecontent %3.1f (r(p50)) (" (") %3.1f (r(p25)) ("-") %3.1f (r(p75)) (")") _tab
}
file write tablecontent _n
end
********************************************************************************
* INVOKE PROGRAMS FOR TABLE 1
*
********************************************************************************
* DROP MISSING UTLA
noi di "DROPPING MISSING UTLA DATA"
drop if utla_group==""
* DROP IF NO DATA ON SGTF
noi di "DROPPING NO SGTF DATA"
drop if has_sgtf==0
noi di "SUBSETTING ON COX POPULATION"
keep if cox_pop==1
noi di "SUBSETTING ON DEATHS"
keep if cox_death == 1
noi di "SUBSETTING ON HOSPITAL ADMISSION"
keep if end_hosp_test == 1
*Set up output file
cap file close tablecontent
file open tablecontent using ./output/table1_deaths1.txt, write text replace
file write tablecontent ("Table 1: Demographic and Clinical Characteristics") _n
file write tablecontent _tab ("Total") _tab ///
("non-VOC") _tab ///
("VOC") _n
* DEMOGRAPHICS (more than one level, potentially missing)
/*reminder of variables:
patient_id age ageCat hh_id hh_size hh_composition case_date case eth5 eth16 ethnicity_16 indexdate sex bmicat smoke imd region comorb_Neuro comorb_Immunosuppression shielding chronic_respiratory_disease chronic_cardiac_disease diabetes chronic_liver_disease cancer egfr_cat hypertension smoke_nomiss rural_urban
*/
*HOSPTIAL ADMISSION
label define end_hosp_testLab 1 "N"
label values end_hosp_test end_hosp_testLab
tabulatevariable, variable(end_hosp_test) min(0) max(1)
file write tablecontent _n
*ICU ADMISSION
tabulatevariable, variable(end_icu_test) min(0) max(1)
file write tablecontent _n
*DIED
tabulatevariable, variable(cox_death) min(0) max(1)
file write tablecontent _n
*DEATH|HOSP
tabulatevariable, variable(end_death_hosp) min(0) max(1)
file write tablecontent _n
*DISCHARGE
tabulatevariable, variable(hosp_discharge) min(0) max(1)
file write tablecontent _n
*TIME TO DEATH
summarizevariable, variable(cox_time_d)
file write tablecontent _n
*FOLLOW-UP TIME
summarizevariable, variable(cox_time)
file write tablecontent _n
*EPI WEEK
tabulatevariable, variable(start_week) min(1) max(8)
file write tablecontent _n
*SEX
tabulatevariable, variable(male) min(0) max(1)
file write tablecontent _n
*AGE
summarizevariable, variable(age)
file write tablecontent _n
tabulatevariable, variable(agegroup) min(0) max(7)
file write tablecontent _n
*ETHNICITY
tabulatevariable, variable(eth5) min(1) max(6)
file write tablecontent _n
*BMI
tabulatevariable, variable(obese4cat) min(1) max(4)
file write tablecontent _n
*SMOKING
tabulatevariable, variable(smoke_nomiss) min(1) max(3)
file write tablecontent _n
*COMORBIDITIES (3 CATEGORIES)
tabulatevariable, variable(comorb_cat) min(0) max(2)
file write tablecontent _n
*IMD
tabulatevariable, variable(imd) min(1) max(5)
file write tablecontent _n
*HOUSEHOLD SIZE
replace hh_total_cat = 5 if hh_total_cat==.
label define hh_total_catLab 5 "Missing", modify
tabulatevariable, variable(hh_total_cat) min(1) max(5)
file write tablecontent _n
*CARE HOME
tabulatevariable, variable(home_bin) min(0) max(1)
file write tablecontent _n
*REGION
tabulatevariable, variable(region) min(0) max(8)
file write tablecontent _n
*RURAL URBAN (five categories)
replace rural_urban5 = 6 if rural_urban5==.
label define rural_urban5Lab 6 "Missing", modify
tabulatevariable, variable(rural_urban5) min(1) max(6)
file write tablecontent _n
file write tablecontent _n _n
file close tablecontent
clear
insheet using ./output/table1_deaths1.txt, clear
export excel using ./output/table1_deaths1.xlsx, replace
*******************************************************************
** NO HOSPITAL ADMISSION
*******************************************************************
clear
use ./output/cr_analysis_new.dta
********************************************************************************
* PROGRAMS TO AUTOMATE TABULATIONS
*
********************************************************************************
* All below code from K Baskharan
* Generic code to output one row of table
cap prog drop generaterow
program define generaterow
syntax, variable(varname) condition(string)
cou
local overalldenom=r(N)
sum `variable' if `variable' `condition'
**K Wing additional code to aoutput variable category labels**
local level=substr("`condition'",3,.)
local lab: label `variable'Lab `level'
file write tablecontent (" `lab'") _tab
/*this is the overall column*/
cou if `variable' `condition'
local rowdenom = r(N)
local colpct = 100*(r(N)/`overalldenom')
file write tablecontent %9.0gc (`rowdenom') (" (") %3.1f (`colpct') (")") _tab
/*this loops through groups*/
forvalues i=0/1{
cou if sgtf == `i'
local rowdenom = r(N)
cou if sgtf == `i' & `variable' `condition'
local pct = 100*(r(N)/`rowdenom')
file write tablecontent %9.0gc (r(N)) (" (") %3.1f (`pct') (")") _tab
}
file write tablecontent _n
end
* Output one row of table for co-morbidities and meds
* This puts it all on the same row, is rohini's edit
cap prog drop generaterow2
program define generaterow2
syntax, variable(varname) condition(string)
cou
local overalldenom=r(N)5
cou if `variable' `condition'
local rowdenom = r(N)
local colpct = 100*(r(N)/`overalldenom')
file write tablecontent %9.0gc (`rowdenom') (" (") %3.1f (`colpct') (")") _tab
forvalues i=0/1{
cou if sgtf == `i'
local rowdenom = r(N)
cou if sgtf == `i' & `variable' `condition'
local pct = 100*(r(N)/`rowdenom')
file write tablecontent %9.0gc (r(N)) (" (") %3.1f (`pct') (")") _tab
}
file write tablecontent _n
end
********************************************************************************
/* Explanatory Notes
defines a program (SAS macro/R function equivalent), generate row
the syntax row specifies two inputs for the program:
a VARNAME which is your variable
a CONDITION which is a string of some condition you impose
the program counts if variable and condition and returns the counts
column percentages are then automatically generated
this is then written to the text file 'tablecontent'
the number followed by space, brackets, formatted pct, end bracket and then tab
the format %3.1f specifies length of 3, followed by 1 dp.
*/
********************************************************************************
* Generic code to output one section (varible) within table (calls above)
cap prog drop tabulatevariable
prog define tabulatevariable
syntax, variable(varname) min(real) max(real) [missing]
local lab: variable label `variable'
file write tablecontent ("`lab'") _n
forvalues varlevel = `min'/`max'{
generaterow, variable(`variable') condition("==`varlevel'")
}
if "`missing'"!="" generaterow, variable(`variable') condition("== 12")
end
********************************************************************************
/* Explanatory Notes
defines program tabulate variable
syntax is :
- a VARNAME which you stick in variable
- a numeric minimum
- a numeric maximum
- optional missing option, default value is .
forvalues lowest to highest of the variable, manually set for each var
run the generate row program for the level of the variable
if there is a missing specified, then run the generate row for missing vals
*/
********************************************************************************
* Generic code to qui summarize a continous variable
cap prog drop summarizevariable
prog define summarizevariable
syntax, variable(varname)
local lab: variable label `variable'
file write tablecontent ("`lab'") _n
qui summarize `variable', d
file write tablecontent ("Mean (SD)") _tab
file write tablecontent %3.1f (r(mean)) (" (") %3.1f (r(sd)) (")") _tab
forvalues i=0/1{
qui summarize `variable' if sgtf == `i', d
file write tablecontent %3.1f (r(mean)) (" (") %3.1f (r(sd)) (")") _tab
}
file write tablecontent _n
qui summarize `variable', d
file write tablecontent ("Median (IQR)") _tab
file write tablecontent %3.1f (r(p50)) (" (") %3.1f (r(p25)) ("-") %3.1f (r(p75)) (")") _tab
forvalues i=0/1{
qui summarize `variable' if sgtf == `i', d
file write tablecontent %3.1f (r(p50)) (" (") %3.1f (r(p25)) ("-") %3.1f (r(p75)) (")") _tab
}
file write tablecontent _n
end
********************************************************************************
* INVOKE PROGRAMS FOR TABLE 1
*
********************************************************************************
* DROP MISSING UTLA
noi di "DROPPING MISSING UTLA DATA"
drop if utla_group==""
* DROP IF NO DATA ON SGTF
noi di "DROPPING NO SGTF DATA"
drop if has_sgtf==0
noi di "SUBSETTING ON COX POPULATION"
keep if cox_pop==1
noi di "SUBSETTING ON DEATHS"
keep if cox_death == 1
noi di "SUBSETTING ON NO HOSPITAL ADMISSION"
keep if end_hosp_test == 0
*Set up output file
cap file close tablecontent
file open tablecontent using ./output/table1_deaths0.txt, write text replace
file write tablecontent ("Table 1: Demographic and Clinical Characteristics") _n
file write tablecontent _tab ("Total") _tab ///
("non-VOC") _tab ///
("VOC") _n
* DEMOGRAPHICS (more than one level, potentially missing)
/*reminder of variables:
patient_id age ageCat hh_id hh_size hh_composition case_date case eth5 eth16 ethnicity_16 indexdate sex bmicat smoke imd region comorb_Neuro comorb_Immunosuppression shielding chronic_respiratory_disease chronic_cardiac_disease diabetes chronic_liver_disease cancer egfr_cat hypertension smoke_nomiss rural_urban
*/
*HOSPTIAL ADMISSION
label define end_hosp_testLab 1 "N"
label values end_hosp_test end_hosp_testLab
tabulatevariable, variable(end_hosp_test) min(0) max(1)
file write tablecontent _n
*ICU ADMISSION
tabulatevariable, variable(end_icu_test) min(0) max(1)
file write tablecontent _n
*DIED
tabulatevariable, variable(cox_death) min(0) max(1)
file write tablecontent _n
*DEATH|HOSP
tabulatevariable, variable(end_death_hosp) min(0) max(1)
file write tablecontent _n
*DISCHARGE
tabulatevariable, variable(hosp_discharge) min(0) max(1)
file write tablecontent _n
*TIME TO DEATH
summarizevariable, variable(cox_time_d)
file write tablecontent _n
*FOLLOW-UP TIME
summarizevariable, variable(cox_time)
file write tablecontent _n
*EPI WEEK
tabulatevariable, variable(start_week) min(1) max(8)
file write tablecontent _n
*SEX
tabulatevariable, variable(male) min(0) max(1)
file write tablecontent _n
*AGE
summarizevariable, variable(age)
file write tablecontent _n
tabulatevariable, variable(agegroup) min(0) max(7)
file write tablecontent _n
*ETHNICITY
tabulatevariable, variable(eth5) min(1) max(6)
file write tablecontent _n
*BMI
tabulatevariable, variable(obese4cat) min(1) max(4)
file write tablecontent _n
*SMOKING
tabulatevariable, variable(smoke_nomiss) min(1) max(3)
file write tablecontent _n
*COMORBIDITIES (3 CATEGORIES)
tabulatevariable, variable(comorb_cat) min(0) max(2)
file write tablecontent _n
*IMD
tabulatevariable, variable(imd) min(1) max(5)
file write tablecontent _n
*HOUSEHOLD SIZE
replace hh_total_cat = 5 if hh_total_cat==.
label define hh_total_catLab 5 "Missing", modify
tabulatevariable, variable(hh_total_cat) min(1) max(5)
file write tablecontent _n
*CARE HOME
tabulatevariable, variable(home_bin) min(0) max(1)
file write tablecontent _n
*REGION
tabulatevariable, variable(region) min(0) max(8)
file write tablecontent _n
*RURAL URBAN (five categories)
replace rural_urban5 = 6 if rural_urban5==.
label define rural_urban5Lab 6 "Missing", modify
tabulatevariable, variable(rural_urban5) min(1) max(6)
file write tablecontent _n
file write tablecontent _n _n
file close tablecontent
clear
insheet using ./output/table1_deaths0.txt, clear
export excel using ./output/table1_deaths0.xlsx, replace
* Close log file
log close