Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: Computing difference of counts within groups
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: Computing difference of counts within groups
Date
Mon, 1 Nov 2010 16:14:04 +0000
No looping needed at all. This yields to -by-.
sysuse auto, clear
egen nforeign = total(foreign == 1), by(rep78)
egen ndomestic = total(foreign == 0), by(rep78)
gen diff = nforeign - nodomestic
etc.
-egen- here is optional, as
bysort rep78: gen nforeign = sum(foreign == 1)
by rep78: replace nforeign = nforeign[_N]
by rep78: gen ndomestic = sum(foreign == 0)
by rep78: replace ndomestic = ndomestic[_N]
gets you to the same place.
Nick
[email protected]
Grealy, Patrick J
I thought there was a simple way to do what I am attempting using the egen command but I have not gotten that to work right. Can someone make another suggestion? I am trying to check if the difference between two groups within another group exceeds a certain value. For example, using the auto dataset, within the five rep78 groups I would like to calculate the difference between Domestic and Foreign and check if it exceeds some limit, say 7.
This code below does what I want but I was hoping to find something simpler:
sysuse auto,clear
gen fdiff = .
foreach ix_rep78 of numlist 1/5 {
quietly count if foreign==0 & rep78==`ix_rep78'
local foreign0 `r(N)'
quietly count if foreign==1 & rep78==`ix_rep78'
local foreign1 `r(N)'
replace fdiff = abs(`foreign0' - `foreign1') if rep78==`ix_rep78'
}
tab rep78 foreign if fdiff > 7
*
* 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/