Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: use of count.


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: use of count.
Date   Thu, 18 Sep 2008 12:13:48 +0100

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
[email protected] 

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/



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