Bookmark and Share

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: Computing difference of counts within groups


From   "Grealy, Patrick J" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: Computing difference of counts within groups
Date   Mon, 1 Nov 2010 10:46:40 -0500

Hi,
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

Thanks,
Pat G.

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index