Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: RE: Computing difference of counts within groups

From   "Grealy, Patrick J" <>
To   "" <>
Subject   st: RE: RE: Computing difference of counts within groups
Date   Mon, 1 Nov 2010 12:04:10 -0500

Thanks to both Nick Cox and Phil Schumm for their solutions using the total() function with egen. - Pat G.

-----Original Message-----
From: [] On Behalf Of Nick Cox
Sent: Monday, November 01, 2010 11:14 AM
To: ''
Subject: st: RE: Computing difference of counts within groups

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 


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


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:

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index