Announcement

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

  • Merging Individual-level dataset with party-level dataset

    Dear all,

    I am working with two cross-sectional datasets on STATA18. The first one is a dataset measuring political attitudes at the individual level, with respondents nested in 14 countries. The identifier is idno. Here is a snapshot of my dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long idno str3 iso_country float(Immigration_cult Immigration_poornoeu Immigration_dfrace EU GenderIss Redistribution)
    26073 "AT"  2        0        0  3   0   0
    53376 "AT"  0        0        0  0   0   0
    10075 "AT"  2 3.333333 3.333333  3   5   0
     2696 "AT"  5       10       10 10 2.5 2.5
    43811 "AT"  4 6.666667 6.666667  5   0   0
    31623 "AT"  8 3.333333 3.333333  8   0 2.5
    44700 "AT"  7       10       10 10   0   5
    61058 "AT"  6 6.666667 6.666667  5   5 2.5
    31702 "AT"  4 6.666667 6.666667  3   0   0
    46004 "AT"  5 6.666667 6.666667  9 2.5 2.5
    30316 "AT"  8       10       10  6 2.5 2.5
    21491 "AT"  5 6.666667 6.666667 10   0   0
     3163 "AT"  6 3.333333 6.666667  6   .   0
    23335 "AT" 10       10       10  7   0   0
    46585 "AT"  .       10 6.666667  6 7.5 2.5
    29420 "AT"  . 3.333333 3.333333  4 2.5   0
    45263 "AT"  . 6.666667 6.666667  .   . 2.5
    14666 "AT" 10 6.666667 6.666667  5 2.5   0
     3773 "AT"  5        0        0 10   0   0
    51005 "AT"  4 6.666667 6.666667  0 7.5 2.5
    58901 "AT"  0 3.333333 3.333333  7   5   0
     6198 "AT"  2        0        0  .   0   0
    55424 "AT"  7 3.333333 3.333333  2 2.5   0
    66609 "AT"  .       10 3.333333  7 2.5 2.5
    14746 "AT"  3 6.666667 6.666667 10   5 2.5
    69494 "AT"  8 6.666667 6.666667  5   5 2.5
    53952 "AT"  6 6.666667 6.666667  6   5  10
    19547 "AT"  2 6.666667 6.666667  4  10 7.5
    54657 "AT"  6 6.666667 3.333333  9   5 2.5
    41238 "AT"  8       10 6.666667  9   0   0
    52318 "AT"  5 6.666667 6.666667  5 2.5   5
    28089 "AT"  6 6.666667 6.666667  6 2.5 2.5
    36230 "AT"  8        . 6.666667  3 7.5   5
    29678 "AT" 10 3.333333 6.666667  3 2.5   0
    68389 "AT"  6 6.666667 6.666667  9 2.5 2.5
    15075 "AT"  7 6.666667 6.666667  4   .   0
    48945 "AT"  7 3.333333 6.666667  5 2.5   0
    44489 "AT"  8 3.333333 3.333333  9 2.5 2.5
    14807 "AT"  4 6.666667 6.666667  7   0   0
    20595 "AT"  5 6.666667 3.333333  3   0 7.5
    67624 "AT" 10 6.666667 3.333333  5 2.5 2.5
    48947 "AT"  3 3.333333 6.666667  6   0 2.5
    49256 "AT" 10       10       10 10 2.5 2.5
    58008 "AT"  7 6.666667 6.666667 10 7.5 2.5
    24673 "AT"  4 3.333333 3.333333  4 2.5 2.5
     5703 "AT"  5 6.666667 6.666667  7   5 2.5
    23671 "AT"  3 3.333333 3.333333  2   0   0
    28378 "AT"  4 6.666667 6.666667  4   0 2.5
    28346 "AT" 10       10       10 10   0   5
    32543 "AT"  1        .        .  0   0   0
    49913 "AT"  3        0        0  4   0   0
    24074 "AT" 10       10       10  5  10 7.5
    29559 "AT" 10       10 6.666667 10   0  10
    47706 "AT"  2        0        0  9 2.5 2.5
    51351 "AT"  4 3.333333 3.333333  0 2.5 2.5
    17534 "AT"  4 6.666667 6.666667  3   0 2.5
    66555 "AT"  0 3.333333 3.333333  3   0 2.5
    52718 "AT"  1 3.333333 3.333333  2   0 2.5
    49473 "AT"  5 3.333333 3.333333  9 2.5 2.5
    19630 "AT"  2 3.333333 3.333333  8 2.5 2.5
    65567 "AT"  9       10 6.666667  8   5 2.5
    56550 "AT"  5 3.333333 3.333333  6   0 7.5
    27857 "AT"  2 3.333333 3.333333  6 2.5 2.5
    33111 "AT"  1 3.333333        0  6 2.5 2.5
     6515 "AT"  0        0        0  0   0   0
    16471 "AT"  5       10       10  6   0 2.5
    36904 "AT"  . 3.333333 3.333333 10   0   0
     5152 "AT"  5 6.666667 6.666667  8 7.5 2.5
     8825 "AT"  0 3.333333 3.333333  0   0   0
    46925 "AT"  5 6.666667        0  .   5 2.5
    35019 "AT" 10       10       10  9   0 2.5
    62447 "AT"  2 3.333333 6.666667  5 2.5 2.5
    59617 "AT"  9       10       10  6 2.5   0
    31162 "AT"  5 3.333333 3.333333  4 2.5   0
      208 "AT"  3 6.666667 6.666667  8   0 2.5
    45122 "AT"  7 3.333333 3.333333  2 7.5 2.5
    39283 "AT"  3        0        0  3   0   0
    58572 "AT"  5       10 3.333333  6   . 2.5
    10474 "AT"  5 6.666667 6.666667  6   5 2.5
    43723 "AT"  8 6.666667 3.333333  7   0   0
    16465 "AT"  8       10       10 10   0   .
    38390 "AT" 10       10       10 10   0 2.5
    12564 "AT"  6 6.666667 6.666667  9 2.5 2.5
    49161 "AT"  1 3.333333 3.333333  7   0 2.5
    32048 "AT"  5        .        .  . 7.5   .
    42213 "AT"  2 6.666667 6.666667 10 2.5   5
    19333 "AT"  1 3.333333 3.333333  0 2.5 2.5
    27470 "AT"  3 3.333333 3.333333  5   0 7.5
    10800 "AT"  7 6.666667 6.666667  8   5 7.5
    35498 "AT"  5 3.333333 3.333333  4 2.5   0
     5571 "AT"  5 3.333333 3.333333  6 2.5 2.5
    11212 "AT"  5 6.666667 6.666667  5   0 2.5
    41864 "AT"  5 6.666667 6.666667  6   0 2.5
    15057 "AT"  2 3.333333        0  1   0 2.5
     9811 "AT"  4       10       10  5   0   0
     6868 "AT"  5 6.666667 6.666667  5   0 2.5
    69241 "AT"  6 6.666667       10  2   5 2.5
     9804 "AT"  5 3.333333 3.333333  5   .   5
    49001 "AT"  4 3.333333 3.333333  4 2.5 2.5
    12892 "AT"  0        0        0  0   0   0
    end
    At the same time, I have a dataset that measures party positions on different issues which ultimately correspond to the ones of my first dataset, and parties nested in 14 countries. The id variable is a string var (party). Here is an example of it:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str15 party str3 iso_country float(lrscale_party Redistribution_party GenderIss_party Immigration_party Multiclt_party EU_party)
    "50PLUS"   "NL"  5.083333       4.4 4.6666665       5.5  6.666667  5.238095
    "A"        "DK"       2.5  2.846154  .5714286         2         2  2.692308
    "AGALEV"   "BE"  2.416667      2.25  .9090909      2.25 1.9166666  .6944442
    "AfD"      "DE"  9.238095         6  9.105263       9.9      9.95 8.4920635
    "BE"       "PT"      .875 1.4285715      1.25       1.5 1.8333334  5.416667
    "BNG"      "ES"  3.142857  2.916667       2.6 3.5555556       3.3 2.4242425
    "BREXIT"   "GB"       8.2     6.375  8.571428       8.6       7.5  8.541667
    "C"        "SE"  6.823529      6.75  .7647059 1.9411764  2.470588 1.5686274
    "CC"       "ES"  6.714286      5.75 4.3636365  6.545455      6.25  1.025641
    "CDA"      "NL"  6.846154         6  5.727273         7  6.833333 2.7777774
    "CDS_PP"   "PT"       8.5  6.857143      8.25         7  5.833333     1.875
    "CDU"      "DE"  5.857143  5.947369  5.105263         6      7.05 1.1904764
    "CDU"      "PT"      .875  .8571429     4.375     2.375 1.8333334  7.708333
    "CDV"      "BE"  5.583333         5  5.363636  5.666667      5.25  .6944442
    "CONS"     "GB"  7.117647  6.647059    3.9375  7.588235       6.5  9.117647
    "CSU"      "DE"  7.190476   6.31579  6.421052       7.7      8.45  2.192983
    "CU"       "NL"  5.076923  4.181818  6.363636 4.4615383       5.9         5
    "Cs"       "ES"       7.2  6.533333  4.133333  6.428571  6.642857  .4444448
    "D66"      "NL"  5.153846      5.75  1.909091         3       2.5 .12820482
    "DENK"     "NL" 4.3636365     3.125  6.222222 1.5555556  .6363636  3.888889
    "DF"       "DK"  6.928571  4.357143  5.428571         9  9.230769  8.333333
    "DLF"      "FR"         9       6.4       8.4  9.333333       9.6  9.285714
    "DS"       "IE"         3  2.833333       1.8      3.75  2.666667  3.333333
    "DieTier"  "DE" 2.3333333         .       2.5         0         3  3.333333
    "ECOLO"    "BE" 2.0833333 1.9166666         1         2      1.75  .6944442
    "EELV"     "FR"       2.5     2.375      .625     1.625 1.5714285 1.0416666
    "EHB"      "ES" 1.2857143 1.9166666         2 2.3636363       2.9 4.1666665
    "EL"       "DK"         1  .6428571  .8571429 2.4285715 2.5384614  7.619047
    "EL"       "GR"         9 3.5714285     9.625       9.5  9.714286     8.125
    "ERC"      "ES"       3.2         3 2.4285715 3.4285715         4 2.4444444
    "FDP"      "DE"  6.428571  8.578947 2.2631578      5.95  5.764706  2.063492
    "FF"       "IE"  5.428571  4.857143  4.714286  4.833333       5.6 1.4285716
    "FG"       "IE"  5.857143  5.857143 3.5714285 4.6666665         5 1.1904764
    "FI"       "FR"      1.25      .625         1         4  3.857143     6.875
    "FI"       "IT"  6.947369  6.888889  6.222222         7    7.4375  3.508772
    "FPO"      "AT"       9.1       5.6       8.4       9.8       9.9  7.833333
    "FdI"      "IT"  9.052631  5.529412  8.470589  9.842105  9.882353  8.421052
    "FvD"      "NL"  9.538462  8.666667       6.4  9.923077  9.916667  9.871795
    "GL"       "NL" 2.3076923 2.1818182 1.6363636 1.6153846       1.5  .8974361
    "GP"       "IE"  3.142857         3  2.142857  3.333333       2.6 1.9047618
    "GREEN"    "GB"         2     1.875  .7058824 1.7058823       2.5 .49019575
    "GRUNEN"   "DE"  3.238095  3.421053  .8421053       1.7       1.9  .3968255
    "Grune"    "AT"       2.5       2.5        .5       2.7       1.4  .8333333
    "I4C"      "IE"      3.25      3.75  2.666667         5       4.5  5.555555
    "IU"       "ES" 1.8666667       1.4 1.7857143  2.533333       3.2  2.857143
    "KD"       "FI"  6.857143  4.785714  8.428572  5.769231  6.357143  5.119047
    "KD"       "SE"  7.823529    6.3125  5.117647  7.823529  7.176471 1.9607846
    "KESK"     "FI"  5.857143  4.928571  5.357143  5.571429  6.142857 3.5714285
    "KF"       "DK"  7.071429  7.214286  4.142857       7.5  7.384615 2.1428568
    "KIDISO"   "GR"       4.2       3.6         2      3.75       2.8 1.3333336
    "KKE"      "GR" .22222222 .11111111      4.75  2.666667 3.7142856  9.814815
    "KOK"      "FI"  7.714286  6.857143  4.071429  4.428571  5.357143  .9523813
    "L"        "SE"  6.529412      6.25         1       4.5  4.470588  .1960786
    "LA"       "DK"         8  9.071428  2.642857  6.285714  5.692307  4.880952
    "LAB"      "GB" 1.9411764 1.5882353  1.235294  3.529412  3.142857         5
    "LIBDEM"   "GB" 4.2352943  3.470588  .9411765 2.5882354         3  .3921572
    "LINKE"    "DE" 1.4285715  .8947368 1.6666666       2.7  2.764706 3.8095236
    "LN"       "IT"  8.789474   7.31579  8.222222  9.947369  9.823529  8.859649
    "LR"       "FR"     7.875     7.375     7.625     7.875  8.142858       2.5
    "LREM"     "FR"  6.333333  5.833333       2.5  5.666667       5.6 .27777752
    "Lab"      "IE" 3.5714285 3.2857144 2.2857144  3.833333       3.4 1.4285716
    "M"        "SE"  7.705883    7.1875    2.8125         8  6.823529 1.5686274
    "M5S"      "IT"  4.777778  1.894737 2.9411764  6.555555  5.571429  5.877193
    "MP"       "SE"  3.294118         3  .4117647    1.3125  .8823529  3.137255
    "MR25"     "GR" 1.4285715         1  .6666667  .6666667  .8333333  3.958333
    "MoDem"    "FR"     6.125  5.857143      4.25     5.375  5.666667  .4761902
    "NB"       "DK"         9  8.785714  6.181818  9.642858  9.692307  8.690476
    "ND"       "GR"  7.111111  7.333333  7.222222  7.777778  7.888889 .18518527
    "NEOS"     "AT"       5.9       6.9       1.6       4.3       3.1  .5000003
    "OVP"      "AT"       6.9       6.2       6.2       8.6       8.1  3.333333
    "PAN"      "PT" 3.4285715       2.8  3.142857       3.2       2.5 4.7222223
    "PASOK"    "GR" 4.3333335  3.777778     2.875  4.777778 4.4444447 .18518527
    "PCF"      "FR"     1.125        .5     1.625      3.25 3.5714285  6.666667
    "PCP"      "PT"      .875  .8571429     4.375 2.2857144         2  7.083333
    "PD"       "IT"  3.210526  2.722222 2.3333333 3.0526316  2.705882  .3508774
    "PEV"      "PT" 1.1428572 1.1666666 3.5714285  2.142857         2  6.666667
    "PLAID"    "GB"  3.090909  3.090909       1.9 2.4444444     3.125 1.2820514
    "PNV"      "ES"         6  5.333333  4.142857  5.285714      5.25  .9999998
    "PPP"      "ES"  8.066667  7.266667  6.933333       7.6  7.866667  .8888888
    "PRL_MR"   "BE"       6.5  5.833333  2.727273  5.583333  4.916667  .8333333
    "PS"       "BE" 2.0833333 1.5833334         2  3.416667  3.083333 1.5277776
    "PS"       "FI"  7.571429  5.285714  8.071428  9.785714  9.785714  8.928572
    "PS"       "FR"         3      2.25      .875      2.75 3.2857144 1.4583334
    "PS"       "PT"     4.125  3.857143     2.875     3.875  3.166667  .4166667
    "PSC_CDH"  "BE" 4.5833335  3.916667 4.4545455       4.5  4.181818  .9722225
    "PSD"      "PT"     6.375  6.285714       6.5     5.375  5.333333  .4166667
    "PSOE"     "ES"       3.6       2.8         2 3.9333334 4.3333335   .333333
    "PVDA_PTB" "BE"  .3333333  .3333333 2.4444444 2.5454545       2.7  7.121212
    "PVV"      "NL"  8.692307  5.833333       4.8  9.923077  9.916667  9.487179
    "PVdD"     "NL" 2.3846154  3.111111     2.375  3.222222       3.8  7.272727
    "Pais"     "ES"  2.733333 2.3333333 1.6153846 2.2857144  2.642857 1.8888887
    "PdeCat"   "ES"  6.666667  6.214286  4.642857  5.357143  6.615385 3.1111114
    "Piraten"  "DE"  2.142857       2.5      .875      1.25       1.5  .8333333
    "Podemos"  "ES" 1.9333333 1.6666666 1.2666667 1.7333333 2.2666667 2.8888886
    "PvdA"     "NL" 3.6153846  2.833333  3.090909 4.1666665 4.4166665 1.8055558
    "RI"       "IE"         8      7.75       6.4       8.4       8.5  6.333333
    "RI"       "IT"    4.3125  5.714286     .5625 1.8666667 2.0714285  .4166667
    "RN"       "FR"      9.75         4      7.75     9.875        10     9.375
    "RV"       "DK"  5.071429  5.142857 1.1428572         2  2.692308 .11904796
    "SD"       "DK"         4  3.142857         3  6.857143  7.307693 3.2142854
    end

    The only variable the two datasets have in common is iso_country, which nonetheless is not unique identifier given the nested nature of both datasets.

    I would like to merge the party dataset into the individual dataset, thus generating for each respondent (idno) as many observations as there are parties in his/her country of origin. So that I will have eg. for idno=223 and iso_country=AT, 5 observations of the following form: 223SPO, 223FPO, 223OVP, 223BZO, 223GRUNE.

    I was trying to use merge and creating merging variables, but all attempts failed.

    Do you have any suggestions?

    Sincerely
    Mattia


  • #2
    This cannot be done using -merge-. This is a job for -joinby-. See -help joinby- for details.

    Comment


    • #3
      Dear Clyde,

      I used joinby iso_country and it worked perfectly.

      Thanks a lot!!!

      Sincerely
      Mattia

      Comment

      Working...
      X