Announcement

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

  • Creating a variable for an individual's mother's age

    Hi,

    I have individual-level data.
    Everyone has a unique personal ID called "IDPERSON", and I've also created a variable called "motherid" which is the IDPERSON of an individual's mother (if they are in the dataset).
    How can I create a variable e.g. Mother_Age, which would be the age of someone's mother? (there is an age variable in the dataset).

    Thank you!

  • #2
    I have made a toy data set that, as best I can guess from the information provided, resembles your data set.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear*
    input int idperson float(motherid age)
     1  6 14
     2  7 25
     3  7 18
     4  9 15
     5 10 18
     6  . 58
     7  . 58
     8  . 53
     9  . 59
    10  . 52
    end
    
    
    frame put idperson age, into(mothers)
    frlink m:1 motherid, frame(mothers idperson)
    frget mother_age = age, from(mothers)
    This code works properly in my example. It may fail in yours because my guesses about your data set may be wrong.

    In the future, please don't leave things to guess work. Even the most conscientous description of a data set in words is likely to be incomplete and often misleading. The helpful thing is to show actual example data from the data set, and to do that using the -dataex- command, as I have done here. If you are running version 18, 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


    • #3
      Thank you, your code worked!
      Apologies for not posting a representation of the data, I will do in future posts.

      Comment


      • #4
        Here's another way to do it using Clyde's data example and rangestat from SSC.


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear*
        input int idperson float(motherid age)
         1  6 14
         2  7 25
         3  7 18
         4  9 15
         5 10 18
         6  . 58
         7  . 58
         8  . 53
         9  . 59
        10  . 52
        end
        
        gen motherid2 = cond(motherid == ., -1, motherid)
        
        rangestat motherage=age, int(idperson motherid2 motherid2)
        
        list 
        
             +-------------------------------------------------+
             | idperson   motherid   age   mother~2   mother~e |
             |-------------------------------------------------|
          1. |        1          6    14          6         58 |
          2. |        2          7    25          7         58 |
          3. |        3          7    18          7         58 |
          4. |        4          9    15          9         59 |
          5. |        5         10    18         10         52 |
             |-------------------------------------------------|
          6. |        6          .    58         -1          . |
          7. |        7          .    58         -1          . |
          8. |        8          .    53         -1          . |
          9. |        9          .    59         -1          . |
         10. |       10          .    52         -1          . |
             +-------------------------------------------------+

        Comment

        Working...
        X