Announcement

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

  • Code to count number of children for each household head

    I want to create a variable to count the number of children and another varible to count the number of adults in each household , for example the number of adults in head of dD 4 are 2 adults and zero children and headof~D 10 have 2 adults and 6 children

    here is an example of what I want is in Bold




    headof~D Questi serial Relati~s Age number of adults number of children
    --------------------------------------
    1. 4 4 1 56 2 0
    2. 6 4 4 80 2 0
    3. 10 6 1 21 2 6
    4. 16 6 4 53 3 0
    5. 20 10 1 35
    --------------------------------------
    6. 22 10 2 34
    7. 24 10 3 11
    8. 26 10 3 10
    9. 28 10 3 7
    10. 30 10 3 4
    --------------------------------------
    11. 32 10 3 3
    12. 36 10 3 3
    13. 38 10 3 1
    14. 40 16 1 61
    15. 42 16 2 57
    --------------------------------------
    16. 44 16 3 31
    17. 46 20 1 44
    18. . 20 2 38
    19. . 20 3 16
    20. . 20 3 16
    --------------------------------------





    ssc install dataex
    checking dataex consistency and verifying not already installed...
    all files already exist and are up to date.

    . list headofhouseholdID QuestionnaireSerialNumberint Relationshiptotheheadofhous Age

    +--------------------------------------+
    headof~D Questi~t Relati~s Age
    --------------------------------------
    1. 4 4 1 56
    2. 6 4 4 80
    3. 10 6 1 21
    4. 16 6 4 53
    5. 20 10 1 35
    --------------------------------------
    6. 22 10 2 34
    7. 24 10 3 11
    8. 26 10 3 10
    9. 28 10 3 7
    10. 30 10 3 4
    --------------------------------------
    11. 32 10 3 3
    12. 36 10 3 3
    13. 38 10 3 1
    14. 40 16 1 61
    15. 42 16 2 57
    --------------------------------------
    16. 44 16 3 31
    17. 46 20 1 44
    18. . 20 2 38
    19. . 20 3 16
    20. . 20 3 16
    --------------------------------------
    21. . 20 3 13
    22. . 20 3 11
    23. . 20 3 9
    24. . 20 3 8
    25. . 22 1 70
    --------------------------------------
    26. . 22 2 62
    27. . 22 3 28
    28. . 24 1 40
    29. . 24 2 34
    30. . 24 3 10
    --------------------------------------
    31. . 24 3 7
    32. . 24 3 3
    33. . 24 3 1
    34. . 26 1 45
    35. . 26 2 43
    --------------------------------------
    36. . 26 3 17
    37. . 26 3 13
    38. . 26 3 10
    39. . 26 3 7
    40. . 26 3 2
    --------------------------------------
    41. . 28 1 54
    42. . 28 2 49
    43. . 28 3 23
    44. . 28 3 18
    45. . 28 3 14
    --------------------------------------
    46. . 30 1 70
    47. . 30 2 39
    48. . 30 3 29
    49. . 30 3 22
    50. . 32 1 69
    --------------------------------------
    51. . 32 2 62
    52. . 32 3 43
    53. . 32 3 42
    54. . 32 7 16
    55. . 36 1 50
    --------------------------------------
    56. . 36 2 43
    57. . 36 3 24
    58. . 36 3 22
    59. . 36 3 16
    60. . 38 1 45
    --------------------------------------
    61. . 38 2 38
    62. . 38 3 12
    63. . 38 3 9
    64. . 38 3 5
    65. . 38 3 4
    --------------------------------------
    66. . 38 5 49
    67. . 40 1 50
    68. . 40 2 44
    69. . 40 3 23
    70. . 40 3 21
    --------------------------------------
    71. . 40 3 18
    72. . 40 3 16
    73. . 40 3 12
    74. . 40 2 53
    75. . 40 3 13
    --------------------------------------
    76. . 42 1 70
    77. . 42 2 51
    78. . 42 3 28
    79. . 42 3 23
    80. . 44 1 40
    --------------------------------------
    81. . 44 2 32
    82. . 44 3 12
    83. . 44 3 7
    84. . 44 3 4
    85. . 44 3 3
    --------------------------------------
    86. . 46 1 52
    87. . 46 2 47
    88. . 46 3 21
    89. . 46 3 16
    90. . . . 13
    --------------------------------------
    91. . . . 8
    92. . . . 6
    93. . . . 3
    +--------------------------------------+

    .

  • #2
    Your data example requires some minor surgery or engineering to get into decent form and I can't follow your variable names easily. It's more straightforward to invent an example.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(hh_id age)
    1 99
    1 88
    1 25
    1  6
    2 43
    2 34
    3  5
    3 42
    3 42
    3 18
    end
    
    egen nchildren = total(age <= 18), by(hh_id)
    
    egen npeople = count(age), by(hh_id)
    
    gen nadults = npeople - nchildren
    
    l, sepby(hh_id)
    
         +--------------------------------------------+
         | hh_id   age   nchild~n   npeople   nadults |
         |--------------------------------------------|
      1. |     1    99          1         4         3 |
      2. |     1    88          1         4         3 |
      3. |     1    25          1         4         3 |
      4. |     1     6          1         4         3 |
         |--------------------------------------------|
      5. |     2    43          0         2         2 |
      6. |     2    34          0         2         2 |
         |--------------------------------------------|
      7. |     3     5          2         4         2 |
      8. |     3    42          2         4         2 |
      9. |     3    42          2         4         2 |
     10. |     3    18          2         4         2 |
         +--------------------------------------------+
    Here I used a definition that children are 18 or younger and anyone not a child is an adult. If your definition is different. modify the code accordingly.

    Comment

    Working...
    X