I'm on record as a big fan of -foreach- but this problem
doesn't seem to need a loop at all.
Nevertheless, you want scalars, although I am not clear why.
You can have it both ways by having variables and scalars too.
(The variables will come in useful for graphs and tables.)
egen cases = sum(dflag), by(year)
egen n = sum(1), by(year)
gen prevalence = cases / n
tabdisp year, c(prevalence)
levelsof year, local(Y)
foreach y of local Y {
su prev if year == `y', meanonly
scalar prev`y' = r(min)
}
Nick
[email protected]