-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_get_bmiupdates_aurum.do
64 lines (49 loc) · 1.88 KB
/
pr_get_bmiupdates_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
cap prog drop pr_get_bmiupdates_aurum
program define pr_get_bmiupdates_aurum
syntax , clinicalfile(string) clinfilenum(string) begin(string) end(string) runin(integer) bmi_cutoff(integer)
preserve
display in red "*******************Observation file number: 1*******************"
use "$QRiskCodelistdir\cr_codelist_qof_cod_aurum", clear
keep if variable=="bmi"
merge 1:m medcodeid using "`clinicalfile'_1", keep(match) keepusing(patid obsdate value numunitid) nogen
drop if obsdate==.
tempfile bmirecs
save `bmirecs', replace
forvalues n=2/`clinfilenum' {
display in red "*******************Observation file number: `n'*******************"
use "$QRiskCodelistdir\cr_codelist_qof_cod_aurum", clear
keep if variable=="bmi"
merge 1:m medcodeid using "`clinicalfile'_`n'", keep(match) keepusing(patid obsdate value numunitid) nogen
drop if obsdate==.
append using "`bmirecs'"
save `bmirecs', replace
}
rename value bmi
destring bmi, replace
replace bmi=. if bmi>`bmi_cutoff'
replace bmi=. if bmi<10
drop if bmi==.
*use average bmi if more than one measurement on same day and <= 5kg/m2 difference
duplicates tag patid obsdate, gen(dup)
sort patid obsdate
by patid obsdate: egen minbmi = min(bmi)
by patid obsdate: egen maxbmi = max(bmi)
by patid obsdate: egen avbmi = mean(bmi)
gen diff = maxbmi - minbmi
drop if diff > 5
replace bmi = avbmi if dup > 0
keep patid obsdate bmi
save `bmirecs', replace
restore
merge 1:m patid using `bmirecs', keep(match master)
rename obsdate bmidate
drop if (bmidate<`begin'-365.25*`runin' | bmidate>`end') & _merge==3
gsort patid -bmidate
duplicates drop patid if bmidate<=`begin', force
sort patid bmidate
expand 2 if bmidate>`begin' & bmidate!=. & patid[_n]!=patid[_n-1]
sort patid bmidate
by patid: replace bmi=. if _n==1 & bmidate>`begin'
by patid: replace bmidate=`begin' if _n==1 & bmidate>`begin'
drop _merge
end