Announcement

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

  • Difficulty to merge two datasets

    Hello,

    I tried various ways to merge these two datasets. I must be doing something wrong. Pls advise. Thanks



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str15 country float(year yield) byte inlist2
    "austria"   1990      8.7275 1
    "austria"   1991    8.546667 1
    "austria"   1992    8.139167 1
    "austria"   1993    6.704325 1
    "austria"   1994    7.025466 1
    "austria"   1995    7.135075 1
    "austria"   1996    6.321466 1
    "austria"   1997    5.682842 1
    "austria"   1998      4.7135 1
    "austria"   1999      4.6809 1
    "austria"   2000      5.5554 1
    "austria"   2001    5.080167 1
    "austria"   2002      4.9638 1
    "austria"   2003   4.1408668 1
    "austria"   2004    4.130892 1
    "austria"   2005    3.393733 1
    "austria"   2006   3.8008416 1
    "austria"   2007   4.2970834 1
    "austria"   2008    4.358525 1
    "austria"   2009    3.937633 1
    "austria"   2010   3.2263834 1
    "austria"   2011    3.319658 1
    "austria"   2012    2.371175 1
    "austria"   2013   2.0107584 1
    "austria"   2014   1.4872917 1
    "austria"   2015    .7470416 1
    "austria"   2016   .37538335 1
    "austria"   2017    .5832833 1
    "austria"   2018    .6861584 1
    "austria"   2019   .06293333 1
    "austria"   2020     -.22485 1
    "austria"   2021  -.08538333 1
    "austria"   2022     1.71115 1
    "australia" 1990       13.18 1
    "australia" 1991   10.690833 1
    "australia" 1992        9.22 1
    "australia" 1993    7.280833 1
    "australia" 1994    9.041667 1
    "australia" 1995    9.210896 1
    "australia" 1996    8.209207 1
    "australia" 1997    6.954918 1
    "australia" 1998     5.49297 1
    "australia" 1999    6.009312 1
    "australia" 2000    6.314569 1
    "australia" 2001    5.615264 1
    "australia" 2002    5.843958 1
    "australia" 2003    5.366776 1
    "australia" 2004    5.590775 1
    "australia" 2005    5.339973 1
    "australia" 2006    5.587872 1
    "australia" 2007    5.994521 1
    "australia" 2008    5.817944 1
    "australia" 2009    5.039512 1
    "australia" 2010    5.366042 1
    "australia" 2011    4.879583 1
    "australia" 2012   3.3791666 1
    "australia" 2013   3.6972916 1
    "australia" 2014    3.656667 1
    "australia" 2015    2.710833 1
    "australia" 2016   2.3366666 1
    "australia" 2017   2.6391666 1
    "australia" 2018    2.684167 1
    "australia" 2019   1.4933333 1
    "australia" 2020    .9208333 1
    "australia" 2021        1.48 1
    "australia" 2022    3.200833 1
    "belgium"   1990   10.006667 1
    "belgium"   1991    9.285833 1
    "belgium"   1992    8.653334 1
    "belgium"   1993    7.228333 1
    "belgium"   1994    7.750834 1
    "belgium"   1995    7.480834 1
    "belgium"   1996      6.4925 1
    "belgium"   1997    5.753334 1
    "belgium"   1998   4.7516665 1
    "belgium"   1999   4.7491665 1
    "belgium"   2000      5.5925 1
    "belgium"   2001    5.130833 1
    "belgium"   2002    4.986667 1
    "belgium"   2003   4.1808333 1
    "belgium"   2004      4.1525 1
    "belgium"   2005    3.428333 1
    "belgium"   2006       3.815 1
    "belgium"   2007   4.3283334 1
    "belgium"   2008      4.4175 1
    "belgium"   2009   3.9016666 1
    "belgium"   2010      3.4625 1
    "belgium"   2011    4.233333 1
    "belgium"   2012           3 1
    "belgium"   2013        2.41 1
    "belgium"   2014   1.7133334 1
    "belgium"   2015         .84 1
    "belgium"   2016    .4758333 1
    "belgium"   2017       .7225 1
    "belgium"   2018        .795 1
    "belgium"   2019   .19333333 1
    "belgium"   2020  -.14833333 1
    "belgium"   2021 -.014166667 1
    "belgium"   2022   1.7333333 1
    "canada"    1990   10.727077 1
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 580 observations
    Use the count() option to list more

    . use "C:\Users\chief\Desktop\dataset\covariates\oecdpan el_workversion2.dta"

    . datex
    command datex is unrecognized
    r(199);

    . dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str40 NAMES_STD str56 country str12 countrycode int ifscode float year double(cab gdp gdprate) float gdptri double inf float(pbal tot) double(pdebt forex) byte inlist2
    "australia"      "Australia"      "AUS" 193 1995   -5 9.68072e+11  3.887106294   .968072               4.628   .84 97.64 31.15581428 11339.6 1
    "austria"        "Austria"        "AUT" 122 1995 -2.9 2.42067e+11  2.667983531   .242067                2.25  -1.8 95.44 67.86475594   17867 1
    "belgium"        "Belgium"        "BEL" 124 1995  5.3 2.86238e+11   2.38475728   .286238               1.467  4.34     . 131.2866706   14680 1
    "canada"         "Canada"         "CAN" 156 1995  -.8 1.22203e+12  2.693976276   1.22203               2.168  4.24 97.08 101.6009979   12629 1
    "denmark"        "Denmark"        "DNK" 128 1995    1 1.52494e+12  3.027587267   1.52494               2.098  2.98 96.35     73.1364 10262.4 1
    "finland"        "Finland"        "FIN" 172 1995  3.8 1.37708e+11   4.21686747   .137708                .985 -2.09 93.38 55.15124456  9293.4 1
    "france"         "France"         "FRA" 132 1995   .5 1.59632e+12  2.106695253   1.59632               1.778 -2.03 95.62 55.40909958   23142 1
    "germany"        "Germany"        "DEU" 134 1995 -1.2 2.32834e+12  1.544146496   2.32834               1.724 -5.98 95.31 54.90243375   77794 1
    "greece"         "Greece"         "GRC" 174 1995 -2.3 1.51808e+11  2.099719737   .151808               8.937   .96 93.47 98.98994241   14611 1
    "ireland"        "Ireland"        "IRL" 178 1995  2.5 92081381000  9.634422073 .09208138               2.514  3.24 94.83  78.5219281  8177.5 1
    "italy"          "Italy"          "ITA" 136 1995  2.1 1.49935e+12  2.886836759   1.49935               5.244  4.11 95.04 119.3620869   32942 1
    "japan"          "Japan"          "JPN" 158 1995    2 4.58229e+14  2.630999616   458.229                 -.1    -1 95.45 92.52557832  172443 1
    "nerlands"       "Netherlands"    "NLD" 138 1995  5.7 4.69502e+11  3.116035986   .469502               1.923 -3.58 89.55 73.13797425 31059.6 1
    "new zealand"    "New Zealand"    "NZL" 196 1995   -4  1.4746e+11  4.722081044    .14746               3.755  9.87 95.53 43.46596501  3514.9 1
    "norway"         "Norway"         "NOR" 142 1995  3.2 2.05226e+12  4.155642995   2.05226               2.456     6 97.84 32.73774606 17221.6 1
    "portugal"       "Portugal"       "PRT" 182 1995  -.1 1.42678e+11   4.28278046   .142678               4.123   .55  92.8     62.2148   15315 1
    "spain"          "Spain"          "ESP" 184 1995 -1.3 7.16127e+11  2.757494046   .716127               4.675 -2.08 93.93 63.38419606 32490.6 1
    "sweden"         "Sweden"         "SWE" 144 1995  3.1 2.57969e+12  3.935192926   2.57969               2.528 -1.84 95.06 68.32512277   22939 1
    "switzerland"    "Switzerland"    "CHE" 146 1995  4.9 4.55775e+11   .480866449   .455775               1.799   .13 97.31 50.00768345   34685 1
    "united kingdom" "United Kingdom" "GBR" 112 1995  -.3 1.28206e+12  2.531670022   1.28206               2.675 -2.34  97.2 44.33890493 42857.1 1
    "australia"      "Australia"      "AUS" 193 1996 -3.5 1.00541e+12  3.856841227   1.00541               2.615  1.83  97.8 29.34345567 13966.6 1
    "austria"        "Austria"        "AUT" 122 1996 -2.8 2.47754e+11  2.349533852   .247754               1.844  -.13 95.61 67.80722447   21861 1
    "belgium"        "Belgium"        "BEL" 124 1996    5 2.90021e+11   1.32145093   .290021               2.059  4.44     . 129.0039573   15380 1
    "canada"         "Canada"         "CAN" 156 1996   .4 1.24181e+12  1.619172495   1.24181               1.571  6.32 97.21 101.7190018   18028 1
    "denmark"        "Denmark"        "DNK" 128 1996  1.6 1.56916e+12  2.900099769   1.56916               2.111  3.65 96.51     69.9283   13366 1
    "finland"        "Finland"        "FIN" 172 1996  3.5 1.42758e+11   3.66717983   .142758                .617   .81 93.68 55.30597651  6205.3 1
    "france"         "France"         "FRA" 132 1996  1.2 1.61887e+12  1.412993673   1.61887               2.005  -.46 95.91 58.02909851 23119.8 1
    "germany"        "Germany"        "DEU" 134 1996  -.7 2.34711e+12   .805822891   2.34711               1.446   .14 95.58 57.79392936   75803 1
    "greece"         "Greece"         "GRC" 174 1996 -3.5 1.56153e+11  2.862128802   .156153               8.196   2.1 93.89 101.3354426 17337.3 1
    "ireland"        "Ireland"        "IRL" 178 1996  2.7 98876641000  7.379624335 .09887664               1.693  4.39 95.17 69.83340271  7714.9 1
    "italy"          "Italy"          "ITA" 136 1996  2.9 1.51835e+12  1.266784802   1.51835               3.975  4.51 95.27 119.1094729   44064 1
    "japan"          "Japan"          "JPN" 158 1996  1.4 4.72589e+14  3.133870993   472.589                  .1 -1.59 95.75 98.05354002  207335 1
    "nerlands"       "Netherlands"    "NLD" 138 1996  4.8 4.85928e+11  3.498742121   .485928               2.017  3.41 90.34 71.38541893 24119.1 1
    "new zealand"    "New Zealand"    "NZL" 196 1996 -4.7 1.52792e+11   3.61567438   .152792               2.286  7.86 95.84 37.26051507  4778.5 1
    "norway"         "Norway"         "NOR" 142 1996  6.5 2.15545e+12  5.027961816   2.15545               1.259  8.75 97.85 28.44587849   20588 1
    "portugal"       "Portugal"       "PRT" 182 1996 -4.5 1.47678e+11  3.504247069   .147678               3.121   .28 93.21     63.3122   15359 1
    "spain"          "Spain"          "ESP" 184 1996  -.9  7.3518e+11  2.660561604    .73518               3.559  -.28 94.33 67.52610208   55879 1
    "sweden"         "Sweden"         "SWE" 144 1996  3.3 2.62043e+12  1.579456406   2.62043                .471  2.08 95.28 68.67886282   18172 1
    "switzerland"    "Switzerland"    "CHE" 146 1996  5.3 4.57923e+11   .471280021   .457923                .819   .18 97.42 51.34820347   36775 1
    "united kingdom" "United Kingdom" "GBR" 112 1996  -.2 1.30652e+12  1.907715788   1.30652               2.457   -.7 97.36 43.92312605 40608.1 1
    "australia"      "Australia"      "AUS" 193 1997 -2.9 1.04475e+12  3.912835473   1.04475  .22499999403953552  2.59 97.79 25.90581133 16099.5 1
    "austria"        "Austria"        "AUT" 122 1997 -2.5 2.52941e+11   2.09359939   .252941  1.3079999685287476  1.79  95.6  63.0558096   18605 1
    "belgium"        "Belgium"        "BEL" 124 1997  5.5 3.01023e+11   3.79365763   .301023  1.6269999742507935  5.43     . 124.2711991   14519 1
    "canada"         "Canada"         "CAN" 156 1997 -1.3 1.29496e+12  4.280040034   1.29496   1.621000051498413  8.57 97.19 96.31790161   15122 1
    "denmark"        "Denmark"        "DNK" 128 1997  1.3 1.62033e+12  3.260890158   1.62033   2.196000099182129  4.42 96.53     65.8482 18157.3 1
    "finland"        "Finland"        "FIN" 172 1997  4.8   1.518e+11  6.333795654     .1518  1.1950000524520874  2.84 93.66 52.21511276  7531.7 1
    "france"         "France"         "FRA" 132 1997  3.7 1.65669e+12  2.336296529   1.65669   1.222000002861023   .14 95.89 59.39720154 27096.9 1
    "germany"        "Germany"        "DEU" 134 1997  -.5 2.38917e+12  1.792160821   2.38917  1.8799999952316284   .64 95.57 58.86658338   69853 1
    "greece"         "Greece"         "GRC" 174 1997 -3.7 1.63155e+11   4.48419902   .163155   5.539000034332275  2.27 93.87 99.45167027 12441.1 1
    "ireland"        "Ireland"        "IRL" 178 1997  2.3 1.09775e+11  11.02176499   .109775   1.437000036239624  5.22 95.13 61.57838617  6019.7 1
    "italy"          "Italy"          "ITA" 136 1997  2.6 1.54614e+12  1.830212234   1.54614  2.0429999828338623  6.55 95.26 116.7821242   53431 1
    "japan"          "Japan"          "JPN" 158 1997  2.1 4.77226e+14   .981228732   477.226  1.7999999523162842  -.27 95.69  104.970938  207866 1
    "nerlands"       "Netherlands"    "NLD" 138 1997    6 5.06964e+11  4.329053405   .506964  2.1760001182556152  3.68 90.27 65.80561773 21880.8 1
    "new zealand"    "New Zealand"    "NZL" 196 1997 -5.2 1.55969e+11   2.07919702   .155969   1.187000036239624  5.67 95.82 34.64610827  3538.1 1
    "norway"         "Norway"         "NOR" 142 1997  5.9 2.26936e+12  5.284557749   2.26936  2.5810000896453857  9.69 97.85 25.75691731 18008.3 1
    "portugal"       "Portugal"       "PRT" 182 1997 -6.1 1.54177e+11  4.400867754   .154177  2.1619999408721924   .36 93.06     58.7155   15130 1
    "spain"          "Spain"          "ESP" 184 1997  -.7   7.624e+11  3.702494627     .7624  1.9709999561309814   .68 94.29  66.2062611 66022.6 1
    "sweden"         "Sweden"         "SWE" 144 1997  3.8 2.70089e+12  3.070526593   2.70089   .5180000066757202  3.49 95.25  67.4293578    9657 1
    "switzerland"    "Switzerland"    "CHE" 146 1997  8.4 4.68278e+11  2.261325239   .468278   .5220000147819519  -.81 97.42 53.68048521   36899 1
    "united kingdom" "United Kingdom" "GBR" 112 1997  -.1 1.36563e+12  4.524067098   1.36563   1.815999984741211  1.39 97.34 43.17250763   31589 1
    "australia"      "Australia"      "AUS" 193 1998 -4.9 1.09295e+12  4.613356892   1.09295   .8600000143051147  2.72  97.3   23.705104 13366.4 1
    "austria"        "Austria"        "AUT" 122 1998 -1.9   2.620e+11  3.581425815      .262   .9210000038146973  1.22  94.6 68.81875724   20918 1
    "belgium"        "Belgium"        "BEL" 124 1998  5.1 3.06929e+11  1.961808272   .306929   .9539999961853027  6.38     . 119.1858966   15763 1
    "canada"         "Canada"         "CAN" 156 1998 -1.3 1.34542e+12  3.896151829   1.34542   .9959999918937683  8.33 96.56 95.16459656   19911 1
    "denmark"        "Denmark"        "DNK" 128 1998  -.1 1.65627e+12  2.218158742   1.65627  1.8530000448226929  4.47 95.66     61.7518 13752.8 1
    "finland"        "Finland"        "FIN" 172 1998  4.7 1.60084e+11  5.457180501   .160084  1.3990000486373901  5.05  92.3  46.8267012  8508.2 1
    "france"         "France"         "FRA" 132 1998  3.7 1.71615e+12  3.588659425   1.71615   .5950000286102295   .68 94.98 59.54190063 38752.8 1
    "germany"        "Germany"        "DEU" 134 1998  -.7 2.43729e+12  2.013932785   2.43729   .9359999895095825  1.04 94.62 59.53470478   64133 1
    "greece"         "Greece"         "GRC" 174 1998 -2.6  1.6951e+11  3.894905028    .16951   4.765999794006348  1.43 92.55  97.4246186 17188.3 1
    "ireland"        "Ireland"        "IRL" 178 1998   .4 1.19398e+11   8.76612231   .119398   2.427000045776367  5.63 94.02 51.41388271  8621.9 1
    "italy"          "Italy"          "ITA" 136 1998  1.7 1.57413e+12  1.810615162   1.57413  1.9630000591278076  5.22  94.2 114.1269198   25447 1
    "japan"          "Japan"          "JPN" 158 1998  2.8 4.71164e+14 -1.270330495   471.164   .6000000238418579 -6.75 94.82 116.0199472  203215 1
    "nerlands"       "Netherlands"    "NLD" 138 1998    3 5.30609e+11   4.66391716   .530609  1.9850000143051147   3.8 88.25 62.76645659 17536.4 1
    "new zealand"    "New Zealand"    "NZL" 196 1998 -2.8 1.57309e+11   .859093008   .157309  1.2640000581741333  3.46 94.86 34.47722523  3184.6 1
    "norway"         "Norway"         "NOR" 142 1998  -.4 2.32992e+12  2.668686623   2.32992    2.25600004196167  5.36 97.24 23.57385989 14169.4 1
    "portugal"       "Portugal"       "PRT" 182 1998 -7.5  1.6159e+11  4.807962976    .16159  2.7160000801086426  -.39 91.43     55.6157   15067 1
    "spain"          "Spain"          "ESP" 184 1998 -1.7 7.95893e+11  4.393100735   .795893  1.8339999914169312  1.19 93.09 64.21157587   52490 1
    "sweden"         "Sweden"         "SWE" 144 1998  3.5 2.81735e+12  4.311836353   2.81735 -.13600000739097595  5.16 94.19 65.13073109 12419.5 1
    "switzerland"    "Switzerland"    "CHE" 146 1998  7.6 4.82467e+11   3.02996917   .482467 .017000000923871994   .07 96.82 55.95050873   38346 1
    "united kingdom" "United Kingdom" "GBR" 112 1998  -.2 1.40874e+12  3.157287339   1.40874  1.5579999685287476  3.36 96.75 40.84923238 29931.8 1
    "australia"      "Australia"      "AUS" 193 1999 -5.5 1.14712e+12  4.956324506   1.14712  1.4830000400543213   3.8 97.71 22.55713459 19507.4 1
    "austria"        "Austria"        "AUT" 122 1999 -2.3 2.71318e+11  3.556331247   .271318   .5619999766349792  1.08  95.1 61.10316575 14016.2 1
    "belgium"        "Belgium"        "BEL" 124 1999  7.8 3.17803e+11  3.542743464   .317803  1.1180000305175781  6.19 83.88 115.3628057  8377.4 1
    "canada"         "Canada"         "CAN" 156 1999   .2 1.41488e+12  5.162913624   1.41488  1.7350000143051147  9.25 96.95 91.36920166   24432 1
    "denmark"        "Denmark"        "DNK" 128 1999  2.5  1.7051e+12  2.948022161    1.7051  2.4790000915527344   5.4 96.08     58.1505   21145 1
    "finland"        "Finland"        "FIN" 172 1999  4.8 1.67095e+11  4.379575723   .167095   1.159000039100647  4.57 93.16  44.0109994  7292.4 1
    "france"         "France"         "FRA" 132 1999  4.4 1.77486e+12  3.421373799   1.77486   .5329999923706055  1.18 95.56 58.85100174 33933.4 1
    "germany"        "Germany"        "DEU" 134 1999 -1.4 2.48328e+12  1.887261154   2.48328   .5699999928474426  1.56 95.14 60.38737934 52661.1 1
    "greece"         "Greece"         "GRC" 174 1999 -3.6 1.74718e+11  3.072596691   .174718    2.63700008392334  1.79 93.52 98.90648708   17726 1
    "ireland"        "Ireland"        "IRL" 178 1999  -.1  1.3197e+11  10.52988626    .13197  1.6399999856948853  4.97 94.66  46.5701415  4869.1 1
    "italy"          "Italy"          "ITA" 136 1999   .8 1.59972e+12  1.625727599   1.59972   1.656000018119812  4.64 94.79 113.2895146 18707.7 1
    "japan"          "Japan"          "JPN" 158 1999  2.5  4.6959e+14  -.333929958    469.59 -.30000001192092896 -3.46 95.35 129.5011891  277708 1
    "nerlands"       "Netherlands"    "NLD" 138 1999  3.5  5.5732e+11  5.034048152    .55732   2.193000078201294  4.69 89.54 58.69655914  6286.8 1
    "new zealand"    "New Zealand"    "NZL" 196 1999   -5 1.65765e+11  5.375364566   .165765 -.10999999940395355  2.33 95.56  31.9992834  3332.8 1
    "norway"         "Norway"         "NOR" 142 1999  5.3  2.3782e+12  2.072047171    2.3782  2.3329999446868896  7.77 97.57 24.95949352 18393.6 1
    "portugal"       "Portugal"       "PRT" 182 1999 -8.9 1.67902e+11   3.90657744   .167902   2.303999900817871   .19 92.18     55.4056  8005.7 1
    "spain"          "Spain"          "ESP" 184 1999 -3.2 8.31633e+11  4.490553378   .831633   2.311000108718872  2.28 93.81 62.45856862 31329.5 1
    "sweden"         "Sweden"         "SWE" 144 1999  3.8 2.93701e+12  4.247184144   2.93701  .45399999618530273  4.56 94.96 60.06735998   13522 1
    "switzerland"    "Switzerland"    "CHE" 146 1999  8.5 4.90489e+11  1.662701284   .490489    .824999988079071  1.51 97.19 52.70069384   34176 1
    "united kingdom" "United Kingdom" "GBR" 112 1999 -2.1  1.4512e+12  3.014070476    1.4512   1.347000002861023  3.75 97.08  39.3364938 30086.8 1
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 560 observations
    Use the count() option to list more

    .

  • #2
    I'm not entirely sure what you want for the end result. But what looks to me like the most natural way to merge these two data sets is on country and year. One obstacle to doing that is that the variable country uses a different nomenclature in the two data sets, so that directly -merge-ing on country will give incorrect results. Rather, what is called country in dataset1 is a match for NAMES_STD in dataset 2. Well, that is easy enough to overcome:

    Code:
    use dataset1, clear
    clonevar NAMES_STD = country
    merge 1:1 NAMES_STD year using dataset2
    Notes:
    1. You might want to consider installing Rafal Raciborski's -kountry- package from SSC and just convert the names in both data sets to a common short code and then use that for the country part of the merge key.
    2. The code above retains all observations from both data sets. In the example data, however, matches only occur for years 1995 through 1999. You may want to remove unmatched observations from the results, or not.

    Comment


    • #3
      Hello Clyde. On your advice I used the kountry package. It worked!. I had to manually remove observations. Thank you so much.

      Comment

      Working...
      X