generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
102_udca_descriptives.do
83 lines (71 loc) · 4.1 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
/*==============================================================================
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
file write tablecontent _tab ("Number of obervations") _tab ("Mean") _tab ("SD") _tab ("Median") _tab ("25th percentile") _tab ("75th percentile") _n
forvalues i=60(30)180 {
use ./output/time_varying_udca_all_vars_`i', clear
drop last
bys patient_id: gen last = _n==_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_2020_`i'_i = udca!=udca_bl & yr_start==2020
bys patient_id: egen switch_2020_`i' = max(switch_2020_`i'_i)
bys patient_id: egen switch_2020_`i'_total = total(switch_2020_`i'_i)
gen switch_2020_`i'_start = (switch_2020_`i'==1 & udca_bl==0)
gen switch_2020_`i'_stop = (switch_2020_`i'==1 & udca_bl==1)
gen switch_2020_`i'_multi = (switch_2020_`i'_total>1 & switch_2020_`i'_total!=.)
keep if last==1
tab switch_2020_`i'
safecount if switch_2020_`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 ("2020 `i'") _tab %3.1f (r(N)) _tab
local percent = (r(N)/_N)*100
safecount if switch_2020_`i'_start==1
file write tablecontent (`percent') _tab %3.1f (r(N)) _tab
local percent_stt = (r(N)/_N)*100
safecount if switch_2020_`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
}
file close tablecontent