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]

Re: st: Computing difference of counts within groups


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: Computing difference of counts within groups
Date   Mon, 1 Nov 2010 11:09:20 -0500

On Nov 1, 2010, at 10:46 AM, Grealy, Patrick J wrote:
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


One way to replicate exactly what you have done is the following:

    egen fdiff = total((foreign*2)-1) if !mi(rep78), by(rep78)
    replace fdiff = abs(fdiff)


-- Phil

*
*   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