Announcement

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

  • Generating an age variable of the partner

    Hello everybody,

    this is the first time I have been doing data management on my own. I did not have any big problems until now. I am working with a panel data with about 900000 observations and need to generate a variable that contains information of the age of the respondent's partner. Here is one example:
    persnr year age partnr age_partner
    45501 2002 57 45502 .
    45501 2003 58 45502 .
    45502 2002 53 45501 .
    45502 2003 54 45501 .
    I have the respondent's identifier as well as the identifier of the partner.The presented individuals are a couple. I have information on age of both over two periods. However, I would like to generate a variable (age_partner) that states the age of each respondent's partner. I need to do this for every individual. I kindly appreciate any help.

    Wishes,

    Joseph

  • #2
    Normally, this is done with some dataset gymnastics that require renaming variables and a merge. Here's another solution using rangestat (from SSC). To install it, type in Stata's Command window:
    Code:
    ssc install rangestat
    The following example assumes that your identifiers are numeric. Please use dataex (also from SSC) to generate data examples as this avoids having to speculate on your data types. I also assume that all identifiers are positive values and that some people have no partner. The solution finds the age for the parter in the current year. If there are household identifiers, you would want to include the variable in the by() option. Finally, since persnr uniquely identifies observations by year, it does not matter that the age is picked-up using (min); you could just as well use (max) or (mean).

    Code:
    clear
    input long persnr int year byte age long partnr
    45501 2002 57 45502
    45501 2003 58 45502
    45502 2002 53 45501
    45502 2003 54 45501
    45503 2002  6     .
    45503 2003  7     .
    end
    
    * verify assumptions about the data
    isid year persnr, sort
    assert persnr > 0
    
    * define interval bounds using 0 and -1 if there is no partner
    gen low = cond(mi(partnr), 0, partnr)
    gen high = cond(mi(partnr), -1, partnr)
    
    rangestat (min) part_age=age, interval(persnr low high) by(year)
    
    list, sepby(year)
    and the results:
    Code:
    . list, sepby(year)
    
         +---------------------------------------------------------+
         | persnr   year   age   partnr     low    high   part_age |
         |---------------------------------------------------------|
      1. |  45501   2002    57    45502   45502   45502         53 |
      2. |  45502   2002    53    45501   45501   45501         57 |
      3. |  45503   2002     6        .       0      -1          . |
         |---------------------------------------------------------|
      4. |  45501   2003    58    45502   45502   45502         54 |
      5. |  45502   2003    54    45501   45501   45501         58 |
      6. |  45503   2003     7        .       0      -1          . |
         +---------------------------------------------------------+

    Comment


    • #3
      Thank you very much for the reply. As I was installing the package I got the error message r(602) saying: . ssc install rangestat, replace
      checking rangestat consistency and verifying not already installed...
      installing into c:\ado\plus\...
      file c:\ado\plus\next.trk already exists
      r(602);

      end of do-file

      Replacing it was not possible, trying to find it did not give me any results, using the uninstall command leads to error message r(111) (package not found). I tried to look for tempfiles but I did not have administrative rights to access them. Are there any other possible ways to solve this new problem???

      Thank you very much for your time.

      Comment


      • #4
        I don't know what this "next.trk" file is. Perhaps this is something that Stata tech support can help with.

        In the mean time, you can install any SSC program in Stata's current directory. First, make sure that Stata's current directory is the one containing your data/do-files (help cd) and then type in Stata's Command window:
        Code:
        net set ado .
        ssc install rangestat
        You will then be able to use rangestat as long as the current directory is not changed.

        Comment

        Working...
        X