Announcement

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

  • Generating a variable dependent on the year

    Hi,
    I am trying to generate a variable (i.e. labor income_breastfeeding; employmentstatus_breastfeeding; etc...) for each of my observations in respect to the year that the mother was breastfeeding (year_breastfeeding). My data is panel data set with child-mother pairs and for example looks like this:

    ID_child ID_mother year labor income year_breastfeeding laborincome_breastfeeding (desired var with desired values)
    1 a 1989 34568 1990 36789
    1 a 1990 36789 1990 36789
    1 a 1991 37329 1990 36789
    2 b 1983 50681 1984 54808
    2 b 1984 54808 1984 54808
    2 b 1985 55029 1984 54808
    2 b 1886 56928 1984 54808

    Therefore, the laborincome_breastfeeding for ID_child 1, ID_mother a, should be 36789 since 1990 is the year_breastfeeding and for ID_child 2, ID_mother b, laborincome_breastfeeding should be 54808.
    I have tried the egen command; however, cannot seem to figure out how to do it correctly. I'm a bit new at state but perhaps a loop over observations would be best?

    Any help would be greatly appreciated, thank you for your time and help!

    Regards,
    Surya

  • #2
    Your variables seem a bit tangled up, but the spirit of what you want seems to be

    Code:
    . clear 
    
    . input ID_child str1 ID_mother year1 income year2  wanted
    
          ID_child  ID_mother      year1     income      year2     wanted
      1. 1 a 1989 34568 1990 36789
      2. 1 a 1990 36789 1990 36789
      3. 1 a 1991 37329 1990 36789
      4. 2 b 1983 50681 1984 54808
      5. 2 b 1984 54808 1984 54808
      6. 2 b 1985 55029 1984 54808
      7. 2 b 1986 56928 1984 54808 
      8. end 
    
    . 
    . egen income_breastfeeding = total(income * (year1 == year2)), by(ID_child ID_mother)  
    
    . assert wanted == income_breastfeeding 
    
    . 
    . l, sepby(ID*) 
    
         +------------------------------------------------------------------+
         | ID_child   ID_mot~r   year1   income   year2   wanted   income~g |
         |------------------------------------------------------------------|
      1. |        1          a    1989    34568    1990    36789      36789 |
      2. |        1          a    1990    36789    1990    36789      36789 |
      3. |        1          a    1991    37329    1990    36789      36789 |
         |------------------------------------------------------------------|
      4. |        2          b    1983    50681    1984    54808      54808 |
      5. |        2          b    1984    54808    1984    54808      54808 |
      6. |        2          b    1985    55029    1984    54808      54808 |
      7. |        2          b    1986    56928    1984    54808      54808 |
         +------------------------------------------------------------------+

    Comment


    • #3
      Thank you Nick!

      Originally posted by Nick Cox View Post
      Your variables seem a bit tangled up, but the spirit of what you want seems to be

      Code:
      . clear
      
      . input ID_child str1 ID_mother year1 income year2 wanted
      
      ID_child ID_mother year1 income year2 wanted
      1. 1 a 1989 34568 1990 36789
      2. 1 a 1990 36789 1990 36789
      3. 1 a 1991 37329 1990 36789
      4. 2 b 1983 50681 1984 54808
      5. 2 b 1984 54808 1984 54808
      6. 2 b 1985 55029 1984 54808
      7. 2 b 1986 56928 1984 54808
      8. end
      
      .
      . egen income_breastfeeding = total(income * (year1 == year2)), by(ID_child ID_mother)
      
      . assert wanted == income_breastfeeding
      
      .
      . l, sepby(ID*)
      
      +------------------------------------------------------------------+
      | ID_child ID_mot~r year1 income year2 wanted income~g |
      |------------------------------------------------------------------|
      1. | 1 a 1989 34568 1990 36789 36789 |
      2. | 1 a 1990 36789 1990 36789 36789 |
      3. | 1 a 1991 37329 1990 36789 36789 |
      |------------------------------------------------------------------|
      4. | 2 b 1983 50681 1984 54808 54808 |
      5. | 2 b 1984 54808 1984 54808 54808 |
      6. | 2 b 1985 55029 1984 54808 54808 |
      7. | 2 b 1986 56928 1984 54808 54808 |
      +------------------------------------------------------------------+

      Comment

      Working...
      X