Announcement

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

  • Assign the oldest in a household as head

    Using index as household identifier, a3age as Age,1== head in the relationship

  • #2
    Shamsudeen:
    welcome to this forum.
    You may want to try something along the following lines:
    Code:
    . set obs 5
    number of observations (_N) was 0, now 5
    
    . g hd_id=1
    
    . g id_hd_member=_n
    
    . g Age=60 in 1
    (4 missing values generated)
    
    . replace Age=Age[_n-1]+5 if Age==.
    (4 real changes made)
    
    . quietly sum Age
    
    . g id_hd_head=1 if Age==r(max)
    (4 missing values generated)
    
    . replace id_hd_head=0 if id_hd_head==.
    (4 real changes made)
    
    . list
    
         +-----------------------------------+
         | hd_id   id_hd_~r   Age   id_hd_~d |
         |-----------------------------------|
      1. |     1          1    60          0 |
      2. |     1          2    65          0 |
      3. |     1          3    70          0 |
      4. |     1          4    75          0 |
      5. |     1          5    80          1 |
         +-----------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      I believe the solution in #2 will only work if there is one household in the dataset. If you have no missing data on your age variable, the following should work

      Code:
      by hh_id (Age), sort: gen head = _n==_N
      This relies on sorting the largest age value last (within household), and then flagging the final observation--where the observation number equals the total number of observations (again within household).

      But missing age values will sort as the largest, so anyone missing will be assigned as head of household. If that's not what you want, you could do something like this:

      Code:
      egen maxAge = max(Age), by(hh_id)
      gen head = Age==maxAge
      The two approaches deal differently with ties--the first chooses arbitrarily between them; the second flags both as head.

      Comment

      Working...
      X