RE: st: use of count.

 From "Nick Cox" To Subject RE: st: use of count. Date Thu, 18 Sep 2008 12:19:11 +0100

```-egen, sum()- is now undocumented, so for the clarity the second line
should refer to -total(tag)-, not -sum(tag)-.

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 18 September 2008 12:14
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: use of count.

I support Johannes' approach here. Note that

egen tag = tag(cnum), by(year state)
egen ncand = sum(tag), by(year state)

is nearly equivalent to his code.

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

Johannes Geyer

You could try the following:

bysort year state cnum: gen     x = _n == 1
by     year state     : replace x = sum(x)
by     year state     : replace x = x[_N]

a solution that is described here:

http://www.stata.com/support/faqs/data/distinct.html

Ashim Kapoor

> I have the variables year state cnum candidat.
>
> Year state and candidate have the usual meaning.
>
> cnum is the consituency within a state.
>
> So in one year I may have 5 states and within that I may have 10
> constituencies and within those  I may have say 4 candidates each.
>
> I want to compute the number of constituencies in each state.
>
> I try to do this by : -
>
> gen noofct=0
>
> bysort year state cnum : count and now I want to store r(N) in noofct
> variable. So the noofct variable will have the same value in any one
> consituency. This is what I am not able to do.
>
> Can someone tell me how to do this ?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```