-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_get_sbpupdates_aurum.do
59 lines (42 loc) · 1.6 KB
/
pr_get_sbpupdates_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
cap prog drop pr_get_sbpupdates_aurum
program define pr_get_sbpupdates_aurum
syntax , clinicalfile(string) clinfilenum(string) begin(string) end(string) runin(integer)
preserve
display in red "*******************Observation file number: 1*******************"
use "$QRiskCodelistdir\cr_codelist_qof_cod_aurum", 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", 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
drop if (bpdate<`begin'-365.25*`runin' | bpdate>`end') & _merge==3
gsort patid -bpdate sbp
duplicates drop patid if bpdate<`begin', force
sort patid bpdate
by patid: drop if sbp[_n-1]==sbp & _n>1
expand 2 if bpdate>`begin' & bpdate!=. & patid[_n]!=patid[_n-1]
sort patid bpdate
by patid: replace sbp=. if _n==1 & bpdate>`begin'
by patid: replace bpdate=`begin' if _n==1 & bpdate>`begin'
drop _merge
end