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

Re: st: count (Stata 8)

From   "Andres E. Hernando" <[email protected]>
To   [email protected]
Subject   Re: st: count (Stata 8)
Date   Tue, 10 May 2005 11:24:15 -0400


This would do it... but only for the last year:

. sort year
. by year: count if dxflag==1
. scalar cases = r(N)
. by year: count
. scalar total = r(N)
. scalar prev = cases/total
. di prev

basically r(N) stores only the last scalar reported so, eventually you'd need to put the above code within a forvalues/foreach cycle, maybe storing results in a vector.

But why to do it in this way? what's wrong with using a couple of egen and gen operators? I'm thinking:

. sort year
. by year: egen cac = count(year) if dxflag==1
. by year: egen cas = max(ct)
. drop cac
. by year: egen tot = count(year)
. gen prev = cas/tot
. by year: su prev


Quoting Heather Gold, who wrote:
-------Original Message-------

Dear Listers -

I know this must be simple, but something eludes me.
I have a data set of subjects (cases and non-cases) by year and am trying to calculate a prevalence per year, ie, [cases/(cases+noncases)].
ie, data look like this:
id year dxflag
1 1995 1
1 1996 0
1 1997 0
2 1995 0
2 1996 1

I found that I can write
by year: count if dxflag==1
by year: count

and these return number of cases in the first line, and count of total population in the second line, just as I want.
But what I need are these values as scalars, so I can create a prevalence per year. In the Stata 8 help/manual it says that -count- saves the scalar of number of observations in r(N), but I can't seem to get this number to appear. Do I actually have to write a program to calculate this? And how does the "by year" part fit in...?

Thanks for your help,

* For searches and help try:
--------End of Message--------

There are 10 kinds of people in the world, those who know binary and those who don't.

* For searches and help try:

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