Announcement

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

  • How to generate a variable for child earnings based on household ID

    Dear Statalisters,

    This is my first question, I apologize if it is not formatted correctly. I am struggling with something that is likely relatively simple for those with experience with commands such as by, sort, egen.

    I have a data set of households and for each "head" of household, I want to generate a value for their children's income. In the example posted below, for the variable 'Relationship', 1= head of household, 3 = one of the head of household's children.

    Essentially, I would love some coding advice on how to execute something like this: For each head of household, generate a value denoting their children's MonthlyUSD or an average value if there is more than one child per household.

    Thank you kindly in advance! And please let me know if you require any other information.



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 hhid str9 persid int Age byte Relationship float MonthlyUSD
    "0100101" "010010101" 77 1         .
    "0100101" "010010103" 36 3         0
    "0100102" "010010201" 38 1         0
    "0100102" "010010203" 12 3         .
    "0100102" "010010204" 11 3         .
    "0100102" "010010205"  1 3         .
    "0100103" "010010301" 50 1         0
    "0100103" "010010303" 15 3 148.14815
    "0100103" "010010304" 11 3         .
    "0100104" "010010401" 73 1         .
    "0100104" "010010402" 43 3         .
    "0100105" "010010501" 42 1         0
    "0100105" "010010502" 24 3 148.14815
    "0100105" "010010503" 19 3         0
    "0100105" "010010504" 14 3         .
    "0100106" "010010601" 33 1 155.55556
    "0100106" "010010603" 10 3         .
    "0100106" "010010604"  1 3         .
    "0100107" "010010701" 35 1  207.4074
    "0100107" "010010703" 11 3         .
    end
    label values Relationship Q01AC06
    label def Q01AC06 1 "Head", modify
    label def Q01AC06 3 "Son/Daughter", modify

  • #2
    Sorry, I realize now that the example I generated is not very useful given the number of missing variables. Here is another with more observations:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 hhid str9 persid int Age byte Relationship float MonthlyUSD
    "0100101" "010010103" 36 3         0
    "0100102" "010010201" 38 1         0
    "0100103" "010010301" 50 1         0
    "0100103" "010010303" 15 3 148.14815
    "0100105" "010010501" 42 1         0
    "0100105" "010010502" 24 3 148.14815
    "0100105" "010010503" 19 3         0
    "0100106" "010010601" 33 1 155.55556
    "0100107" "010010701" 35 1  207.4074
    "0100108" "010010801" 34 1         0
    "0100109" "010010901" 60 1         0
    "0100110" "010011001" 56 1         0
    "0100110" "010011003" 20 3 259.25925
    "0100201" "010020101" 51 1         0
    "0100201" "010020102" 34 3 118.51852
    "0100201" "010020103" 31 3  192.5926
    "0100202" "010020201" 25 1  98.76543
    "0100203" "010020301" 46 1 256.79013
    "0100203" "010020303" 21 3  192.5926
    "0100203" "010020304" 17 3  192.5926
    "0100204" "010020401" 43 1         0
    "0100205" "010020501" 43 1         0
    "0100205" "010020504" 18 3         0
    "0100206" "010020601" 45 1 148.14815
    "0100206" "010020603" 20 3         0
    "0100207" "010020702" 45 3         0
    "0100208" "010020801" 60 1         0
    "0100208" "010020803" 25 3         0
    "0100208" "010020804" 21 3         0
    "0100209" "010020901" 49 1         0
    "0100210" "010021003" 31 3         0
    "0100301" "010030101" 56 1         0
    "0100301" "010030103" 35 3  311.1111
    "0100303" "010030301" 51 1         0
    "0100303" "010030303" 25 3         0
    "0100304" "010030401" 49 1         0
    "0100304" "010030403" 17 3 167.90123
    "0100305" "010030501" 82 1         0
    "0100305" "010030503" 48 3  234.5679
    "0100305" "010030504" 38 3         0
    "0100305" "010030505" 40 3 186.41975
    "0100305" "010030506" 32 3  234.5679
    "0100306" "010030601" 64 1         0
    "0100306" "010030603" 34 3  192.5926
    "0100306" "010030604" 30 3 209.87654
    "0100306" "010030605" 27 3 171.60493
    "0100306" "010030606" 23 3  234.5679
    "0100307" "010030701" 36 1 222.22223
    "0100307" "010030703" 15 3         0
    "0100308" "010030801" 39 1 256.79013
    end
    label values Relationship Q01AC06
    label def Q01AC06 1 "Head", modify
    label def Q01AC06 3 "Son/Daughter", modify


    Comment


    • #3
      Yann, in the following code, "cMonthlyUSD" is the wanted variable and its value appears only for household head.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str7 hhid str9 persid int Age byte Relationship float MonthlyUSD
      "0100101" "010010103" 36 3         0
      "0100102" "010010201" 38 1         0
      "0100103" "010010301" 50 1         0
      "0100103" "010010303" 15 3 148.14815
      "0100105" "010010501" 42 1         0
      "0100105" "010010502" 24 3 148.14815
      "0100105" "010010503" 19 3         0
      "0100106" "010010601" 33 1 155.55556
      "0100107" "010010701" 35 1  207.4074
      "0100108" "010010801" 34 1         0
      "0100109" "010010901" 60 1         0
      "0100110" "010011001" 56 1         0
      "0100110" "010011003" 20 3 259.25925
      "0100201" "010020101" 51 1         0
      "0100201" "010020102" 34 3 118.51852
      "0100201" "010020103" 31 3  192.5926
      "0100202" "010020201" 25 1  98.76543
      "0100203" "010020301" 46 1 256.79013
      "0100203" "010020303" 21 3  192.5926
      "0100203" "010020304" 17 3  192.5926
      "0100204" "010020401" 43 1         0
      "0100205" "010020501" 43 1         0
      "0100205" "010020504" 18 3         0
      "0100206" "010020601" 45 1 148.14815
      "0100206" "010020603" 20 3         0
      "0100207" "010020702" 45 3         0
      "0100208" "010020801" 60 1         0
      "0100208" "010020803" 25 3         0
      "0100208" "010020804" 21 3         0
      "0100209" "010020901" 49 1         0
      "0100210" "010021003" 31 3         0
      "0100301" "010030101" 56 1         0
      "0100301" "010030103" 35 3  311.1111
      "0100303" "010030301" 51 1         0
      "0100303" "010030303" 25 3         0
      "0100304" "010030401" 49 1         0
      "0100304" "010030403" 17 3 167.90123
      "0100305" "010030501" 82 1         0
      "0100305" "010030503" 48 3  234.5679
      "0100305" "010030504" 38 3         0
      "0100305" "010030505" 40 3 186.41975
      "0100305" "010030506" 32 3  234.5679
      "0100306" "010030601" 64 1         0
      "0100306" "010030603" 34 3  192.5926
      "0100306" "010030604" 30 3 209.87654
      "0100306" "010030605" 27 3 171.60493
      "0100306" "010030606" 23 3  234.5679
      "0100307" "010030701" 36 1 222.22223
      "0100307" "010030703" 15 3         0
      "0100308" "010030801" 39 1 256.79013
      end
      label values Relationship Q01AC06
      label def Q01AC06 1 "Head", modify
      label def Q01AC06 3 "Son/Daughter", modify
      
      bys hhid: egen cMonthlyUSD = mean(MonthlyUSD) if Relationship == 3
      bys hhid (cMonthlyUSD): replace cMonthlyUSD = cMonthlyUSD[1] if Relationship == 1
      replace cMonthlyUSD = . if Relationship != 1
      sort hhid persid

      Comment


      • #4
        Fei,

        Thank you very very much, this code works as desired. Greatly appreciated!

        Best,

        Yann

        Comment

        Working...
        X