-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr_getlast_bmi_aurum.do
64 lines (50 loc) · 1.96 KB
/
pr_getlast_bmi_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_getlast_bmi_aurum
program define pr_getlast_bmi_aurum
syntax , clinicalfile(string) clinfilenum(string) index(string) runin(integer) bmi_cutoff(integer)
preserve
display in red "*******************Observation file number: 1*******************"
use "$QRiskCodelistdir\cr_codelist_qof_cod_aurum.dta", clear
keep if variable=="bmi"
merge 1:m medcodeid using "`clinicalfile'_1", keep(match) keepusing(patid obsdate value) 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.dta", clear
keep if variable=="bmi"
merge 1:m medcodeid using "`clinicalfile'_`n'", keep(match) keepusing(patid obsdate value) 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)
sort patid obsdate
by patid: replace bmi=. if _merge==3 & obsdate>`index' & _n==1
by patid: replace obsdate=. if _merge==3 & obsdate>`index' & _n==1
drop if obsdate>`index' & _merge==3 & obsdate!=.
by patid: replace bmi=. if _merge==3 & obsdate<`index'-365.25*`runin' & _n==_N
by patid: replace obsdate=. if _merge==3 & obsdate<`index'-365.25*`runin' & _n==_N
drop if obsdate<`index'-365.25*`runin' & _merge==3
gsort patid obsdate bmi
by patid: keep if _n==_N
rename obsdate bmidate
drop _merge
end