Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: count (Stata 8)


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: count (Stata 8)
Date   Tue, 10 May 2005 22:16:10 +0100

-egen- haters can spend a line interactively and 
save dozens and dozens off-stage: 

bysort year : gen cases = sum(dflag)
by year : replace cases = cases[_N] 
by year: gen n = _N 

Also, in Stata 9, -egen, sum()- still works but 
the preferred name is now -total()-. 

Nick 
n.j.cox@durham.ac.uk 

Heather Gold
 
> Thanks - this works perfectly to give both scalars and variables. I 
> appreciate your help!
> At 03:02 PM 5/10/2005, you wrote:
> >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)
> >}

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index