-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_getsbpupdates.do
67 lines (47 loc) · 1.55 KB
/
pr_getsbpupdates.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
cap prog drop pr_getsbpupdates
program define pr_getsbpupdates
syntax , clinicalfile(string) additionalfile(string) begin(string) end(string) runin(integer) [limitlow(string) limithigh(string)] [wide]
if "`limitlow'"=="" {
local limitlow "0"
}
if "`limithigh'"=="" {
local limithigh "."
}
preserve
use "$QRiskCodelistdir\cr_codelist_qof_cod", clear
keep if variable=="sbp"
merge 1:m medcode using `clinicalfile' , keep(match) nogen
replace adid = -_n if adid==0
rename value sbp
merge 1:1 patid adid using `additionalfile', keep(match master)
replace sbp=data2 if data2!=.
drop if eventdate == .
drop if sbp==.
drop if sbp==1
drop if sbp >`limithigh'
drop if sbp <`limitlow'
keep patid eventdate sbp
*keep lowest sbp if more than one measurement on same day
keep patid eventdate sbp
duplicates tag patid eventdate, gen(dup)
bysort patid eventdate (sbp): keep if _n==1
tempfile sbprecs
save `sbprecs' , replace
restore
merge 1:m patid using `sbprecs' , keep(match master)
rename eventdate bpdate
drop if (bpdate<`begin'-365.25*`runin' | bpdate>`end') & _merge==3
gsort patid -bpdate
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
if "`wide'"=="wide" {
by patid: gen sbp_rec_num=_n
reshape wide sbp bpdate, i(patid) j(sbp_rec_num)
}
end