Announcement

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

  • Generate household size size

    Hello,

    I am working with a longitudinal panel data for households between 2008 and 2011. It would be a sort of hierarchical structure where each year nests households (labeled with the variable hhid), and each household contains each household member (pid).

    I am having trouble generating a variable containing the household size for each year. I am able to create a variable that tells me the total number of individuals in the household, but if there are changes in the household composition, this variable does not take it into account. For example, I have a household that in 2008, 2009 and 2010 there are 3 individuals, but in 2011 there are 4, and the variable I create takes the value 4 for all years, instead of 3 for the first 3 years. How do I solve my problem? And if I want to specify the number of adults and the number of children, what do I do? (For this I have an age variable)


  • #2
    Below is an example.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(hhid pid year age)
    1 1 2008 17
    1 2 2008  5
    1 3 2008 36
    1 1 2009 18
    1 2 2009  6
    1 3 2009 37
    1 4 2009 60
    1 2 2010  7
    1 3 2010 38
    1 4 2010 61
    1 5 2010 23
    end
    
    bys hhid year: gen hhsize = _N
    bys hhid year: egen adult = total(age>=18 & !mi(age))
    bys hhid year: egen child = total(age<18)
    Code:
         +--------------------------------------------------+
         | hhid   pid   year   age   hhsize   adult   child |
         |--------------------------------------------------|
      1. |    1     1   2008    17        3       1       2 |
      2. |    1     2   2008     5        3       1       2 |
      3. |    1     3   2008    36        3       1       2 |
      4. |    1     1   2009    18        4       3       1 |
      5. |    1     2   2009     6        4       3       1 |
      6. |    1     3   2009    37        4       3       1 |
      7. |    1     4   2009    60        4       3       1 |
      8. |    1     2   2010     7        4       3       1 |
      9. |    1     3   2010    38        4       3       1 |
     10. |    1     4   2010    61        4       3       1 |
     11. |    1     5   2010    23        4       3       1 |
         +--------------------------------------------------+

    Comment


    • #3
      Thank you very much! It works perfectly

      Comment

      Working...
      X