Statalist The Stata Listserver


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

Re: st: counting number of children in a household


From   David Bell <[email protected]>
To   [email protected]
Subject   Re: st: counting number of children in a household
Date   Tue, 28 Feb 2006 21:30:47 -0600

There are advantages to brevity. On the other hand, there are also advantages to clarity. When I was a programmer (self employed), I preferred Michael's brief approach. However, now that I work with research teams including students, I have found that Zamira's more verbose approach has its own distinct advantages. The logic, because it is broken down into smaller steps, is accessible to more members of the team and not just to the sophisticated.

Furthermore, for my own work, although I do use -gen-, for most commands and options I prefer to minimize abbreviation. So when I have to redo an analysis some months later, I find it easier to understand what I did the first time.

Dave Bell

On Feb 28, 2006, at 6:21 PM, Michael Blasnik wrote:


Your approach involves a lot of unnecessary work -- 5 lines of code with two egen calls -- when it can be done more quickly from first principles:

bysort hhid: gen no_childr=sum(age<19)
by hhid: replace no_childr=no_childr[_N]

Michael Blasnik
[email protected]

----- Original Message ----- From: "Zamira Simkins" <[email protected]>
To: <[email protected]>
Sent: Tuesday, February 28, 2006 7:01 PM
Subject: RE: st: counting number of children in a household


Someone asked how to count how many children are in a given household and
how to assign this number to each individual in the same household.

What I usually do is:

sort hhid
egen no_kids=count(id) if age<19, by(hhid)
/*this counts kids in every hh and places the sum in a row where an
individual is <=18yo, rows with adults will have a missing value because
they did not meet the if condition*/
egen no_childr=max(no_kids), by(hhid)
/*this assigns the total number of children to each individual within the
same hh*/
replace no_childr=0 if no_childr==.
drop no_kids
/*you do not need no_kids anymore, so drop it*/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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