I've been the code (see below) - but I need to copy and paste this for 20 times for different variables(see bold) eg Dvt, MI, heartfailure etc.
bys hospitalid2 date: egen new_var = max(dvt == 1)
by hospitalid2 date: egen all_missing = min(missing(dvt))
by hospitalid2 date: keep if _n == 1
replace new_var = . if all_missing == 1
drop dvt all_missing
rename new_var dvt
I have created a loop:
foreach i of dvt mi heartfailure {
bys hospitalid2 date: egen new_var = max(`i' == 1)
by hospitalid2 date: egen all_missing = min(missing(`i'))
by hospitalid2 date: keep if _n == 1
replace new_var = . if all_missing == 1
drop `i' all_missing
}
however stata says invalid syntax...
I am perhaps not writing it correctly, can anyone help?
bys hospitalid2 date: egen new_var = max(dvt == 1)
by hospitalid2 date: egen all_missing = min(missing(dvt))
by hospitalid2 date: keep if _n == 1
replace new_var = . if all_missing == 1
drop dvt all_missing
rename new_var dvt
I have created a loop:
foreach i of dvt mi heartfailure {
bys hospitalid2 date: egen new_var = max(`i' == 1)
by hospitalid2 date: egen all_missing = min(missing(`i'))
by hospitalid2 date: keep if _n == 1
replace new_var = . if all_missing == 1
drop `i' all_missing
}
however stata says invalid syntax...
I am perhaps not writing it correctly, can anyone help?
Comment