Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Count number of children in a household

    Dear all,

    I´m working with survey data and i need to compute a variable that counts the number of children younger than 3 in a household. I found the following command for doing it but i do not understand what it does in the second step when substracts the age from the variable. Could someone explain it?
    egen nchild = total(age <= 3), by(household) replace nchild = nchild - (age <= 3) Thanks!

  • #2
    These are two separate lines of code

    Code:
    egen nchild = total(age <= 3), by(household)
    replace nchild = nchild - (age <= 3) 
    The first line counts up the number of children with age <=3, and presumably you follow that bit. This expression

    (age<=3)

    evaluates to 1 when the age is 3 or less and evaluates to 0 otherwise, so what the second line does is subtract 1 if the child is 3 or younger. What I don't know is why someone would want to do that. Perhaps what is wanted is the number of children *other than oneself* that is <= 3? Because that's what -nchild- contains; for children over 3, it is just the number of children under 3, but if the child is under 3, then 1 is subtracted to produce the number under 3 other than that child.

    Comment


    • #3
      This sounds like something I might have suggested for the problem sketched in the last few sentences, i.e. for each child 3 or younger, how many other such children are there in the same household?

      As Jeph implies, the first line of code is enough for the stated problem, just counting the number.

      But what is being manipulated is an expression

      Code:
       
      age <= 3
      and it's vital to understand that this evaluates as 1 or 0. It's not the age itself that is being used in calculations. See e.g. http://www.stata.com/support/faqs/da...lse/index.html

      Comment


      • #4
        Thanks a lot! I get it now.

        Comment

        Working...
        X