Announcement

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

  • Linking individuals

    Hello everyone,
    I am only a beginner in Stata and I am currently trying to link individuals. For each individual, I observe his identifier (variable identifier), his age (variable age) and the identifier of his father (variable father identifier). I would like to add a fourth column, which would be the age of the father (the father also belongs to the database).
    An example would be:

    Identifier Age Father identifier Father age
    Son1 18 Father1
    Father 1 50 Grandfather1

    I would like to make "50" appear in the column "father age" for "son1".

    Does anyone have an idea of how to do it?
    Thanks a lot in advance,
    Quentin

  • #2
    Quentin:
    assuming that -Identifier- is -string- variable:
    Code:
    replace Father_Age=50 if Identifier=="Son1"
    In addition, please consider that you need an underscore to link the two parts the name of the variable is split into.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Dear Carlo,
      Thanks a lot for your answer. It works well, but wouldn't you have a code that I can generalize? I have +- 12,000 individuals in the database that I need to link with their fathers. Sorry to disturb you with this question.
      Best regards,
      Quentin

      Comment


      • #4
        What is needed here is some variable, e.g. "FamilyID," that has the same value for each son and father, which you didn't happen to mention. Presuming you have that, you can use -merge- do what you want. Here's some pseudo-code.
        Code:
        preserve
        keep if person is a father // pseudo because no example with actual variables
        keep FamilyID age
        rename age FatherAge
        tempname fatherfile
        save `fatherfile'
        merge m:1 using `fatherfile'
        There are various complexities here, i.e., if multiple sons are present or if multiple generations are present. However, without a data example (see the StataList FAQ re the -dataex- command) and a more complete description of the data file, providing real code would not be possible.

        Comment


        • #5
          My thought - based on similar problems posted here before - is that the ID is distinct across all individuals, not just within families. With that in mind, the following example may start you in a useful direction.
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(ID Age FatherID)
          1 18 2
          2 50 3
          end
          
          preserve
          
          drop FatherID
          rename (ID Age) (FatherID FatherAge)
          tempfile fathers
          save `fathers'
          
          restore
          merge m:1 FatherID using `fathers', keep(match master)
          drop _merge
          
          list, clean abbreviate(12)
          Code:
          . list, clean abbreviate(12)
          
                 ID   Age   FatherID   FatherAge  
            1.    1    18          2          50  
            2.    2    50          3           .

          Comment

          Working...
          X