I have data on the presence of 21 side effects (SE), which was collected from 73 patients over 11,000 total visits during a 2 year time period.
I want to count the number of unique patients that have each SE. The same patient might have multiple SE during the study period, and might have the same SE over multiple visits. They might have multiple visits on the same day.
A subset of my data is attached. Var "ptid" uniquely identifies the patients, and var "ccenc" uniquely identifies the visits. SE are coded as 0=no 1=yes.
So far, I've tried (using n_sidefxloss, the first SE, as an example):
bysort ptid: egen se1 = count(n_sidefxloss)
bysort ptid ccenc: egen se1 = sum(n_sidefxloss)
bysort ptid ccenc: egen se1 = total(n_sidefxloss)
bysort ptid ccenc : egen se1 = max(n_sidefxloss)
total n_sidefxloss , over(ptid)
tab n_sidefxloss, generate(selossdummy)
collapse (max) se1count=selossdummy2, by(ptid)
If collapse is a good choice, is there a way to use a macro to sequentially collapse the 21 var and then merge them back into the original dataset?
foreach v of var n_sidefx* {
tab `v', g(`v'dummy)
collapse (max) `v'count=`v'dummy2, by (ptid)
save dotsewip_se_d, replace
merge 1:1 ptid using dotsewip_all
drop _merge
save dotsewip_all, replace
}