Announcement

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

  • create dummy variables for children education

    Hi

    I want to create four separate dummy variables at household level:
    1. No children between 5 and 16 years old in the household
    2. All children between 5 and 16 years old are attending school
    3. Only some children between 5 and 16 years old are attending school
    4. None of the children between 5 and 16 years old are attending school

    Following is the data on age and education of each household member.

    Code:
    clear
    
    input double hhid float age float yrsedu
    
    101000 50 0
    101000 41 0
    101000 14 7
    101000 17 0
    101000 8  0
    102000 50 0
    102000 42 0
    102000 14 7
    102000 14 5
    102000 18 8
    103000 4  0
    103000 20 8
    103000 28 8
    104000 8  0
    104000 15 0
    104000 29 10
    104000 30 10
    end
    
    label var yrsedu "Years of Education"
    What I want is to have, for first variable, '1' if the household has no children between 5 and 16 years of age and '0' otherwise, for second variable, '1' if all children between age of 5 and 16 are attending school and '0' othewise and the same for variable 3 and 4. I have used the following code for variable 1, but it puts either '0' or '1' for each hh member who meets the condition. What I want is to have '0' or '1' for the whole household if any one of the member meets the condition.

    Code:
     gen all5to16_sch=0
     by hhid: replace all5to16_sch=1 if inrange(age, 5,16) & years_edu !=0
    Thanks

  • #2
    Dear Statalisters

    I am looking forward to your guidance on my query.
    Thanks in anticipation.

    Comment


    • #3
      This may help

      Code:
      clear
      
      input double hhid float age float yrsedu
      
      101000 50 0
      101000 41 0
      101000 14 7
      101000 17 0
      101000 8  0
      102000 50 0
      102000 42 0
      102000 14 7
      102000 14 5
      102000 18 8
      103000 4  0
      103000 20 8
      103000 28 8
      104000 8  0
      104000 15 0
      104000 29 10
      104000 30 10
      end
      
      label var yrsedu "Years of Education"
      
      egen children = total(inrange(age, 5, 16)), by(hhid)
      gen no_children = children == 0 
      
      egen school = total(inrange(age, 5, 16) & (yrsedu > 0)), by(hhid) 
      
      gen all_at_school = children == school
      gen some_at_school = inrange(school, 1, children - 1)
      gen none_at_school = (children > 0) & (school == 0) 
      
      list, sepby(hhid) 
      
           +---------------------------------------------------------------------------------------+
           |   hhid   age   yrsedu   children   no_chi~n   school   all_at~l   some_a~l   none_a~l |
           |---------------------------------------------------------------------------------------|
        1. | 101000    50        0          2          0        1          0          1          0 |
        2. | 101000    41        0          2          0        1          0          1          0 |
        3. | 101000    14        7          2          0        1          0          1          0 |
        4. | 101000    17        0          2          0        1          0          1          0 |
        5. | 101000     8        0          2          0        1          0          1          0 |
           |---------------------------------------------------------------------------------------|
        6. | 102000    50        0          2          0        2          1          0          0 |
        7. | 102000    42        0          2          0        2          1          0          0 |
        8. | 102000    14        7          2          0        2          1          0          0 |
        9. | 102000    14        5          2          0        2          1          0          0 |
       10. | 102000    18        8          2          0        2          1          0          0 |
           |---------------------------------------------------------------------------------------|
       11. | 103000     4        0          0          1        0          1          0          0 |
       12. | 103000    20        8          0          1        0          1          0          0 |
       13. | 103000    28        8          0          1        0          1          0          0 |
           |---------------------------------------------------------------------------------------|
       14. | 104000     8        0          2          0        0          0          0          1 |
       15. | 104000    15        0          2          0        0          0          0          1 |
       16. | 104000    29       10          2          0        0          0          0          1 |
       17. | 104000    30       10          2          0        0          0          0          1 |
           +---------------------------------------------------------------------------------------+
      Some reading

      https://www.stata-journal.com/articl...article=dm0055

      https://www.stata.com/support/faqs/d...rue-and-false/

      https://www.stata.com/support/faqs/d...ble-recording/

      Comment


      • #4
        Dear Nick

        Thank you. That's what I required. The readings you shard are also very helpful.

        Comment

        Working...
        X