-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_getlast_sbp_aurum.do
65 lines (46 loc) · 2.04 KB
/
pr_getlast_sbp_aurum.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
cap prog drop pr_getlast_sbp_aurum
program define pr_getlast_sbp_aurum
syntax , clinicalfile(string) clinfilenum(string) index(string) runin(integer)
preserve
display in red "*******************Observation file number: 1*******************"
use "$QRiskCodelistdir\cr_codelist_qof_cod_aurum.dta", clear
keep if variable=="sbp"
merge 1:m medcodeid using "`clinicalfile'_1", keep(match) keepusing(patid obsdate value) nogen
tempfile sbprecs
save `sbprecs', replace
forvalues n=2/`clinfilenum' {
display in red "*******************Observation file number: `n'*******************"
use "$QRiskCodelistdir\cr_codelist_qof_cod_aurum.dta", clear
keep if variable=="sbp"
merge 1:m medcodeid using "`clinicalfile'_`n'", keep(match) keepusing(patid obsdate value) nogen
append using "`sbprecs'"
save `sbprecs', replace
}
rename value sbp
destring sbp, replace
drop if obsdate==.
drop if sbp==.
drop if sbp<=1
*keep lowest sbp if more than one measurement on same day
keep patid obsdate sbp
bysort patid obsdate (sbp): keep if _n==1
save `sbprecs', replace
restore
merge 1:m patid using `sbprecs', keep(match master)
rename obsdate bpdate
sort patid bpdate
*if first sbp record is after index, sets sbp and eventdate to missing for this record
by patid: replace sbp=. if _merge==3 & bpdate>`index' & _n==1
by patid: replace bpdate=. if _merge==3 & bpdate>`index' & _n==1
*drop remaining eventdates that are after the indexdate
drop if bpdate>`index' & _merge==3 & bpdate!=.
*if last sbp is before the run in period, sets sbp and eventdate to missing for this record
by patid: replace sbp=. if _merge==3 & bpdate<`index'-365.25*`runin' & _n==_N
by patid: replace bpdate=. if _merge==3 & bpdate<`index'-365.25*`runin' & _n==_N
*drop remaining eventdates that are before the run in period
drop if _merge==3 & bpdate<`index'-365.25*`runin'
*keeps last event of sbp records that are within the specified period (i.e. event nearest to index)
gsort patid -bpdate sbp
duplicates drop patid if _merge==3 , force
drop _merge
end