Announcement

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

  • Moving and matching observations

    I am trying to create a new variable for husband’s ethnicity and want to ensure that it is in same cell as ethnicity (which is spouse’s ethnicity).
    • hhid is case identification (constructed out of cluster number and household number)
    • year is survey year
    • hvidx is line number
    • husband is line number of husband for the married female (for hhid "101 2", year 2006, hvidx 2, the female’s husband corresponds to the same hhid, year but hvidx 1).
    I would like have husband’s ethnicity in a new variable parallel to this wife’s ethnicity. The goal later is to subtract wife’s ethnicity from husband’s ethnicity and see if there is any difference.

    Any help on this is highly appreciated.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 hhid int(hv001 hv002) float year byte(hvidx husband gender) float ethnicity
    "     101   1" 101  1 2006 1 . 1  .
    "     101   1" 101  1 2006 2 . 2  .
    "     101   2" 101  2 2006 1 . 1 10
    "     101   2" 101  2 2006 2 1 2 10
    "     101   2" 101  2 2006 3 . 2 10
    "     101   2" 101  2 2006 4 . 1 10
    "     101   2" 101  2 2006 5 . 2  .
    "     101   3" 101  3 2006 1 . 1  .
    "     101   3" 101  3 2006 2 . 2  .
    "     101   3" 101  3 2006 3 . 2 10
    "     101   3" 101  3 2006 4 . 1 10
    "     101   3" 101  3 2006 5 . 1  .
    "     101   4" 101  4 2006 1 . 2 10
    "     101   4" 101  4 2006 2 . 1  .
    "     101   4" 101  4 2006 3 . 2  .
    "     101   4" 101  4 2006 4 . 1  .
    "     101   5" 101  5 2006 1 . 1  .
    "     101   5" 101  5 2006 2 . 2  .
    "     101   5" 101  5 2006 3 . 2  .
    "     101   5" 101  5 2006 4 . 1  .
    "     101   6" 101  6 2006 1 . 1 10
    "     101   6" 101  6 2006 2 . 2  .
    "     101   6" 101  6 2006 3 . 1 10
    "     101   6" 101  6 2006 4 3 2 10
    "     101   6" 101  6 2006 5 . 2 10
    "     101   6" 101  6 2006 6 . 2 10
    "     101   6" 101  6 2006 7 . 2  .
    "     101   6" 101  6 2006 8 . 1  .
    "     101   6" 101  6 2006 9 . 2  .
    "     101   7" 101  7 2006 1 . 2  .
    "     101   7" 101  7 2006 2 . 2 10
    "     101   7" 101  7 2006 3 . 2 10
    "     101   8" 101  8 2006 1 . 2 10
    "     101   8" 101  8 2006 2 . 1 10
    "     101   8" 101  8 2006 3 . 1  .
    "     101   9" 101  9 2006 1 . 1  .
    "     101   9" 101  9 2006 2 . 2 10
    "     101   9" 101  9 2006 3 . 2  .
    "     101   9" 101  9 2006 4 . 1  .
    "     101   9" 101  9 2006 5 . 2  .
    "     101   9" 101  9 2006 6 . 1  .
    "     101   9" 101  9 2006 7 . 2  .
    "     101  10" 101 10 2006 1 . 1 10
    "     101  10" 101 10 2006 2 . 1  .
    "     101  10" 101 10 2006 3 . 2  .
    "     101  10" 101 10 2006 4 . 2 10
    "     101  10" 101 10 2006 5 . 2  .
    "     101  10" 101 10 2006 6 . 1  .
    "     101  10" 101 10 2006 7 . 1  .
    "     101  11" 101 11 2006 1 . 1  .
    "     101  11" 101 11 2006 2 . 2 10
    "     101  11" 101 11 2006 3 . 1  .
    "     101  11" 101 11 2006 4 . 2 10
    "     101  11" 101 11 2006 5 . 2 10
    "     101  11" 101 11 2006 6 . 1  .
    "     101  11" 101 11 2006 7 . 1  .
    "     101  11" 101 11 2006 8 . 2  .
    "     101  12" 101 12 2006 1 . 1 10
    "     101  12" 101 12 2006 2 . 2  .
    "     101  12" 101 12 2006 3 . 2 10
    "     101  12" 101 12 2006 4 . 2 10
    "     101  12" 101 12 2006 5 . 2 10
    "     101  12" 101 12 2011 1 . 1  .
    "     101  12" 101 12 2011 2 . 2  2
    "     101  12" 101 12 2011 3 . 2  .
    "     101  12" 101 12 2011 4 . 1  .
    "     101  12" 101 12 2011 5 . 1  .
    "     101  13" 101 13 2011 1 . 1  .
    "     101  13" 101 13 2011 2 . 2  .
    "     101  13" 101 13 2011 3 . 1  2
    "     101  13" 101 13 2011 4 . 1  2
    "     101  13" 101 13 2011 5 . 2  .
    "     101  14" 101 14 2011 1 . 1  .
    "     101  14" 101 14 2011 2 . 2  .
    "     101  15" 101 15 2011 1 . 1  2
    "     101  15" 101 15 2011 2 1 2  2
    "     101  15" 101 15 2011 3 . 1  .
    "     101  15" 101 15 2011 4 . 2  .
    "     101  16" 101 16 2011 1 . 1  .
    "     101  16" 101 16 2011 2 . 2  2
    "     101  16" 101 16 2011 3 . 2  .
    "     101  16" 101 16 2011 4 . 2  .
    "     101  16" 101 16 2011 5 . 1  .
    "     101  17" 101 17 2011 1 . 1  2
    "     101  17" 101 17 2011 2 1 2  2
    "     101  17" 101 17 2011 3 . 2  2
    "     101  18" 101 18 2011 1 . 2  .
    "     101  18" 101 18 2011 2 . 1  .
    "     101  19" 101 19 2011 1 . 1  2
    "     101  19" 101 19 2011 2 1 2  2
    "     101  19" 101 19 2011 3 . 1  2
    "     101  19" 101 19 2011 4 3 2  2
    "     101  19" 101 19 2011 5 . 1  .
    "     101  19" 101 19 2011 6 . 2  2
    "     101  19" 101 19 2011 7 . 1  2
    "     101  20" 101 20 2011 1 . 1  .
    "     101  20" 101 20 2011 2 . 2  .
    "     101  21" 101 21 2011 1 . 1  .
    "     101  21" 101 21 2011 2 . 2  .
    "     101  21" 101 21 2011 3 . 1  .
    end
    label values husband v034
    label values gender hv104
    label def hv104 1 "male", modify
    label def hv104 2 "female", modify

  • #2
    If ethnicity is a number and you only have one husband per wife in a given year, you could do bygroup hhind year: egen huseth=mean(ethnicity) if hv104 ==1
    Alternatively, you could create a new variables, save the data, and then merge it to match husbands and wives. But egen is probably much easier.

    Comment


    • #3
      A third way to do this would be to sort the data properly (by family, year hv104), and then do a generate that refers to the next observation
      g huseth=ethnicity[_n-1] if hhind== hhind[_n-1] & year==year[_n-1]

      Comment

      Working...
      X