Announcement

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

  • How to assign the same values to the same ID?

    Hi,

    I am cleaning panel data. I merged the family data and newborn data, but only the mothers' data are linked, e.g. for family 11, only the mother (ID: 1, Sex: female) has the newborn birth year data. So, could you tell me how to copy the newborn's data to his/her father? Also, I want to assign the newborn_year to all his/her mother's and fathers' records.

    ID. Year family_id Sex newborn_year
    1. 2001. 11. F. .
    1. 2002 11. F. 1994
    1. 2003. 11. F. .
    2. 2001. 11. M. .
    2. 2002. 11. M. .
    2. 2003. 11. M. .
    3. 2001. 12. F. .
    3. 2002 12. F. .
    3. 2003. 12. F. 2003
    4. 2001. 12. M. .
    4. 2002. 12. M. .
    4. 2003. 12. M. .


    The final data I expect is:

    ID. Year family_id Sex newborn_year
    1. 2001. 11. F. 1994
    1. 2002 11. F. 1994
    1. 2003. 11. F. 1994
    2. 2001. 11. M. 1994
    2. 2002. 11. M. 1994
    2. 2003. 11. M. 1994
    3. 2001. 12. F. 2003
    3. 2002 12. F. 2003
    3. 2003. 12. F. 2003
    4. 2001. 12. M. 2003
    4. 2002. 12. M. 2003
    4. 2003. 12. M. 2003


    Thank you so much.
    Zichun






  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id int year byte family_id str2 sex int newborn_year
    1 2001 11 "F."    .
    1 2002 11 "F." 1994
    1 2003 11 "F."    .
    2 2001 11 "M."    .
    2 2002 11 "M."    .
    2 2003 11 "M."    .
    3 2001 12 "F."    .
    3 2002 12 "F."    .
    3 2003 12 "F." 2003
    4 2001 12 "M."    .
    4 2002 12 "M."    .
    4 2003 12 "M."    .
    end
    
    by family_id (newborn_year), sort: replace newborn_year = newborn_year[1]
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X