generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
102_udca_descriptives.do
147 lines (128 loc) · 7.4 KB
/
102_udca_descriptives.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
/*==============================================================================
DO FILE NAME: udca_descriptives.do
DATE: 25/04/2023
AUTHOR: R Costello
DESCRIPTION OF FILE: Produce descriptives of UDCA prescribing
==============================================================================*/
adopath + ./analysis/ado
cap mkdir ./output/tables
* Open a log file
cap log using ./logs/udca_descriptives.log, replace
tempfile tempfile
import delimited using ./output/input_pbc.csv
save `tempfile'
file open tablecontent using ./output/tables/udca_descriptives.txt, write text replace
* Loop through each dataset with different lengths of days of prescriptions
forvalues i=60(30)180 {
use ./output/time_varying_udca_all_vars_`i', clear
drop last
bys patient_id: gen last = _n==_N
bys patient_id (start): gen udca_bl_check = udca[1]
tab udca_count_bl udca_bl_check
file write tablecontent _tab ("Number of obervations") _tab ("Mean") _tab ("SD") _tab ("Median") _tab ("25th percentile") _tab ("75th percentile") _n
* summarise total number of prescriptions during follow-up
sum total_no_presc if last==1, d
file write tablecontent ("Number of prescriptions `i'") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
* Summarise follow-up
sum total_fu if last==1, d
file write tablecontent ("Total length of follow-up `i'") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
* Summarise proportion of time on udca
* First generate time for each row
*gen time = stop - start
* Generate total time on & off udca
* First calculate time for both on and off within one variable
bys patient_id udca: egen time_udca = total(time)
* From this generate variable with time off udca
bys patient_id: egen time_off_udca = max(time_udca) if udca==0
bys patient_id (time_off_udca): replace time_off_udca = time_off_udca[_n-1] if time_off_udca==. & time_off_udca[_n-1]!=.
* Update time_udca to only contain time on udca
replace time_udca=. if udca==0
bys patient_id: egen time_on_udca = max(time_udca)
* Check on and off equals total time of follow-up
count if time_on_udca + time_off_udca!=total_fu
sum time_on_udca if last==1, d
file write tablecontent ("Total time on UDCA `i'") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
sum time_off_udca if last==1, d
file write tablecontent ("Total time off UDCA `i'") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
bys patient_id: gen rows = _N
sum rows if last==1, d
file write tablecontent ("Number of rows `i'") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n _n
* Summarising switching
gen udca_bl = (udca_count_bl>=1 & udca_count_bl!=.)
gen yr_start = year(start)
sum yr_start
gen switch_`i'_i = udca!=udca_bl
bys patient_id: egen switch_`i' = max(switch_`i'_i)
bys patient_id: egen switch_`i'_total = total(switch_`i'_i)
gen switch_`i'_start = (switch_`i'==1 & udca_bl==0)
gen switch_`i'_stop = (switch_`i'==1 & udca_bl==1)
gen switch_`i'_multi = (switch_`i'_total>1 & switch_`i'_total!=.)
keep if last==1
tab switch_`i'
safecount if switch_`i'==1
file write tablecontent ("switching") _tab ("Number switched") _tab ("Percent switched") _tab ("Switched from unexposed") _tab ("Percent switched unexposed") _tab ("Switched from exposed") _tab ("Percent switched exposed") _n
file write tablecontent ("`i'") _tab %3.1f (r(N)) _tab
local percent = (r(N)/_N)*100
safecount if switch_`i'_start==1
file write tablecontent (`percent') _tab %3.1f (r(N)) _tab
local percent_stt = (r(N)/_N)*100
safecount if switch_`i'_stop==1
file write tablecontent (`percent_stt') _tab %3.1f (r(N)) _tab
local percent_stp = (r(N)/_N)*100
file write tablecontent (`percent_stp') _n _n
}
use ./output/time_varying_udca_overlap_all_vars_120, clear
drop last
bys patient_id: gen last = _n==_N
file write tablecontent _tab ("Number of obervations") _tab ("Mean") _tab ("SD") _tab ("Median") _tab ("25th percentile") _tab ("75th percentile") _n
* summarise total number of prescriptions during follow-up
sum total_no_presc if last==1, d
file write tablecontent ("Number of prescriptions overlap 120") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
* Summarise follow-up
sum total_fu if last==1, d
file write tablecontent ("Total length of follow-up overlap 120") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
* Summarise proportion of time on udca
* First generate time for each row
*gen time = stop - start
* Generate total time on & off udca
* First calculate time for both on and off within one variable
bys patient_id udca: egen time_udca = total(time)
* From this generate variable with time off udca
bys patient_id: egen time_off_udca = max(time_udca) if udca==0
bys patient_id (time_off_udca): replace time_off_udca = time_off_udca[_n-1] if time_off_udca==. & time_off_udca[_n-1]!=.
* Update time_udca to only contain time on udca
replace time_udca=. if udca==0
bys patient_id: egen time_on_udca = max(time_udca)
* Check on and off equals total time of follow-up
count if time_on_udca + time_off_udca!=total_fu
sum time_on_udca if last==1, d
file write tablecontent ("Total time on UDCA overlap 120") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
sum time_off_udca if last==1, d
file write tablecontent ("Total time off UDCA overlap 120") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n
bys patient_id: gen rows = _N
sum rows if last==1, d
file write tablecontent ("Number of rows overlap 120") _tab %3.1f (r(N)) _tab %3.1f (r(mean)) _tab %3.1f (r(sd)) _tab %3.1f (r(p50)) _tab %3.1f (r(p25)) _tab %3.1f (r(p75)) _n _n
* Summarising switching
gen udca_bl = (udca_count_bl>=1 & udca_count_bl!=.)
gen yr_start = year(start)
sum yr_start
gen switch_o_120_i = udca!=udca_bl
bys patient_id: egen switch_o_120 = max(switch_o_120_i)
bys patient_id: egen switch_o_120_total = total(switch_o_120_i)
gen switch_o_120_start = (switch_o_120==1 & udca_bl==0)
gen switch_o_120_stop = (switch_o_120==1 & udca_bl==1)
gen switch_o_120_multi = (switch_o_120_total>1 & switch_o_120_total!=.)
keep if last==1
tab switch_o_120
safecount if switch_o_120==1
file write tablecontent ("switching") _tab ("Number switched") _tab ("Percent switched") _tab ("Switched from unexposed") _tab ("Percent switched unexposed") _tab ("Switched from exposed") _tab ("Percent switched exposed") _n
file write tablecontent ("o_120") _tab %3.1f (r(N)) _tab
local percent = (r(N)/_N)*100
safecount if switch_o_120_start==1
file write tablecontent (`percent') _tab %3.1f (r(N)) _tab
local percent_stt = (r(N)/_N)*100
safecount if switch_o_120_stop==1
file write tablecontent (`percent_stt') _tab %3.1f (r(N)) _tab
local percent_stp = (r(N)/_N)*100
file write tablecontent (`percent_stp') _n _n
file close tablecontent