Announcement

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

  • Matching values in 2 columns and generating an output for a new variable.

    Dear all,

    I am a Stata newbie and would appreciate your help with the following problem. I have a dataset with variables pid (=unique number of an individual), parid (= pid of individual's partner), sex (= sex of individual with pid). With these I would like to generate output for a new variable parsex capturing sex of each parid. The idea is to find parid in the pid column and show sex = parsex.

    I have tried the following code:
    gen parsex = .
    replace parsex = sex if pid == par







  • #2
    Try this:
    Code:
    isid pid
    frame put pid sex, into(partners)
    frlink 1:1 parid, frame(partners pid)
    frget parsex = sex, from(partners)
    Note: Because no example data was provided, this code is untested and may be wrong. If it does not work as intended, please post back using the -dataex- command to show example data exhibiting the problematic behavior.

    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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X