Announcement

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

  • How can I copy values from a variable to a different observation?

    Hi guys,

    I'd appreciate someone's help.
    I'd like to know how can I copy a value from one observation to another. I've got married people and the place where they were born. There's an identifier to know who is the husband or wife of who and what I want is to generate a variable that show where your husband or wife was born. Something like this:
    conglome vivienda hogar p203
    place_born
    wh_born
    0994 095 11 1 Ayacucho
    0994 095 11 2 Lima
    Conglome, vivienda and hogar define the home where people belong. p203 determines if it's about a husband or wife (1=husband, 2=wife), place_born is where the person was born and wh_born shows the date of birth of the husband o wife's person.
    First observation is a husband and the correspondent wh_born should be Lima (where his wife was born). The same with the second observation (but in this case for the wife).

    I've tried with

    by conglome vivienda hogar, sort: g wh_born=place_born[p203+1] if p203==1

    for husbands, but the problem is that [p203+1] points allways at the second position and sometimes (in the data) husband is in the second position, so wh_born reports the place of birth of the same person. Also, there are more people in the family as son or daughter (which means that p203 has got more categories) and plenty of homes in database. If anyone can help me, I'd really appreciate it.

    Thank you so much.

    Marissa
    Last edited by Marisa Ruiz; 29 Nov 2015, 17:23.

  • #2
    Consider

    Code:
    by conglome vivienda hogar (p203), sort: gen other = anything[3 - _n] if p203[1] == 1 & p203[2] == 2
    If the observation is 1, then 3 - _n is 2 and vice versa.

    See also http://www.stata-journal.com/sjpdf.h...iclenum=dm0043

    http://www.stata-journal.com/article...article=dm0055

    Comment


    • #3
      Sometimes the syntax "by x y (z), sort:" can be helpful. Here it might help you keep your p203 order as you want it.

      Comment


      • #4
        Thank you both, it works as I wanted.

        Comment

        Working...
        X