Announcement

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

  • #16
    Originally posted by Clyde Schechter View Post
    That's a simple change. Before reducing dataset1 to those observations that have the longest duration, we save a copy of the original data. Then we continue as before, and, at the very end, merge back the original data.

    Code:
    // FOR DATASET 1
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 ccode int ts long(edate date start end) str9 country int startyear byte(startmonth startday tap1 tap2) long(pr gr)
    "Australia" 1990 . . 3 6 "Australia" 1987 7 22 1 1 3 3
    "Australia" 1990 . . 6 5 "Australia" 1990 4 4 1 1 2 2
    "Australia" 1991 . . 6 5 "Australia" 1990 4 4 1 1 2 2
    "Australia" 1991 . . 5 3 "Australia" 1991 12 27 1 1 2 2
    "Australia" 1992 . . 5 3 "Australia" 1991 12 27 1 1 2 2
    "Australia" 1993 . . 5 3 "Australia" 1991 12 27 1 1 2 2
    "Australia" 1993 34041 199303 4 1 "Australia" 1993 3 24 1 1 1 1
    "Australia" 1994 . . 4 1 "Australia" 1993 3 24 1 1 1 1
    "Australia" 1995 . . 4 1 "Australia" 1993 3 24 1 1 1 1
    "Australia" 1996 35126 199603 4 1 "Australia" 1993 3 24 1 1 1 1
    "Australia" 1996 . . 1 2 "Australia" 1996 3 11 3 3 4 4
    "Australia" 1997 . . 1 2 "Australia" 1996 3 11 3 3 4 4
    "Australia" 1998 36071 199810 1 2 "Australia" 1996 3 11 3 3 4 4
    "Australia" 1998 . . 2 4 "Australia" 1998 10 21 2 2 5 5
    "Australia" 1999 . . 2 4 "Australia" 1998 10 21 2 2 5 5
    "Australia" 2000 . . 2 4 "Australia" 1998 10 21 2 2 5 5
    end
    label values start start
    label def start 1 "11/3/96", modify
    label def start 2 "21/10/98", modify
    label def start 3 "22/7/87", modify
    label def start 4 "24/3/93", modify
    label def start 5 "27/12/91", modify
    label def start 6 "4/4/90", modify
    label values end end
    label def end 1 "11/3/96", modify
    label def end 2 "21/10/98", modify
    label def end 3 "24/3/93", modify
    label def end 4 "26/11/01", modify
    label def end 5 "27/12/91", modify
    label def end 6 "4/4/90", modify
    label values pr pr
    label def pr 1 "-0,165", modify
    label def pr 2 "-14,9", modify
    label def pr 3 "-4,5", modify
    label def pr 4 "22,593", modify
    label def pr 5 "48,458", modify
    label values gr gr
    label def gr 1 "-0,165", modify
    label def gr 2 "-14,9", modify
    label def gr 3 "-4,5", modify
    label def gr 4 "22,593", modify
    label def gr 5 "48,458", modify
    
    // CREATE USABLE DATE VARIABLES
    drop start
    gen start = mdy(startmonth, startday, startyear)
    assert missing(start) == missing(startmonth, startday, startyear)
    format start %td
    decode end, gen(_end)
    drop end
    gen end = daily(_end, "DMY", 2020)
    assert missing(end) == missing(_end)
    format end %td
    drop _end startmonth startday startyear // NO LONGER NEEDED
    gen in_year_duration = min(end, mdy(12, 31, ts)) - max(start, mdy(1, 1, ts))
    assert !missing(in_year_duration)
    
    // KEEP THE OBSERVATION WITH LONGEST IN YEAR DURATION FOR EACH
    // COUNTRY AND YEAR
    gen long obs_no = _n
    tempfile original_dataset1
    save `original_dataset1'
    by country ts (in_year_duration), sort: keep if _n == _N
    tempfile reduced_dataset1
    save `reduced_dataset1'
    
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 Country str3 Symbol int year str6 YearSymbol long(rate1 rate2)
    "Australia" "AUS" 1990 "YR1990" 11 5
    "Australia" "AUS" 1991 "YR1991" 8 10
    "Australia" "AUS" 1992 "YR1992" 3 1
    "Australia" "AUS" 1993 "YR1993" 5 2
    "Australia" "AUS" 1994 "YR1994" 6 11
    "Australia" "AUS" 1995 "YR1995" 10 8
    "Australia" "AUS" 1996 "YR1996" 7 9
    "Australia" "AUS" 1997 "YR1997" 1 7
    "Australia" "AUS" 1998 "YR1998" 2 6
    "Australia" "AUS" 1999 "YR1999" 4 4
    "Australia" "AUS" 2000 "YR2000" 9 3
    end
    label values rate1 rate1
    label def rate1 1 "0,224887556", modify
    label def rate1 2 "0,86013463", modify
    label def rate1 3 "1,012231126", modify
    label def rate1 4 "1,483129403", modify
    label def rate1 5 "1,753653445", modify
    label def rate1 6 "1,969634797", modify
    label def rate1 7 "2,615384615", modify
    label def rate1 8 "3,17667537", modify
    label def rate1 9 "4,457435148", modify
    label def rate1 10 "4,6277666", modify
    label def rate1 11 "7,333021952", modify
    label values rate2 rate2
    label def rate2 1 "10,72879982", modify
    label def rate2 2 "10,87380028", modify
    label def rate2 3 "6,282599926", modify
    label def rate2 4 "6,872300148", modify
    label def rate2 5 "6,926000118", modify
    label def rate2 6 "7,675600052", modify
    label def rate2 7 "8,362199783", modify
    label def rate2 8 "8,46930027", modify
    label def rate2 9 "8,506199837", modify
    label def rate2 10 "9,579199791", modify
    label def rate2 11 "9,718700409", modify
    
    // CONVERT THE RATE VARIABLES TO ACTUAL NUMERIC RATES, NOT VALUE LABELED INTEGERS
    foreach v of varlist rate1 rate2 {
    decode `v', gen(_`v')
    drop `v'
    rename _`v' `v'
    destring `v', dpcomma replace
    }
    isid Country year
    rename Country country
    rename year ts
    tempfile dataset2
    save `dataset2'
    
    // LINK THE TWO DATA SETS AND BRING THE DATASET 1 VARIABLES OVER
    use `reduced_dataset1', clear
    merge 1:1 country ts using `dataset2', nogenerate
    merge 1:1 obs_no using `original_dataset1', assert(match using) nogenerate
    drop obs_no
    Changes from the previous code are shown in bold face.
    Dear Clyde,


    I am trying to implement the code on my main final datasets (after some changes imported to the first ones). The first I run the code on a provisional file with small time spam(not in panel) and just a couple of variables it worked .It did actually matched by the longest duration.However it dropped the all variables that are shorter in my first dataset ,which contain the political indexed.That’s not desired as I will lose important information.


    Please allow me to summarize what I need to do. I am having two datasets. The dataset 1 refers to the start day and end day of the actual dates of incumbents in office with some some sort of quality indexes(I provide only 2 here but there are more) while dataset2 refers to a number of macroeconomics data.(I provide only 2 series for one country here again ,but there are around 40 more of them).Consider that these data are only a small part of a panel for multiple years for multiple countries. I consider each year and country separately according to the actual duration in days of each incumbent in office for that particular year (365 or 366 days in a year depending on the leap year).
    Now I need to match up only observations referring to dastet2 (macro data) to the same country for that year (1990 to 1990 etc) without mixing other counties, to the longest duration of a government in office in that year, without dropping observations in the datset1(politics) for the same year. In some years more than one governments occurred .In the dataset1 provided there are indeed two or more observations for specific years (note year 1990 in the data provided for the years with more than one observation since there was a change in government and was coded as such. The macro data in dattaset2(yearly observations) must to be matched to the longest duration of governments in a year, without dropping the politics indexes for governments, others than the longest.
    Data below are for the case of Australia. In year duration was created by the code you provide. I have included only two of the indexes , but there are more.



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str15 ccode str14 oecdmember str16 eumember long(ts date) str15 country int startyear byte(startmonth startday) float(p g start) int time
    "Australia" "oecd member" "no eu membership" 1990      . "Australia" 1987  7 22    -4.5      -4.5 10064  11051
    "Australia" "oecd member" "no eu membership" 1990      . "Australia" 1990  4  4   -14.9     -14.9 11051  11683
    "Australia" "oecd member" "no eu membership" 1991      . "Australia" 1990  4  4   -14.9     -14.9 11051  11683
    "Australia" "oecd member" "no eu membership" 1991      . "Australia" 1991 12 27   -14.9     -14.9 11683  12136
    "Australia" "oecd member" "no eu membership" 1992      . "Australia" 1991 12 27   -14.9     -14.9 11683  12136
    "Australia" "oecd member" "no eu membership" 1993      . "Australia" 1991 12 27   -14.9     -14.9 11683  12136
    "Australia" "oecd member" "no eu membership" 1993 199303 "Australia" 1993  3 24   -.165     -.165 12136  13219
    "Australia" "oecd member" "no eu membership" 1994      . "Australia" 1993  3 24   -.165     -.165 12136  13219
    "Australia" "oecd member" "no eu membership" 1995      . "Australia" 1993  3 24   -.165     -.165 12136  13219
    "Australia" "oecd member" "no eu membership" 1996 199603 "Australia" 1993  3 24   -.165     -.165 12136  13219
    "Australia" "oecd member" "no eu membership" 1996      . "Australia" 1996  3 11  22.593    22.593 13219  14173
    "Australia" "oecd member" "no eu membership" 1997      . "Australia" 1996  3 11  22.593    22.593 13219  14173
    "Australia" "oecd member" "no eu membership" 1998 199810 "Australia" 1996  3 11  22.593    22.593 13219  14173
    "Australia" "oecd member" "no eu membership" 1998      . "Australia" 1998 10 21  48.458    48.458 14173 -21220
    "Australia" "oecd member" "no eu membership" 1999      . "Australia" 1998 10 21  48.458    48.458 14173 -21220
    "Australia" "oecd member" "no eu membership" 2000      . "Australia" 1998 10 21  48.458    48.458 14173 -21220
    "Australia" "oecd member" "no eu membership" 2001 200111 "Australia" 1998 10 21  48.458    48.458 14173 -21220
    "Australia" "oecd member" "no eu membership" 2001      . "Australia" 2001 11 26  33.333 30.905804 15305 -20155
    "Australia" "oecd member" "no eu membership" 2002      . "Australia" 2001 11 26  33.333 30.905804 15305 -20155
    "Australia" "oecd member" "no eu membership" 2003      . "Australia" 2001 11 26  33.333 30.905804 15305 -20155
    "Australia" "oecd member" "no eu membership" 2004      . "Australia" 2001 11 26  33.333 30.905804 15305 -20155
    "Australia" "oecd member" "no eu membership" 2004 200410 "Australia" 2004 10 26  31.889  34.52776 16370 -19022
    "Australia" "oecd member" "no eu membership" 2005      . "Australia" 2004 10 26  31.889  34.52776 16370 -19022
    "Australia" "oecd member" "no eu membership" 2006      . "Australia" 2004 10 26  31.889  34.52776 16370 -19022
    "Australia" "oecd member" "no eu membership" 2007      . "Australia" 2004 10 26  31.889  34.52776 16370 -19022
    "Australia" "oecd member" "no eu membership" 2007 200711 "Australia" 2007 12  3   5.674     5.674 17503 -18088
    "Australia" "oecd member" "no eu membership" 2008      . "Australia" 2007 12  3   5.674     5.674 17503 -18088
    "Australia" "oecd member" "no eu membership" 2009      . "Australia" 2007 12  3   5.674     5.674 17503 -18088
    "Australia" "oecd member" "no eu membership" 2010      . "Australia" 2007 12  3   5.674     5.674 17503 -18088
    "Australia" "oecd member" "no eu membership" 2010 201008 "Australia" 2010  6 24   5.674     5.674 18437 -18006
    "Australia" "oecd member" "no eu membership" 2010      . "Australia" 2010  9 14 -34.113   -34.113 18519 -16989
    "Australia" "oecd member" "no eu membership" 2011      . "Australia" 2010  9 14 -34.113   -34.113 18519 -16989
    "Australia" "oecd member" "no eu membership" 2012      . "Australia" 2010  9 14 -34.113   -34.113 18519 -16989
    "Australia" "oecd member" "no eu membership" 2013      . "Australia" 2010  9 14 -34.113   -34.113 18519 -16989
    "Australia" "oecd member" "no eu membership" 2013 201309 "Australia" 2013  6 27 -34.113   -34.113 19536 -16906
    "Australia" "oecd member" "no eu membership" 2013      . "Australia" 2013  9 18  22.975 18.485498 19619 -16179
    "Australia" "oecd member" "no eu membership" 2014      . "Australia" 2013  9 18  22.975 18.485498 19619 -16179
    "Australia" "oecd member" "no eu membership" 2015      . "Australia" 2013  9 18  22.975 18.485498 19619 -16179
    "Australia" "oecd member" "no eu membership" 2015      . "Australia" 2013  9 15  22.975 18.485498 19616 -15105
    "Australia" "oecd member" "no eu membership" 2016 201607 "Australia" 2013  9 15  22.975 18.485498 19616 -15105
    "Australia" "oecd member" "no eu membership" 2016      . "Australia" 2013  9 15   12.24         . 19616 -15105
    "Australia" "oecd member" "no eu membership" 2017      . "Australia" 2013  9 15   12.24         . 19616 -15105
    "Australia" "oecd member" "no eu membership" 2018      . "Australia" 2013  9 15   12.24         . 19616 -15105
    "Australia" "oecd member" "no eu membership" 2018      . "Australia" 2013  8 24   12.24         . 19594 -14976
    end
    format %td start
    ------------------ copy up to and including the previous line ------------------




    ,

    And here is a small sample of my macro data of one country , including a just a few of the macro series



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str48 country str3 countrycode int ts str6 timecode float(growth1 growth2) str7 gb1 str6(gb2 gb3) str7 d byte notnumeric long(gb1b gb2b gb3b d1)
    "Australia" "AUS" 1990 "YR1990" -.25998396    .8315406 "7,924" "0,0965" "5,648" "16.445" 1 674  26 711  152
    "Australia" "AUS" 1991 "YR1991"  1.3334774   2.1083677 "8,777" "0,0864" "3,078" "21.650" 1 705  15 627  207
    "Australia" "AUS" 1992 "YR1992"  3.2923484   3.4185865 "8,553" "0,1099" "1,402" "27.678" 1 697  47 467  281
    "Australia" "AUS" 1993 "YR1993"   3.715279   2.6728065 "7,368" "0,1498" "1,779" "30.721" 1 656 133 517  327
    "Australia" "AUS" 1994 "YR1994"  1.6375794   1.2805543 "5,84"  "0,1489" "1,133" "31.782" 1 565 130 427  336
    "Australia" "AUS" 1995 "YR1995"  -.9972533   -.9632453 "7,754" "0,2159" "2,393" "31.234" 1 664 252 583  332
    "Australia" "AUS" 1996 "YR1996"   2.770518    3.154481 "6,368" "0,1598" "2,206" "29.422" 1 600 159 562  310
    "Australia" "AUS" 1997 "YR1997"   3.889898   3.9895246 "5,838" "0,1703" "1,387" "25.959" 1 564 180 465  262
    "Australia" "AUS" 1998 "YR1998"  2.9572344   3.7896104 "5,414" "0,1701" "0,726" "23.754" 1 518 179 358  240
    "Australia" "AUS" 1999 "YR1999"   2.561527    2.339495 "4,035" "0,1985" "1,22"  "22.595" 1 368 221 440  221
    "Australia" "AUS" 2000 "YR2000"   .3891381   .50802076 "5,668" "0,1953" "1,461" "19.552" 1 550 216 475  187
    "Australia" "AUS" 2001 "YR2001"  1.1405312   3.1164474 "5,152" "0,1514" "0,624" "17.170" 1 489 137 342  158
    "Australia" "AUS" 2002 "YR2002"   2.158647   2.3763344 "5,171" "0,1963" "1,053" "15.051" 1 493 217 414  144
    "Australia" "AUS" 2003 "YR2003"   .3566159    .7994667 "4,268" "0,1937" "0,971" "13.221" 1 401 212 397  110
    "Australia" "AUS" 2004 "YR2004"  2.7113535    2.782252 "4,361" "0,2044" "1,299" "11.948" 1 419 228 453   70
    "Australia" "AUS" 2005 "YR2005"  -.5891249   -.4728382 "3,639" "0,1525" "1,696" "10.891" 1 311 139 502    6
    "Australia" "AUS" 2006 "YR2006"   .4218182   1.0691733 "3,34"  "0,1278" "1,865" "9.967"  1 275  83 526 1037
    "Australia" "AUS" 2007 "YR2007"  1.1622334   1.1005231 "3,99"  "0,1159" "1,916" "9.687"  1 359  58 532 1034
    "Australia" "AUS" 2008 "YR2008" -.08318187  .003607297 "4,438" "0,1276" "2,003" "11.753" 1 429  82 548   69
    "Australia" "AUS" 2009 "YR2009"  1.1294768    2.830966 "3,871" "0,257"  "1,046" "16.654" 1 341 283 413  153
    "Australia" "AUS" 2010 "YR2010"   .3700751 -.070456676 "3,88"  "0,2605" "2,337" "20.397" 1 342 287 577  193
    "Australia" "AUS" 2011 "YR2011"   1.048757   1.0357703 "3,451" "0,1435" "2,595" "24.065" 1 288 120 598  250
    "Australia" "AUS" 2012 "YR2012"   2.587376    2.995731 "3,194" "0,2031" "1,899" "27.540" 1 261 227 529  279
    "Australia" "AUS" 2013 "YR2013"  1.1289984   1.2830683 "1,754" "0,295"  "1,98"  "30.498" 1 135 310 537  323
    "Australia" "AUS" 2014 "YR2014"  1.6469382    1.930875 "2,279" "0,2662" "2,3"   "34.025" 1 215 292 572  360
    "Australia" "AUS" 2015 "YR2015"   .3492132   .35471275 "0,711" "0,2123" "2,289" "37.690" 1  60 246 570  420
    "Australia" "AUS" 2016 "YR2016"  1.0832386   1.7193124 "0,891" "0,3553" "2,255" "40.458" 1  76 342 566  479
    "Australia" "AUS" 2017 "YR2017" .036598407  -.01947236 "0,414" "0,3673" "2,588" "41.066" 1  36 346 597  492
    "Australia" "AUS" 2018 "YR2018"   .1107098    .7712589 "0,583" "0,2131" "2,248" "41.369" 1  49 249 565  496
    end
    label values gb1b TR10y_yield
    label def TR10y_yield 36 "0,414", modify
    label def TR10y_yield 49 "0,583", modify
    label def TR10y_yield 60 "0,711", modify
    label def TR10y_yield 76 "0,891", modify
    label def TR10y_yield 135 "1,754", modify
    label def TR10y_yield 215 "2,279", modify
    label def TR10y_yield 261 "3,194", modify
    label def TR10y_yield 275 "3,34", modify
    label def TR10y_yield 288 "3,451", modify
    label def TR10y_yield 311 "3,639", modify
    label def TR10y_yield 341 "3,871", modify
    label def TR10y_yield 342 "3,88", modify
    label def TR10y_yield 359 "3,99", modify
    label def TR10y_yield 368 "4,035", modify
    label def TR10y_yield 401 "4,268", modify
    label def TR10y_yield 419 "4,361", modify
    label def TR10y_yield 429 "4,438", modify
    label def TR10y_yield 489 "5,152", modify
    label def TR10y_yield 493 "5,171", modify
    label def TR10y_yield 518 "5,414", modify
    label def TR10y_yield 550 "5,668", modify
    label def TR10y_yield 564 "5,838", modify
    label def TR10y_yield 565 "5,84", modify
    label def TR10y_yield 600 "6,368", modify
    label def TR10y_yield 656 "7,368", modify
    label def TR10y_yield 664 "7,754", modify
    label def TR10y_yield 674 "7,924", modify
    label def TR10y_yield 697 "8,553", modify
    label def TR10y_yield 705 "8,777", modify
    label values gb2b TR10y_volatility
    label def TR10y_volatility 15 "0,0864", modify
    label def TR10y_volatility 26 "0,0965", modify
    label def TR10y_volatility 47 "0,1099", modify
    label def TR10y_volatility 58 "0,1159", modify
    label def TR10y_volatility 82 "0,1276", modify
    label def TR10y_volatility 83 "0,1278", modify
    label def TR10y_volatility 120 "0,1435", modify
    label def TR10y_volatility 130 "0,1489", modify
    label def TR10y_volatility 133 "0,1498", modify
    label def TR10y_volatility 137 "0,1514", modify
    label def TR10y_volatility 139 "0,1525", modify
    label def TR10y_volatility 159 "0,1598", modify
    label def TR10y_volatility 179 "0,1701", modify
    label def TR10y_volatility 180 "0,1703", modify
    label def TR10y_volatility 212 "0,1937", modify
    label def TR10y_volatility 216 "0,1953", modify
    label def TR10y_volatility 217 "0,1963", modify
    label def TR10y_volatility 221 "0,1985", modify
    label def TR10y_volatility 227 "0,2031", modify
    label def TR10y_volatility 228 "0,2044", modify
    label def TR10y_volatility 246 "0,2123", modify
    label def TR10y_volatility 249 "0,2131", modify
    label def TR10y_volatility 252 "0,2159", modify
    label def TR10y_volatility 283 "0,257", modify
    label def TR10y_volatility 287 "0,2605", modify
    label def TR10y_volatility 292 "0,2662", modify
    label def TR10y_volatility 310 "0,295", modify
    label def TR10y_volatility 342 "0,3553", modify
    label def TR10y_volatility 346 "0,3673", modify
    label values gb3b spreads
    label def spreads 342 "0,624", modify
    label def spreads 358 "0,726", modify
    label def spreads 397 "0,971", modify
    label def spreads 413 "1,046", modify
    label def spreads 414 "1,053", modify
    label def spreads 427 "1,133", modify
    label def spreads 440 "1,22", modify
    label def spreads 453 "1,299", modify
    label def spreads 465 "1,387", modify
    label def spreads 467 "1,402", modify
    label def spreads 475 "1,461", modify
    label def spreads 502 "1,696", modify
    label def spreads 517 "1,779", modify
    label def spreads 526 "1,865", modify
    label def spreads 529 "1,899", modify
    label def spreads 532 "1,916", modify
    label def spreads 537 "1,98", modify
    label def spreads 548 "2,003", modify
    label def spreads 562 "2,206", modify
    label def spreads 565 "2,248", modify
    label def spreads 566 "2,255", modify
    label def spreads 570 "2,289", modify
    label def spreads 572 "2,3", modify
    label def spreads 577 "2,337", modify
    label def spreads 583 "2,393", modify
    label def spreads 597 "2,588", modify
    label def spreads 598 "2,595", modify
    label def spreads 627 "3,078", modify
    label def spreads 711 "5,648", modify
    label values d1 debt
    label def debt 6 "10.891", modify
    label def debt 69 "11.753", modify
    label def debt 70 "11.948", modify
    label def debt 110 "13.221", modify
    label def debt 144 "15.051", modify
    label def debt 152 "16.445", modify
    label def debt 153 "16.654", modify
    label def debt 158 "17.170", modify
    label def debt 187 "19.552", modify
    label def debt 193 "20.397", modify
    label def debt 207 "21.650", modify
    label def debt 221 "22.595", modify
    label def debt 240 "23.754", modify
    label def debt 250 "24.065", modify
    label def debt 262 "25.959", modify
    label def debt 279 "27.540", modify
    label def debt 281 "27.678", modify
    label def debt 310 "29.422", modify
    label def debt 323 "30.498", modify
    label def debt 327 "30.721", modify
    label def debt 332 "31.234", modify
    label def debt 336 "31.782", modify
    label def debt 360 "34.025", modify
    label def debt 420 "37.690", modify
    label def debt 479 "40.458", modify
    label def debt 492 "41.066", modify
    label def debt 496 "41.369", modify
    label def debt 1034 "9.687", modify
    label def debt 1037 "9.967", modify
    ------------------ copy up to and including the previous line ------------------


    Now when I run the code the first time I obtained the following



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 oecdmember str16 eumember long(ts edate date) str48 country int startyear byte(startmonth startday) float(p g start) long end float(in_dur in_year_duration) str3 countrycode str6 timecode float(growth1 growth2 reexr) byte(notnumeric _merge)
    "oecd member" "no eu membership" 1990     .      . "Australia" 1990  4  4   -14.9     -14.9 11051 11683 271 271 "AUS" "YR1990" -.25998396    .8315406  86.5236 1 3
    "oecd member" "no eu membership" 1991     .      . "Australia" 1990  4  4   -14.9     -14.9 11051 11683 360 360 "AUS" "YR1991"  1.3334774   2.1083677 85.14064 1 3
    "oecd member" "no eu membership" 1992     .      . "Australia" 1991 12 27   -14.9     -14.9 11683 12136 365 365 "AUS" "YR1992"  3.2923484   3.4185865 76.92034 1 3
    "oecd member" "no eu membership" 1993 34041 199303 "Australia" 1993  3 24   -.165     -.165 12136 13219 282 282 "AUS" "YR1993"   3.715279   2.6728065 71.60689 1 3
    "oecd member" "no eu membership" 1994     .      . "Australia" 1993  3 24   -.165     -.165 12136 13219 364 364 "AUS" "YR1994"  1.6375794   1.2805543 75.18513 1 3
    "oecd member" "no eu membership" 1995     .      . "Australia" 1993  3 24   -.165     -.165 12136 13219 364 364 "AUS" "YR1995"  -.9972533   -.9632453 73.50241 1 3
    "oecd member" "no eu membership" 1996     .      . "Australia" 1996  3 11  22.593    22.593 13219 14173 295 295 "AUS" "YR1996"   2.770518    3.154481 80.38114 1 3
    "oecd member" "no eu membership" 1997     .      . "Australia" 1996  3 11  22.593    22.593 13219 14173 364 364 "AUS" "YR1997"   3.889898   3.9895246 79.97285 1 3
    "oecd member" "no eu membership" 1998 36071 199810 "Australia" 1996  3 11  22.593    22.593 13219 14173 293 293 "AUS" "YR1998"  2.9572344   3.7896104 73.25237 1 3
    "oecd member" "no eu membership" 1999     .      . "Australia" 1998 10 21  48.458    48.458 14173 15305 364 364 "AUS" "YR1999"   2.561527    2.339495 73.50773 1 3
    "oecd member" "no eu membership" 2000     .      . "Australia" 1998 10 21  48.458    48.458 14173 15305 365 365 "AUS" "YR2000"   .3891381   .50802076 70.44536 1 3
    "oecd member" "no eu membership" 2001 37205 200111 "Australia" 1998 10 21  48.458    48.458 14173 15305 329 329 "AUS" "YR2001"  1.1405312   3.1164474 67.56935 1 3
    "oecd member" "no eu membership" 2002     .      . "Australia" 2001 11 26  33.333 30.905804 15305 16370 364 364 "AUS" "YR2002"   2.158647   2.3763344 70.85816 1 3
    "oecd member" "no eu membership" 2003     .      . "Australia" 2001 11 26  33.333 30.905804 15305 16370 364 364 "AUS" "YR2003"   .3566159    .7994667 79.37099 1 3
    "oecd member" "no eu membership" 2004     .      . "Australia" 2001 11 26  33.333 30.905804 15305 16370 299 299 "AUS" "YR2004"  2.7113535    2.782252 85.42181 1 3
    "oecd member" "no eu membership" 2005     .      . "Australia" 2004 10 26  31.889  34.52776 16370 17503 364 364 "AUS" "YR2005"  -.5891249   -.4728382 87.96766 1 3
    "oecd member" "no eu membership" 2006     .      . "Australia" 2004 10 26  31.889  34.52776 16370 17503 364 364 "AUS" "YR2006"   .4218182   1.0691733 87.24076 1 3
    "oecd member" "no eu membership" 2007     .      . "Australia" 2004 10 26  31.889  34.52776 16370 17503 336 336 "AUS" "YR2007"  1.1622334   1.1005231 92.33583 1 3
    "oecd member" "no eu membership" 2008     .      . "Australia" 2007 12  3   5.674     5.674 17503 18437 365 365 "AUS" "YR2008" -.08318187  .003607297  90.3757 1 3
    "oecd member" "no eu membership" 2009     .      . "Australia" 2007 12  3   5.674     5.674 17503 18437 364 364 "AUS" "YR2009"  1.1294768    2.830966 87.59603 1 3
    "oecd member" "no eu membership" 2010     .      . "Australia" 2007 12  3   5.674     5.674 17503 18437 174 174 "AUS" "YR2010"   .3700751 -.070456676      100 1 3
    "oecd member" "no eu membership" 2011     .      . "Australia" 2010  9 14 -34.113   -34.113 18519 19536 364 364 "AUS" "YR2011"   1.048757   1.0357703  106.966 1 3
    "oecd member" "no eu membership" 2012     .      . "Australia" 2010  9 14 -34.113   -34.113 18519 19536 365 365 "AUS" "YR2012"   2.587376    2.995731  109.022 1 3
    "oecd member" "no eu membership" 2013     .      . "Australia" 2010  9 14 -34.113   -34.113 18519 19536 177 177 "AUS" "YR2013"  1.1289984   1.2830683 103.4509 1 3
    "oecd member" "no eu membership" 2014     .      . "Australia" 2013  9 18  22.975 18.485498 19619 20346 364 364 "AUS" "YR2014"  1.6469382    1.930875 98.32265 1 3
    "oecd member" "no eu membership" 2015     .      . "Australia" 2013  9 15  22.975 18.485498 19616 21420 364 364 "AUS" "YR2015"   .3492132   .35471275 89.83605 1 3
    "oecd member" "no eu membership" 2016     .      . "Australia" 2013  9 15   12.24         . 19616 21420 365 365 "AUS" "YR2016"  1.0832386   1.7193124 90.90352 1 3
    "oecd member" "no eu membership" 2017     .      . "Australia" 2013  9 15   12.24         . 19616 21420 364 364 "AUS" "YR2017" .036598407  -.01947236 93.67592 1 3
    "oecd member" "no eu membership" 2018     .      . "Australia" 2013  8 24   12.24         . 19594 21549 364 364 "AUS" "YR2018"   .1107098    .7712589 89.92953 1 3
    end
    format %td start
    format %td end
    label values _merge _merge
    label def _merge 3 "matched (3)", modify
    ------------------ copy up to and including the previous line ------------------


    We can easily see that indeed has merged to the longest in year duration of governments duration but has dropped all the observation for the political indexes in the first dataset when more than one governments occurred. This is not desired. They should stay there without being associated to the variables of the second dataset. In order to help better understand I paste the form the dataset should have created in excel. The variables and the values of the data might not be the same with the datasets but does not matter. This is just for example purposes. See the cases for 1993 and 1996




    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 ccode str11 oecdmember str16 eumember int ts long(edate date) str8(start end) str9 country int startyear byte(startmonth startday) float(p g) str9 countryname str3 countrycode int time str6 timecode float(growth growth2b)
    "Australia" "oecd member" "no eu membership" 1990     .      . "22/7/87"  "4/4/90"   "Australia" 1987  7 22   -4.5      -4.5 ""          ""       . ""               .         .
    "Australia" "oecd member" "no eu membership" 1990     .      . "4/4/90"   "27/12/91" "Australia" 1990  4  4  -14.9     -14.9 "Australia" "AUS" 1990 "YR1990"  -.259984  .8315406
    "Australia" "oecd member" "no eu membership" 1991     .      . "4/4/90"   "27/12/91" "Australia" 1990  4  4  -14.9     -14.9 ""          ""       . ""               .         .
    "Australia" "oecd member" "no eu membership" 1991     .      . "27/12/91" "24/3/93"  "Australia" 1991 12 27  -14.9     -14.9 "Australia" "AUS" 1991 "YR1991" 1.3334774 2.1083677
    "Australia" "oecd member" "no eu membership" 1992     .      . "27/12/91" "24/3/93"  "Australia" 1991 12 27  -14.9     -14.9 "Australia" "AUS" 1992 "YR1992" 3.2923484 3.4185865
    "Australia" "oecd member" "no eu membership" 1993     .      . "27/12/91" "24/3/93"  "Australia" 1991 12 27  -14.9     -14.9 ""          ""       . ""               .         .
    "Australia" "oecd member" "no eu membership" 1993 34041 199303 "24/3/93"  "11/3/96"  "Australia" 1993  3 24  -.165     -.165 "Australia" "AUS" 1993 "YR1993"  3.715279 2.6728065
    "Australia" "oecd member" "no eu membership" 1994     .      . "24/3/93"  "11/3/96"  "Australia" 1993  3 24  -.165     -.165 "Australia" "AUS" 1994 "YR1994" 1.6375794 1.2805543
    "Australia" "oecd member" "no eu membership" 1995     .      . "24/3/93"  "11/3/96"  "Australia" 1993  3 24  -.165     -.165 "Australia" "AUS" 1995 "YR1995" -.9972533 -.9632453
    "Australia" "oecd member" "no eu membership" 1996 35126 199603 "24/3/93"  "11/3/96"  "Australia" 1993  3 24  -.165     -.165 ""          ""       . ""               .         .
    "Australia" "oecd member" "no eu membership" 1996     .      . "11/3/96"  "21/10/98" "Australia" 1996  3 11 22.593    22.593 "Australia" "AUS" 1996 "YR1996"  2.770518  3.154481
    "Australia" "oecd member" "no eu membership" 1997     .      . "11/3/96"  "21/10/98" "Australia" 1996  3 11 22.593    22.593 "Australia" "AUS" 1997 "YR1997"  3.889898 3.9895246
    "Australia" "oecd member" "no eu membership" 1998 36071 199810 "11/3/96"  "21/10/98" "Australia" 1996  3 11 22.593    22.593 "Australia" "AUS" 1998 "YR1998" 2.9572344 3.7896104
    "Australia" "oecd member" "no eu membership" 1998     .      . "21/10/98" "26/11/01" "Australia" 1998 10 21 48.458    48.458 ""          ""       . ""               .         .
    "Australia" "oecd member" "no eu membership" 1999     .      . "21/10/98" "26/11/01" "Australia" 1998 10 21 48.458    48.458 "Australia" "AUS" 1999 "YR1999"  2.561527  2.339495
    "Australia" "oecd member" "no eu membership" 2000     .      . "21/10/98" "26/11/01" "Australia" 1998 10 21 48.458    48.458 "Australia" "AUS" 2000 "YR2000"  .3891381 .50802076
    "Australia" "oecd member" "no eu membership" 2001 37205 200111 "21/10/98" "26/11/01" "Australia" 1998 10 21 48.458    48.458 "Australia" "AUS" 2001 "YR2001" 1.1405312 3.1164474
    "Australia" "oecd member" "no eu membership" 2001     .      . "26/11/01" "26/10/04" "Australia" 2001 11 26 33.333 30.905804 ""          ""       . ""               .         .
    "Australia" "oecd member" "no eu membership" 2002     .      . "26/11/01" "26/10/04" "Australia" 2001 11 26 33.333 30.905804 "Australia" "AUS" 2002 "YR2002"  2.158647 2.3763344
    "Australia" "oecd member" "no eu membership" 2003     .      . "26/11/01" "26/10/04" "Australia" 2001 11 26 33.333 30.905804 "Australia" "AUS" 2003 "YR2003"  .3566159  .7994667
    "Australia" "oecd member" "no eu membership" 2004     .      . "26/11/01" "26/10/04" "Australia" 2001 11 26 33.333 30.905804 "Australia" "AUS" 2004 "YR2004" 2.7113535  2.782252
    "Australia" "oecd member" "no eu membership" 2004 38269 200410 "26/10/04" "3/12/07"  "Australia" 2004 10 26 31.889  34.52776 ""          ""       . ""               .         .
    "Australia" "oecd member" "no eu membership" 2005     .      . "26/10/04" "3/12/07"  "Australia" 2004 10 26 31.889  34.52776 "Australia" "AUS" 2005 "YR2005" -.5891249 -.4728382
    "Australia" "oecd member" "no eu membership" 2006     .      . "26/10/04" "3/12/07"  "Australia" 2004 10 26 31.889  34.52776 "Australia" "AUS" 2006 "YR2006"  .4218182 1.0691733
    end
    ------------------ copy up to and including the previous line ------------------


    Please note also in the second dataset there are some data coded as long. I tried to convert them in real numbers, first by using both the destring and decode commands and then the program you wrote above,
    Code:
    foreach v of varlist myvars1 myvar2 myvar3 myvar4 {
    decode `v', gen(_`v')
    drop `v'
    rename _`v' `v'
    destring `v', dpcomma replace
    }
    isid country ts
    nonetheless I got errors stating that contains nonnumeric characters; no replace.


    I would appreciate any further help you can provide. I am already indebted to you

    Regards,
    Mario Ferri




    Comment


    • #17
      Your new example dataset 1 does not contain any end dates for the governments, so I can't do anything with this.

      I will need a workable example dataset 1 and example dataset 2 to troubleshoot this. When I reread the code, I don't see any reason why the end result does not include the additional governments. And when I ran it with the original example data, it did include those. So something is different about your new data sets. But unless I can actually run the code with them, there is nothing I can do to fix the problem.

      So post back with example data from dataset 1 and dataset 2. Be sure that the examples you post exhibit the problem you are encountering when you run my original code, and make sure that my original code runs in them without error messages! For that to happen, the examples must include all the variables mentioned in the code. You can include additional variables showing the macro indicators and quality indices if you like, but really they are not relevant to getting this code to work. (If you can't get it to run without error messages, show the output, including error messages that you are getting from Stata.)

      foreach v of varlist myvars1 myvar2 myvar3 myvar4 {
      decode `v', gen(_`v')
      drop `v'
      rename _`v' `v'
      destring `v', dpcomma replace
      }



      The error messsages you are getting tells me that the variables myvars* contain illicit characters that are not allowed in numbers. But as you have provided no example data for these variables, again, there is nothing I can do to help you. The best advice I can give you is to review these variables and see if you can identify, by eye, the offending observations. If you can't spot them by eye, run
      Code:
      foreach v of varlist myvars1 myvar2 myvar3 myvar4 {
      decode `v', gen(_`v')
      replace _`v' = subinstr(_`v', ",", ".", .)
      tab _`v' if missing(real(_`v'))
      }
      The output from this will show you the offending values of these variables. Then you can go back through the data management that created these problems and fix them. This is clearly a problem with your data, not a problem with the code.

      Last edited by Clyde Schechter; 09 Feb 2020, 15:15.

      Comment


      • #18
        Originally posted by Clyde Schechter View Post
        Your new example dataset 1 does not contain any end dates for the governments, so I can't do anything with this.

        I will need a workable example dataset 1 and example dataset 2 to troubleshoot this. When I reread the code, I don't see any reason why the end result does not include the additional governments. And when I ran it with the original example data, it did include those. So something is different about your new data sets. But unless I can actually run the code with them, there is nothing I can do to fix the problem.

        So post back with example data from dataset 1 and dataset 2. Be sure that the examples you post exhibit the problem you are encountering when you run my original code, and make sure that my original code runs in them without error messages! For that to happen, the examples must include all the variables mentioned in the code. You can include additional variables showing the macro indicators and quality indices if you like, but really they are not relevant to getting this code to work. (If you can't get it to run without error messages, show the output, including error messages that you are getting from Stata.)


        The error messages you are getting tells me that the variables myvars* contain illicit characters that are not allowed in numbers. But as you have provided no example data for these variables, again, there is nothing I can do to help you. The best advice I can give you is to review these variables and see if you can identify, by eye, the offending observations. If you can't spot them by eye, run
        Code:
        foreach v of varlist myvars1 myvar2 myvar3 myvar4 {
        decode `v', gen(_`v')
        replace _`v' = subinstr(_`v', ",", ".", .)
        tab _`v' if missing(real(_`v'))
        }
        The output from this will show you the offending values of these variables. Then you can go back through the data management that created these problems and fix them. This is clearly a problem with your data, not a problem with the code.

        Thanks for the help once again.I paste a larger sample of my data.The first dataset1 is referred to the politics variable . The second dataset is referred to some of the macro variable There are more variables in each datasets . Both datasets are part of their final form and dataset1 was created with your code, after I made some modifications from their original form.

        A part form the question I raised about the merging, and hope to be clear by now what I really aim to do, I have included in the dataset 2 the variables mentioned in my precious query that turned to contains nonnumeric characters; . in their sting and encoded form . gb1 gb2 gb3 d1 gb1_b gb2_b gb3_b d1_b . Running there the code suggested to spot the error did solved converting the series back to strings


        Code:
         foreach v of varlist gb1_b gb2_b gb3_b d1_b  { decode `v', gen(_`v') replace _`v' = subinstr(_`v', ",", ".", .) tab _`v' if missing(real(_`v')) }
        (829 real changes made)
        no observations
        (467 real changes made)
        no observations
        (721 real changes made)
        no observations
        (93 real changes made)
        no observations


        thank you again for your time dedicated and your help!

        dataset1(politics)


        ----------------------- copy starting from the next line -----------------------
        [CODE]
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str15 ccode str14 oecdmember str16 eumember long(ts edate date) str15 country int startyear byte(startmonth t1 t2) float(pm gv start) int time float(end in_year_duration)
        "Australia" "oecd member" "no eu membership" 1990 . . "Australia" 1987 7 1 1 -4.5 -4.5 10064 11051 11051 93
        "Australia" "oecd member" "no eu membership" 1990 . . "Australia" 1990 4 1 1 -14.9 -14.9 11051 11683 11683 271
        "Australia" "oecd member" "no eu membership" 1991 . . "Australia" 1990 4 1 1 -14.9 -14.9 11051 11683 11683 360
        "Australia" "oecd member" "no eu membership" 1991 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 4
        "Australia" "oecd member" "no eu membership" 1992 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 365
        "Australia" "oecd member" "no eu membership" 1993 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 82
        "Australia" "oecd member" "no eu membership" 1993 34041 199303 "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 282
        "Australia" "oecd member" "no eu membership" 1994 . . "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 364
        "Australia" "oecd member" "no eu membership" 1995 . . "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 364
        "Australia" "oecd member" "no eu membership" 1996 35126 199603 "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 70
        "Australia" "oecd member" "no eu membership" 1996 . . "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 295
        "Australia" "oecd member" "no eu membership" 1997 . . "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 364
        "Australia" "oecd member" "no eu membership" 1998 36071 199810 "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 293
        "Australia" "oecd member" "no eu membership" 1998 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 71
        "Australia" "oecd member" "no eu membership" 1999 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 364
        "Australia" "oecd member" "no eu membership" 2000 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 365
        "Australia" "oecd member" "no eu membership" 2001 37205 200111 "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 329
        "Australia" "oecd member" "no eu membership" 2001 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 35
        "Australia" "oecd member" "no eu membership" 2002 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 364
        "Australia" "oecd member" "no eu membership" 2003 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 364
        "Australia" "oecd member" "no eu membership" 2004 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 299
        "Australia" "oecd member" "no eu membership" 2004 38269 200410 "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 66
        "Australia" "oecd member" "no eu membership" 2005 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 364
        "Australia" "oecd member" "no eu membership" 2006 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 364
        "Australia" "oecd member" "no eu membership" 2007 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 336
        "Australia" "oecd member" "no eu membership" 2007 39410 200711 "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 28
        "Australia" "oecd member" "no eu membership" 2008 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 365
        "Australia" "oecd member" "no eu membership" 2009 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 364
        "Australia" "oecd member" "no eu membership" 2010 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 174
        "Australia" "oecd member" "no eu membership" 2010 40411 201008 "Australia" 2010 6 1 1 5.674 5.674 18437 -18006 18519 82
        "Australia" "oecd member" "no eu membership" 2010 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 108
        "Australia" "oecd member" "no eu membership" 2011 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 364
        "Australia" "oecd member" "no eu membership" 2012 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 365
        "Australia" "oecd member" "no eu membership" 2013 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 177
        "Australia" "oecd member" "no eu membership" 2013 41524 201309 "Australia" 2013 6 4 4 -34.113 -34.113 19536 -16906 19619 83
        "Australia" "oecd member" "no eu membership" 2013 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 104
        "Australia" "oecd member" "no eu membership" 2014 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 364
        "Australia" "oecd member" "no eu membership" 2015 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 257
        "Australia" "oecd member" "no eu membership" 2015 . . "Australia" 2013 9 2 2 22.975 18.485498 19616 -15105 21420 364
        "Australia" "oecd member" "no eu membership" 2016 42553 201607 "Australia" 2013 9 2 2 22.975 18.485498 19616 -15105 21420 365
        "Australia" "oecd member" "no eu membership" 2016 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 365
        "Australia" "oecd member" "no eu membership" 2017 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 364
        "Australia" "oecd member" "no eu membership" 2018 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 235
        "Australia" "oecd member" "no eu membership" 2018 . . "Australia" 2013 8 2 2 12.24 . 19594 -14976 21549 364

        end
        format %td start
        format %td end
        Last edited by Mario Ferri; 09 Feb 2020, 19:13.

        Comment


        • #19
          Originally posted by Clyde Schechter View Post
          Your new example dataset 1 does not contain any end dates for the governments, so I can't do anything with this.

          I will need a workable example dataset 1 and example dataset 2 to troubleshoot this. When I reread the code, I don't see any reason why the end result does not include the additional governments. And when I ran it with the original example data, it did include those. So something is different about your new data sets. But unless I can actually run the code with them, there is nothing I can do to fix the problem.

          So post back with example data from dataset 1 and dataset 2. Be sure that the examples you post exhibit the problem you are encountering when you run my original code, and make sure that my original code runs in them without error messages! For that to happen, the examples must include all the variables mentioned in the code. You can include additional variables showing the macro indicators and quality indices if you like, but really they are not relevant to getting this code to work. (If you can't get it to run without error messages, show the output, including error messages that you are getting from Stata.)


          The error messsages you are getting tells me that the variables myvars* contain illicit characters that are not allowed in numbers. But as you have provided no example data for these variables, again, there is nothing I can do to help you. The best advice I can give you is to review these variables and see if you can identify, by eye, the offending observations. If you can't spot them by eye, run
          Code:
          foreach v of varlist myvars1 myvar2 myvar3 myvar4 {
          decode `v', gen(_`v')
          replace _`v' = subinstr(_`v', ",", ".", .)
          tab _`v' if missing(real(_`v'))
          }
          The output from this will show you the offending values of these variables. Then you can go back through the data management that created these problems and fix them. This is clearly a problem with your data, not a problem with the code.

          [/CODE]
          ------------------ copy up to and including the previous line ------------------


          dataset 2(macro variables)

          ----------------------- copy starting from the next line -----------------------
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str48 country str3 countrycode int ts str6 timecode float(growth1 growth2 growth3) str7 gb1 str6(gb2 gb3) str7 d1 long(gb1_b gb2_b gb3_b d1_b)
          "Australia" "AUS" 1990 "YR1990" -.25998396    .8315406   1.496551 "7,924" "0,0965" "5,648"  "16.445"  674  26 711  152
          "Australia" "AUS" 1991 "YR1991"  1.3334774   2.1083677 -1.0142618 "8,777" "0,0864" "3,078"  "21.650"  705  15 627  207
          "Australia" "AUS" 1992 "YR1992"  3.2923484   3.4185865   2.523101 "8,553" "0,1099" "1,402"  "27.678"  697  47 467  281
          "Australia" "AUS" 1993 "YR1993"   3.715279   2.6728065   3.833016 "7,368" "0,1498" "1,779"  "30.721"  656 133 517  327
          "Australia" "AUS" 1994 "YR1994"  1.6375794   1.2805543   4.774543 "5,84"  "0,1489" "1,133"  "31.782"  565 130 427  336
          "Australia" "AUS" 1995 "YR1995"  -.9972533   -.9632453   2.801609 "7,754" "0,2159" "2,393"  "31.234"  664 252 583  332
          "Australia" "AUS" 1996 "YR1996"   2.770518    3.154481   4.078352 "6,368" "0,1598" "2,206"  "29.422"  600 159 562  310
          "Australia" "AUS" 1997 "YR1997"   3.889898   3.9895246  4.4481344 "5,838" "0,1703" "1,387"  "25.959"  564 180 465  262
          "Australia" "AUS" 1998 "YR1998"  2.9572344   3.7896104   4.613108 "5,414" "0,1701" "0,726"  "23.754"  518 179 358  240
          "Australia" "AUS" 1999 "YR1999"   2.561527    2.339495   4.356505 "4,035" "0,1985" "1,22"   "22.595"  368 221 440  221
          "Australia" "AUS" 2000 "YR2000"   .3891381   .50802076    2.99929 "5,668" "0,1953" "1,461"  "19.552"  550 216 475  187
          "Australia" "AUS" 2001 "YR2001"  1.1405312   3.1164474   2.561032 "5,152" "0,1514" "0,624"  "17.170"  489 137 342  158
          "Australia" "AUS" 2002 "YR2002"   2.158647   2.3763344   4.093701 "5,171" "0,1963" "1,053"  "15.051"  493 217 414  144
          "Australia" "AUS" 2003 "YR2003"   .3566159    .7994667   2.630159 "4,268" "0,1937" "0,971"  "13.221"  401 212 397  110
          "Australia" "AUS" 2004 "YR2004"  2.7113535    2.782252  4.1981416 "4,361" "0,2044" "1,299"  "11.948"  419 228 453   70
          "Australia" "AUS" 2005 "YR2005"  -.5891249   -.4728382   2.836245 "3,639" "0,1525" "1,696"  "10.891"  311 139 502    6
          "Australia" "AUS" 2006 "YR2006"   .4218182   1.0691733  2.7778394 "3,34"  "0,1278" "1,865"  "9.967"   275  83 526 1037
          "Australia" "AUS" 2007 "YR2007"  1.1622334   1.1005231   4.278156 "3,99"  "0,1159" "1,916"  "9.687"   359  58 532 1034
          "Australia" "AUS" 2008 "YR2008" -.08318187  .003607297   2.642911 "4,438" "0,1276" "2,003"  "11.753"  429  82 548   69
          "Australia" "AUS" 2009 "YR2009"  1.1294768    2.830966  1.8660793 "3,871" "0,257"  "1,046"  "16.654"  341 283 413  153
          "Australia" "AUS" 2010 "YR2010"   .3700751 -.070456676  2.3563232 "3,88"  "0,2605" "2,337"  "20.397"  342 287 577  193
          "Australia" "AUS" 2011 "YR2011"   1.048757   1.0357703  2.7214854 "3,451" "0,1435" "2,595"  "24.065"  288 120 598  250
          "Australia" "AUS" 2012 "YR2012"   2.587376    2.995731   3.808844 "3,194" "0,2031" "1,899"  "27.540"  261 227 529  279
          "Australia" "AUS" 2013 "YR2013"  1.1289984   1.2830683   2.113799 "1,754" "0,295"  "1,98"   "30.498"  135 310 537  323
          "Australia" "AUS" 2014 "YR2014"  1.6469382    1.930875   2.608791 "2,279" "0,2662" "2,3"    "34.025"  215 292 572  360
          "Australia" "AUS" 2015 "YR2015"   .3492132   .35471275   2.429927 "0,711" "0,2123" "2,289"  "37.690"   60 246 570  420
          "Australia" "AUS" 2016 "YR2016"  1.0832386   1.7193124   2.741536 "0,891" "0,3553" "2,255"  "40.458"   76 342 566  479
          "Australia" "AUS" 2017 "YR2017" .036598407  -.01947236  2.3533013 "0,414" "0,3673" "2,588"  "41.066"   36 346 597  492
          "Australia" "AUS" 2018 "YR2018"   .1107098    .7712589  2.7736175 "0,583" "0,2131" "2,248"  "41.369"   49 249 565  496
          
          end
          label values gb1_b TR10y_yield
          label def TR10y_yield 36 "0,414", modify
          label def TR10y_yield 42 "0,514", modify
          label def TR10y_yield 49 "0,583", modify
          label def TR10y_yield 53 "0,634", modify
          label def TR10y_yield 60 "0,711", modify
          label def TR10y_yield 71 "0,824", modify
          label def TR10y_yield 76 "0,891", modify
          label def TR10y_yield 82 "0,977", modify
          label def TR10y_yield 135 "1,754", modify
          label def TR10y_yield 156 "10", modify
          label def TR10y_yield 202 "2,051", modify
          label def TR10y_yield 215 "2,279", modify
          label def TR10y_yield 232 "2,558", modify
          label def TR10y_yield 261 "3,194", modify
          label def TR10y_yield 275 "3,34", modify
          label def TR10y_yield 279 "3,365", modify
          label def TR10y_yield 288 "3,451", modify
          label def TR10y_yield 311 "3,639", modify
          label def TR10y_yield 312 "3,653", modify
          label def TR10y_yield 322 "3,716", modify
          label def TR10y_yield 328 "3,778", modify
          label def TR10y_yield 341 "3,871", modify
          label def TR10y_yield 342 "3,88", modify
          label def TR10y_yield 347 "3,928", modify
          label def TR10y_yield 354 "3,977", modify
          label def TR10y_yield 359 "3,99", modify
          label def TR10y_yield 368 "4,035", modify
          label def TR10y_yield 370 "4,051", modify
          label def TR10y_yield 394 "4,241", modify
          label def TR10y_yield 401 "4,268", modify
          label def TR10y_yield 412 "4,315", modify
          label def TR10y_yield 415 "4,345", modify
          label def TR10y_yield 419 "4,361", modify
          label def TR10y_yield 429 "4,438", modify
          label def TR10y_yield 435 "4,469", modify
          label def TR10y_yield 489 "5,152", modify
          label def TR10y_yield 493 "5,171", modify
          label def TR10y_yield 495 "5,185", modify
          label def TR10y_yield 501 "5,22", modify
          label def TR10y_yield 518 "5,414", modify
          label def TR10y_yield 522 "5,451", modify
          label def TR10y_yield 550 "5,668", modify
          label def TR10y_yield 554 "5,701", modify
          label def TR10y_yield 564 "5,838", modify
          label def TR10y_yield 565 "5,84", modify
          label def TR10y_yield 568 "5,87", modify
          label def TR10y_yield 600 "6,368", modify
          label def TR10y_yield 601 "6,397", modify
          label def TR10y_yield 618 "6,688", modify
          label def TR10y_yield 656 "7,368", modify
          label def TR10y_yield 664 "7,754", modify
          label def TR10y_yield 665 "7,76", modify
          label def TR10y_yield 674 "7,924", modify
          label def TR10y_yield 693 "8,436", modify
          label def TR10y_yield 697 "8,553", modify
          label def TR10y_yield 705 "8,777", modify
          label def TR10y_yield 707 "8,91", modify
          label def TR10y_yield 719 "9,49", modify
          label values gb2_b TR10y_volatility
          label def TR10y_volatility 15 "0,0864", modify
          label def TR10y_volatility 26 "0,0965", modify
          label def TR10y_volatility 47 "0,1099", modify
          label def TR10y_volatility 58 "0,1159", modify
          label def TR10y_volatility 82 "0,1276", modify
          label def TR10y_volatility 83 "0,1278", modify
          label def TR10y_volatility 120 "0,1435", modify
          label def TR10y_volatility 130 "0,1489", modify
          label def TR10y_volatility 133 "0,1498", modify
          label def TR10y_volatility 137 "0,1514", modify
          label def TR10y_volatility 139 "0,1525", modify
          label def TR10y_volatility 159 "0,1598", modify
          label def TR10y_volatility 179 "0,1701", modify
          label def TR10y_volatility 180 "0,1703", modify
          label def TR10y_volatility 212 "0,1937", modify
          label def TR10y_volatility 216 "0,1953", modify
          label def TR10y_volatility 217 "0,1963", modify
          label def TR10y_volatility 221 "0,1985", modify
          label def TR10y_volatility 227 "0,2031", modify
          label def TR10y_volatility 228 "0,2044", modify
          label def TR10y_volatility 246 "0,2123", modify
          label def TR10y_volatility 249 "0,2131", modify
          label def TR10y_volatility 252 "0,2159", modify
          label def TR10y_volatility 283 "0,257", modify
          label def TR10y_volatility 287 "0,2605", modify
          label def TR10y_volatility 292 "0,2662", modify
          label def TR10y_volatility 310 "0,295", modify
          label def TR10y_volatility 342 "0,3553", modify
          label def TR10y_volatility 346 "0,3673", modify
          label def TR10y_volatility 410 "0.00", modify
          label def TR10y_volatility 411 "0.01", modify
          label def TR10y_volatility 412 "0.02", modify
          label def TR10y_volatility 413 "0.03", modify
          label def TR10y_volatility 414 "0.04", modify
          label def TR10y_volatility 415 "0.05", modify
          label def TR10y_volatility 416 "0.06", modify
          label def TR10y_volatility 417 "0.07", modify
          label def TR10y_volatility 418 "0.08", modify
          label def TR10y_volatility 419 "0.09", modify
          label def TR10y_volatility 420 "0.10", modify
          label values gb3_b spreads
          label def spreads 16 "-0,113", modify
          label def spreads 18 "-0,145", modify
          label def spreads 23 "-0,215", modify
          label def spreads 125 "0,005", modify
          label def spreads 130 "0,018", modify
          label def spreads 131 "0,019", modify
          label def spreads 138 "0,03", modify
          label def spreads 139 "0,031", modify
          label def spreads 144 "0,044", modify
          label def spreads 150 "0,06", modify
          label def spreads 159 "0,079", modify
          label def spreads 165 "0,09", modify
          label def spreads 171 "0,107", modify
          label def spreads 174 "0,114", modify
          label def spreads 180 "0,126", modify
          label def spreads 181 "0,127", modify
          label def spreads 187 "0,145", modify
          label def spreads 195 "0,159", modify
          label def spreads 201 "0,169", modify
          label def spreads 202 "0,17", modify
          label def spreads 216 "0,202", modify
          label def spreads 218 "0,207", modify
          label def spreads 221 "0,21", modify
          label def spreads 222 "0,214", modify
          label def spreads 228 "0,228", modify
          label def spreads 229 "0,229", modify
          label def spreads 235 "0,245", modify
          label def spreads 239 "0,257", modify
          label def spreads 241 "0,261", modify
          label def spreads 249 "0,283", modify
          label def spreads 255 "0,293", modify
          label def spreads 262 "0,316", modify
          label def spreads 264 "0,328", modify
          label def spreads 265 "0,329", modify
          label def spreads 269 "0,338", modify
          label def spreads 270 "0,343", modify
          label def spreads 282 "0,384", modify
          label def spreads 285 "0,394", modify
          label def spreads 300 "0,449", modify
          label def spreads 309 "0,492", modify
          label def spreads 314 "0,531", modify
          label def spreads 323 "0,565", modify
          label def spreads 331 "0,599", modify
          label def spreads 338 "0,617", modify
          label def spreads 342 "0,624", modify
          label def spreads 355 "0,714", modify
          label def spreads 358 "0,726", modify
          label def spreads 362 "0,746", modify
          label def spreads 371 "0,789", modify
          label def spreads 376 "0,834", modify
          label def spreads 379 "0,85", modify
          label def spreads 388 "0,922", modify
          label def spreads 389 "0,927", modify
          label def spreads 397 "0,971", modify
          label def spreads 409 "0.672", modify
          label def spreads 410 "1,008", modify
          label def spreads 413 "1,046", modify
          label def spreads 414 "1,053", modify
          label def spreads 427 "1,133", modify
          label def spreads 440 "1,22", modify
          label def spreads 452 "1,293", modify
          label def spreads 453 "1,299", modify
          label def spreads 465 "1,387", modify
          label def spreads 467 "1,402", modify
          label def spreads 475 "1,461", modify
          label def spreads 502 "1,696", modify
          label def spreads 517 "1,779", modify
          label def spreads 526 "1,865", modify
          label def spreads 529 "1,899", modify
          label def spreads 532 "1,916", modify
          label def spreads 537 "1,98", modify
          label def spreads 548 "2,003", modify
          label def spreads 562 "2,206", modify
          label def spreads 563 "2,238", modify
          label def spreads 565 "2,248", modify
          label def spreads 566 "2,255", modify
          label def spreads 570 "2,289", modify
          label def spreads 572 "2,3", modify
          label def spreads 577 "2,337", modify
          label def spreads 578 "2,34", modify
          label def spreads 583 "2,393", modify
          label def spreads 597 "2,588", modify
          label def spreads 598 "2,595", modify
          label def spreads 627 "3,078", modify
          label def spreads 711 "5,648", modify
          label values d1_b debt
          label def debt 6 "10.891", modify
          label def debt 16 "101.113", modify
          label def debt 22 "102.030", modify
          label def debt 28 "102.592", modify
          label def debt 32 "103.401", modify
          label def debt 34 "104,33", modify
          label def debt 36 "104.332", modify
          label def debt 40 "104.711", modify
          label def debt 46 "105.452", modify
          label def debt 49 "106.109", modify
          label def debt 50 "106.342", modify
          label def debt 55 "107.523", modify
          label def debt 56 "107.592", modify
          label def debt 59 "108.769", modify
          label def debt 69 "11.753", modify
          label def debt 70 "11.948", modify
          label def debt 77 "114.417", modify
          label def debt 85 "118.196", modify
          label def debt 96 "123.205", modify
          label def debt 103 "127.973", modify
          label def debt 108 "129.571", modify
          label def debt 110 "13.221", modify
          label def debt 113 "130.544", modify
          label def debt 115 "131.052", modify
          label def debt 122 "133.923", modify
          label def debt 123 "136.308", modify
          label def debt 126 "138.141", modify
          label def debt 140 "141,31", modify
          label def debt 144 "15.051", modify
          label def debt 152 "16.445", modify
          label def debt 153 "16.654", modify
          label def debt 158 "17.170", modify
          label def debt 165 "170,64", modify
          label def debt 167 "172,3", modify
          label def debt 187 "19.552", modify
          label def debt 193 "20.397", modify
          label def debt 207 "21.650", modify
          label def debt 221 "22.595", modify
          label def debt 240 "23.754", modify
          label def debt 250 "24.065", modify
          label def debt 262 "25.959", modify
          label def debt 279 "27.540", modify
          label def debt 281 "27.678", modify
          label def debt 306 "289,55", modify
          label def debt 310 "29.422", modify
          label def debt 323 "30.498", modify
          label def debt 327 "30.721", modify
          label def debt 332 "31.234", modify
          label def debt 336 "31.782", modify
          label def debt 360 "34.025", modify
          label def debt 420 "37.690", modify
          label def debt 479 "40.458", modify
          label def debt 492 "41.066", modify
          label def debt 496 "41.369", modify
          label def debt 669 "53.819", modify
          label def debt 698 "55.932", modify
          label def debt 700 "56.028", modify
          label def debt 704 "56.134", modify
          label def debt 768 "60.629", modify
          label def debt 772 "61.103", modify
          label def debt 791 "63.056", modify
          label def debt 798 "63.721", modify
          label def debt 817 "64.740", modify
          label def debt 818 "64.849", modify
          label def debt 821 "64.896", modify
          label def debt 831 "65.738", modify
          label def debt 841 "66.355", modify
          label def debt 846 "66.978", modify
          label def debt 848 "67.001", modify
          label def debt 855 "67.574", modify
          label def debt 858 "67.807", modify
          label def debt 861 "67.865", modify
          label def debt 871 "68.318", modify
          label def debt 873 "68.417", modify
          label def debt 881 "68.819", modify
          label def debt 927 "73.754", modify
          label def debt 928 "73.859", modify
          label def debt 945 "76.502", modify
          label def debt 954 "78.489", modify
          label def debt 962 "79.406", modify
          label def debt 963 "79.580", modify
          label def debt 985 "81.011", modify
          label def debt 990 "81.661", modify
          label def debt 993 "82.178", modify
          label def debt 995 "82.418", modify
          label def debt 997 "82.866", modify
          label def debt 1002 "83.758", modify
          label def debt 1006 "84.404", modify
          label def debt 1019 "87.028", modify
          label def debt 1034 "9.687", modify
          label def debt 1037 "9.967", modify
          label def debt 1042 "91.049", modify
          label def debt 1047 "92.531", modify
          label def debt 1052 "94.677", modify
          label def debt 1061 "96,44", modify
          label def debt 1062 "96.524", modify
          label def debt 1078 "99.536", modify
          label def debt 1079 "99.720", modify
          ------------------ copy up to and including the previous line --------
          Last edited by Mario Ferri; 09 Feb 2020, 19:12.

          Comment


          • #20
            I am unable to replicate your problem. I have modified the code slightly to reflect that you have update the datasets by doing some of the cleaning from before. But that doesn't change the actual operations of mergeing that underlie the process. When I run the code with these new examples it correctly links up the macro data with the observations having the longest duration of government and preserves the other observations as well.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str15 ccode str14 oecdmember str16 eumember long(ts edate date) str15 country int startyear byte(startmonth t1 t2) float(pm gv start) int time float(end in_year_duration)
            "Australia" "oecd member" "no eu membership" 1990 . . "Australia" 1987 7 1 1 -4.5 -4.5 10064 11051 11051 93
            "Australia" "oecd member" "no eu membership" 1990 . . "Australia" 1990 4 1 1 -14.9 -14.9 11051 11683 11683 271
            "Australia" "oecd member" "no eu membership" 1991 . . "Australia" 1990 4 1 1 -14.9 -14.9 11051 11683 11683 360
            "Australia" "oecd member" "no eu membership" 1991 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 4
            "Australia" "oecd member" "no eu membership" 1992 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 365
            "Australia" "oecd member" "no eu membership" 1993 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 82
            "Australia" "oecd member" "no eu membership" 1993 34041 199303 "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 282
            "Australia" "oecd member" "no eu membership" 1994 . . "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 364
            "Australia" "oecd member" "no eu membership" 1995 . . "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 364
            "Australia" "oecd member" "no eu membership" 1996 35126 199603 "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 70
            "Australia" "oecd member" "no eu membership" 1996 . . "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 295
            "Australia" "oecd member" "no eu membership" 1997 . . "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 364
            "Australia" "oecd member" "no eu membership" 1998 36071 199810 "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 293
            "Australia" "oecd member" "no eu membership" 1998 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 71
            "Australia" "oecd member" "no eu membership" 1999 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 364
            "Australia" "oecd member" "no eu membership" 2000 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 365
            "Australia" "oecd member" "no eu membership" 2001 37205 200111 "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 329
            "Australia" "oecd member" "no eu membership" 2001 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 35
            "Australia" "oecd member" "no eu membership" 2002 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 364
            "Australia" "oecd member" "no eu membership" 2003 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 364
            "Australia" "oecd member" "no eu membership" 2004 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 299
            "Australia" "oecd member" "no eu membership" 2004 38269 200410 "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 66
            "Australia" "oecd member" "no eu membership" 2005 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 364
            "Australia" "oecd member" "no eu membership" 2006 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 364
            "Australia" "oecd member" "no eu membership" 2007 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 336
            "Australia" "oecd member" "no eu membership" 2007 39410 200711 "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 28
            "Australia" "oecd member" "no eu membership" 2008 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 365
            "Australia" "oecd member" "no eu membership" 2009 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 364
            "Australia" "oecd member" "no eu membership" 2010 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 174
            "Australia" "oecd member" "no eu membership" 2010 40411 201008 "Australia" 2010 6 1 1 5.674 5.674 18437 -18006 18519 82
            "Australia" "oecd member" "no eu membership" 2010 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 108
            "Australia" "oecd member" "no eu membership" 2011 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 364
            "Australia" "oecd member" "no eu membership" 2012 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 365
            "Australia" "oecd member" "no eu membership" 2013 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 177
            "Australia" "oecd member" "no eu membership" 2013 41524 201309 "Australia" 2013 6 4 4 -34.113 -34.113 19536 -16906 19619 83
            "Australia" "oecd member" "no eu membership" 2013 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 104
            "Australia" "oecd member" "no eu membership" 2014 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 364
            "Australia" "oecd member" "no eu membership" 2015 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 257
            "Australia" "oecd member" "no eu membership" 2015 . . "Australia" 2013 9 2 2 22.975 18.485498 19616 -15105 21420 364
            "Australia" "oecd member" "no eu membership" 2016 42553 201607 "Australia" 2013 9 2 2 22.975 18.485498 19616 -15105 21420 365
            "Australia" "oecd member" "no eu membership" 2016 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 365
            "Australia" "oecd member" "no eu membership" 2017 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 364
            "Australia" "oecd member" "no eu membership" 2018 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 235
            "Australia" "oecd member" "no eu membership" 2018 . . "Australia" 2013 8 2 2 12.24 . 19594 -14976 21549 364
            end
            format %td start
            format %td end
            tempfile original_dataset_1
            save `original_dataset_1'
            
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str48 country str3 countrycode int ts str6 timecode float(growth1 growth2 growth3) str7 gb1 str6(gb2 gb3) str7 d1 long(gb1_b gb2_b gb3_b d1_b)
            "Australia" "AUS" 1990 "YR1990" -.25998396    .8315406   1.496551 "7,924" "0,0965" "5,648"  "16.445"  674  26 711  152
            "Australia" "AUS" 1991 "YR1991"  1.3334774   2.1083677 -1.0142618 "8,777" "0,0864" "3,078"  "21.650"  705  15 627  207
            "Australia" "AUS" 1992 "YR1992"  3.2923484   3.4185865   2.523101 "8,553" "0,1099" "1,402"  "27.678"  697  47 467  281
            "Australia" "AUS" 1993 "YR1993"   3.715279   2.6728065   3.833016 "7,368" "0,1498" "1,779"  "30.721"  656 133 517  327
            "Australia" "AUS" 1994 "YR1994"  1.6375794   1.2805543   4.774543 "5,84"  "0,1489" "1,133"  "31.782"  565 130 427  336
            "Australia" "AUS" 1995 "YR1995"  -.9972533   -.9632453   2.801609 "7,754" "0,2159" "2,393"  "31.234"  664 252 583  332
            "Australia" "AUS" 1996 "YR1996"   2.770518    3.154481   4.078352 "6,368" "0,1598" "2,206"  "29.422"  600 159 562  310
            "Australia" "AUS" 1997 "YR1997"   3.889898   3.9895246  4.4481344 "5,838" "0,1703" "1,387"  "25.959"  564 180 465  262
            "Australia" "AUS" 1998 "YR1998"  2.9572344   3.7896104   4.613108 "5,414" "0,1701" "0,726"  "23.754"  518 179 358  240
            "Australia" "AUS" 1999 "YR1999"   2.561527    2.339495   4.356505 "4,035" "0,1985" "1,22"   "22.595"  368 221 440  221
            "Australia" "AUS" 2000 "YR2000"   .3891381   .50802076    2.99929 "5,668" "0,1953" "1,461"  "19.552"  550 216 475  187
            "Australia" "AUS" 2001 "YR2001"  1.1405312   3.1164474   2.561032 "5,152" "0,1514" "0,624"  "17.170"  489 137 342  158
            "Australia" "AUS" 2002 "YR2002"   2.158647   2.3763344   4.093701 "5,171" "0,1963" "1,053"  "15.051"  493 217 414  144
            "Australia" "AUS" 2003 "YR2003"   .3566159    .7994667   2.630159 "4,268" "0,1937" "0,971"  "13.221"  401 212 397  110
            "Australia" "AUS" 2004 "YR2004"  2.7113535    2.782252  4.1981416 "4,361" "0,2044" "1,299"  "11.948"  419 228 453   70
            "Australia" "AUS" 2005 "YR2005"  -.5891249   -.4728382   2.836245 "3,639" "0,1525" "1,696"  "10.891"  311 139 502    6
            "Australia" "AUS" 2006 "YR2006"   .4218182   1.0691733  2.7778394 "3,34"  "0,1278" "1,865"  "9.967"   275  83 526 1037
            "Australia" "AUS" 2007 "YR2007"  1.1622334   1.1005231   4.278156 "3,99"  "0,1159" "1,916"  "9.687"   359  58 532 1034
            "Australia" "AUS" 2008 "YR2008" -.08318187  .003607297   2.642911 "4,438" "0,1276" "2,003"  "11.753"  429  82 548   69
            "Australia" "AUS" 2009 "YR2009"  1.1294768    2.830966  1.8660793 "3,871" "0,257"  "1,046"  "16.654"  341 283 413  153
            "Australia" "AUS" 2010 "YR2010"   .3700751 -.070456676  2.3563232 "3,88"  "0,2605" "2,337"  "20.397"  342 287 577  193
            "Australia" "AUS" 2011 "YR2011"   1.048757   1.0357703  2.7214854 "3,451" "0,1435" "2,595"  "24.065"  288 120 598  250
            "Australia" "AUS" 2012 "YR2012"   2.587376    2.995731   3.808844 "3,194" "0,2031" "1,899"  "27.540"  261 227 529  279
            "Australia" "AUS" 2013 "YR2013"  1.1289984   1.2830683   2.113799 "1,754" "0,295"  "1,98"   "30.498"  135 310 537  323
            "Australia" "AUS" 2014 "YR2014"  1.6469382    1.930875   2.608791 "2,279" "0,2662" "2,3"    "34.025"  215 292 572  360
            "Australia" "AUS" 2015 "YR2015"   .3492132   .35471275   2.429927 "0,711" "0,2123" "2,289"  "37.690"   60 246 570  420
            "Australia" "AUS" 2016 "YR2016"  1.0832386   1.7193124   2.741536 "0,891" "0,3553" "2,255"  "40.458"   76 342 566  479
            "Australia" "AUS" 2017 "YR2017" .036598407  -.01947236  2.3533013 "0,414" "0,3673" "2,588"  "41.066"   36 346 597  492
            "Australia" "AUS" 2018 "YR2018"   .1107098    .7712589  2.7736175 "0,583" "0,2131" "2,248"  "41.369"   49 249 565  496
            end
            label values gb1_b TR10y_yield
            label def TR10y_yield 36 "0,414", modify
            label def TR10y_yield 42 "0,514", modify
            label def TR10y_yield 49 "0,583", modify
            label def TR10y_yield 53 "0,634", modify
            label def TR10y_yield 60 "0,711", modify
            label def TR10y_yield 71 "0,824", modify
            label def TR10y_yield 76 "0,891", modify
            label def TR10y_yield 82 "0,977", modify
            label def TR10y_yield 135 "1,754", modify
            label def TR10y_yield 156 "10", modify
            label def TR10y_yield 202 "2,051", modify
            label def TR10y_yield 215 "2,279", modify
            label def TR10y_yield 232 "2,558", modify
            label def TR10y_yield 261 "3,194", modify
            label def TR10y_yield 275 "3,34", modify
            label def TR10y_yield 279 "3,365", modify
            label def TR10y_yield 288 "3,451", modify
            label def TR10y_yield 311 "3,639", modify
            label def TR10y_yield 312 "3,653", modify
            label def TR10y_yield 322 "3,716", modify
            label def TR10y_yield 328 "3,778", modify
            label def TR10y_yield 341 "3,871", modify
            label def TR10y_yield 342 "3,88", modify
            label def TR10y_yield 347 "3,928", modify
            label def TR10y_yield 354 "3,977", modify
            label def TR10y_yield 359 "3,99", modify
            label def TR10y_yield 368 "4,035", modify
            label def TR10y_yield 370 "4,051", modify
            label def TR10y_yield 394 "4,241", modify
            label def TR10y_yield 401 "4,268", modify
            label def TR10y_yield 412 "4,315", modify
            label def TR10y_yield 415 "4,345", modify
            label def TR10y_yield 419 "4,361", modify
            label def TR10y_yield 429 "4,438", modify
            label def TR10y_yield 435 "4,469", modify
            label def TR10y_yield 489 "5,152", modify
            label def TR10y_yield 493 "5,171", modify
            label def TR10y_yield 495 "5,185", modify
            label def TR10y_yield 501 "5,22", modify
            label def TR10y_yield 518 "5,414", modify
            label def TR10y_yield 522 "5,451", modify
            label def TR10y_yield 550 "5,668", modify
            label def TR10y_yield 554 "5,701", modify
            label def TR10y_yield 564 "5,838", modify
            label def TR10y_yield 565 "5,84", modify
            label def TR10y_yield 568 "5,87", modify
            label def TR10y_yield 600 "6,368", modify
            label def TR10y_yield 601 "6,397", modify
            label def TR10y_yield 618 "6,688", modify
            label def TR10y_yield 656 "7,368", modify
            label def TR10y_yield 664 "7,754", modify
            label def TR10y_yield 665 "7,76", modify
            label def TR10y_yield 674 "7,924", modify
            label def TR10y_yield 693 "8,436", modify
            label def TR10y_yield 697 "8,553", modify
            label def TR10y_yield 705 "8,777", modify
            label def TR10y_yield 707 "8,91", modify
            label def TR10y_yield 719 "9,49", modify
            label values gb2_b TR10y_volatility
            label def TR10y_volatility 15 "0,0864", modify
            label def TR10y_volatility 26 "0,0965", modify
            label def TR10y_volatility 47 "0,1099", modify
            label def TR10y_volatility 58 "0,1159", modify
            label def TR10y_volatility 82 "0,1276", modify
            label def TR10y_volatility 83 "0,1278", modify
            label def TR10y_volatility 120 "0,1435", modify
            label def TR10y_volatility 130 "0,1489", modify
            label def TR10y_volatility 133 "0,1498", modify
            label def TR10y_volatility 137 "0,1514", modify
            label def TR10y_volatility 139 "0,1525", modify
            label def TR10y_volatility 159 "0,1598", modify
            label def TR10y_volatility 179 "0,1701", modify
            label def TR10y_volatility 180 "0,1703", modify
            label def TR10y_volatility 212 "0,1937", modify
            label def TR10y_volatility 216 "0,1953", modify
            label def TR10y_volatility 217 "0,1963", modify
            label def TR10y_volatility 221 "0,1985", modify
            label def TR10y_volatility 227 "0,2031", modify
            label def TR10y_volatility 228 "0,2044", modify
            label def TR10y_volatility 246 "0,2123", modify
            label def TR10y_volatility 249 "0,2131", modify
            label def TR10y_volatility 252 "0,2159", modify
            label def TR10y_volatility 283 "0,257", modify
            label def TR10y_volatility 287 "0,2605", modify
            label def TR10y_volatility 292 "0,2662", modify
            label def TR10y_volatility 310 "0,295", modify
            label def TR10y_volatility 342 "0,3553", modify
            label def TR10y_volatility 346 "0,3673", modify
            label def TR10y_volatility 410 "0.00", modify
            label def TR10y_volatility 411 "0.01", modify
            label def TR10y_volatility 412 "0.02", modify
            label def TR10y_volatility 413 "0.03", modify
            label def TR10y_volatility 414 "0.04", modify
            label def TR10y_volatility 415 "0.05", modify
            label def TR10y_volatility 416 "0.06", modify
            label def TR10y_volatility 417 "0.07", modify
            label def TR10y_volatility 418 "0.08", modify
            label def TR10y_volatility 419 "0.09", modify
            label def TR10y_volatility 420 "0.10", modify
            label values gb3_b spreads
            label def spreads 16 "-0,113", modify
            label def spreads 18 "-0,145", modify
            label def spreads 23 "-0,215", modify
            label def spreads 125 "0,005", modify
            label def spreads 130 "0,018", modify
            label def spreads 131 "0,019", modify
            label def spreads 138 "0,03", modify
            label def spreads 139 "0,031", modify
            label def spreads 144 "0,044", modify
            label def spreads 150 "0,06", modify
            label def spreads 159 "0,079", modify
            label def spreads 165 "0,09", modify
            label def spreads 171 "0,107", modify
            label def spreads 174 "0,114", modify
            label def spreads 180 "0,126", modify
            label def spreads 181 "0,127", modify
            label def spreads 187 "0,145", modify
            label def spreads 195 "0,159", modify
            label def spreads 201 "0,169", modify
            label def spreads 202 "0,17", modify
            label def spreads 216 "0,202", modify
            label def spreads 218 "0,207", modify
            label def spreads 221 "0,21", modify
            label def spreads 222 "0,214", modify
            label def spreads 228 "0,228", modify
            label def spreads 229 "0,229", modify
            label def spreads 235 "0,245", modify
            label def spreads 239 "0,257", modify
            label def spreads 241 "0,261", modify
            label def spreads 249 "0,283", modify
            label def spreads 255 "0,293", modify
            label def spreads 262 "0,316", modify
            label def spreads 264 "0,328", modify
            label def spreads 265 "0,329", modify
            label def spreads 269 "0,338", modify
            label def spreads 270 "0,343", modify
            label def spreads 282 "0,384", modify
            label def spreads 285 "0,394", modify
            label def spreads 300 "0,449", modify
            label def spreads 309 "0,492", modify
            label def spreads 314 "0,531", modify
            label def spreads 323 "0,565", modify
            label def spreads 331 "0,599", modify
            label def spreads 338 "0,617", modify
            label def spreads 342 "0,624", modify
            label def spreads 355 "0,714", modify
            label def spreads 358 "0,726", modify
            label def spreads 362 "0,746", modify
            label def spreads 371 "0,789", modify
            label def spreads 376 "0,834", modify
            label def spreads 379 "0,85", modify
            label def spreads 388 "0,922", modify
            label def spreads 389 "0,927", modify
            label def spreads 397 "0,971", modify
            label def spreads 409 "0.672", modify
            label def spreads 410 "1,008", modify
            label def spreads 413 "1,046", modify
            label def spreads 414 "1,053", modify
            label def spreads 427 "1,133", modify
            label def spreads 440 "1,22", modify
            label def spreads 452 "1,293", modify
            label def spreads 453 "1,299", modify
            label def spreads 465 "1,387", modify
            label def spreads 467 "1,402", modify
            label def spreads 475 "1,461", modify
            label def spreads 502 "1,696", modify
            label def spreads 517 "1,779", modify
            label def spreads 526 "1,865", modify
            label def spreads 529 "1,899", modify
            label def spreads 532 "1,916", modify
            label def spreads 537 "1,98", modify
            label def spreads 548 "2,003", modify
            label def spreads 562 "2,206", modify
            label def spreads 563 "2,238", modify
            label def spreads 565 "2,248", modify
            label def spreads 566 "2,255", modify
            label def spreads 570 "2,289", modify
            label def spreads 572 "2,3", modify
            label def spreads 577 "2,337", modify
            label def spreads 578 "2,34", modify
            label def spreads 583 "2,393", modify
            label def spreads 597 "2,588", modify
            label def spreads 598 "2,595", modify
            label def spreads 627 "3,078", modify
            label def spreads 711 "5,648", modify
            label values d1_b debt
            label def debt 6 "10.891", modify
            label def debt 16 "101.113", modify
            label def debt 22 "102.030", modify
            label def debt 28 "102.592", modify
            label def debt 32 "103.401", modify
            label def debt 34 "104,33", modify
            label def debt 36 "104.332", modify
            label def debt 40 "104.711", modify
            label def debt 46 "105.452", modify
            label def debt 49 "106.109", modify
            label def debt 50 "106.342", modify
            label def debt 55 "107.523", modify
            label def debt 56 "107.592", modify
            label def debt 59 "108.769", modify
            label def debt 69 "11.753", modify
            label def debt 70 "11.948", modify
            label def debt 77 "114.417", modify
            label def debt 85 "118.196", modify
            label def debt 96 "123.205", modify
            label def debt 103 "127.973", modify
            label def debt 108 "129.571", modify
            label def debt 110 "13.221", modify
            label def debt 113 "130.544", modify
            label def debt 115 "131.052", modify
            label def debt 122 "133.923", modify
            label def debt 123 "136.308", modify
            label def debt 126 "138.141", modify
            label def debt 140 "141,31", modify
            label def debt 144 "15.051", modify
            label def debt 152 "16.445", modify
            label def debt 153 "16.654", modify
            label def debt 158 "17.170", modify
            label def debt 165 "170,64", modify
            label def debt 167 "172,3", modify
            label def debt 187 "19.552", modify
            label def debt 193 "20.397", modify
            label def debt 207 "21.650", modify
            label def debt 221 "22.595", modify
            label def debt 240 "23.754", modify
            label def debt 250 "24.065", modify
            label def debt 262 "25.959", modify
            label def debt 279 "27.540", modify
            label def debt 281 "27.678", modify
            label def debt 306 "289,55", modify
            label def debt 310 "29.422", modify
            label def debt 323 "30.498", modify
            label def debt 327 "30.721", modify
            label def debt 332 "31.234", modify
            label def debt 336 "31.782", modify
            label def debt 360 "34.025", modify
            label def debt 420 "37.690", modify
            label def debt 479 "40.458", modify
            label def debt 492 "41.066", modify
            label def debt 496 "41.369", modify
            label def debt 669 "53.819", modify
            label def debt 698 "55.932", modify
            label def debt 700 "56.028", modify
            label def debt 704 "56.134", modify
            label def debt 768 "60.629", modify
            label def debt 772 "61.103", modify
            label def debt 791 "63.056", modify
            label def debt 798 "63.721", modify
            label def debt 817 "64.740", modify
            label def debt 818 "64.849", modify
            label def debt 821 "64.896", modify
            label def debt 831 "65.738", modify
            label def debt 841 "66.355", modify
            label def debt 846 "66.978", modify
            label def debt 848 "67.001", modify
            label def debt 855 "67.574", modify
            label def debt 858 "67.807", modify
            label def debt 861 "67.865", modify
            label def debt 871 "68.318", modify
            label def debt 873 "68.417", modify
            label def debt 881 "68.819", modify
            label def debt 927 "73.754", modify
            label def debt 928 "73.859", modify
            label def debt 945 "76.502", modify
            label def debt 954 "78.489", modify
            label def debt 962 "79.406", modify
            label def debt 963 "79.580", modify
            label def debt 985 "81.011", modify
            label def debt 990 "81.661", modify
            label def debt 993 "82.178", modify
            label def debt 995 "82.418", modify
            label def debt 997 "82.866", modify
            label def debt 1002 "83.758", modify
            label def debt 1006 "84.404", modify
            label def debt 1019 "87.028", modify
            label def debt 1034 "9.687", modify
            label def debt 1037 "9.967", modify
            label def debt 1042 "91.049", modify
            label def debt 1047 "92.531", modify
            label def debt 1052 "94.677", modify
            label def debt 1061 "96,44", modify
            label def debt 1062 "96.524", modify
            label def debt 1078 "99.536", modify
            label def debt 1079 "99.720", modify
            tempfile dataset2
            save `dataset2'
            
            //  CODE BEGINS HERE
            use `original_dataset_1', clear
            gen long obs_no = _n
            assert !missing(in_year_duration)
            tempfile the_whole_thing
            save `the_whole_thing'
            
            //  REDUCE TO GOVERNMENT WITH LONGEST IN YEAR DURATION
            by country ts (in_year_duration), sort: keep if _n == _N
            
            //  BRING IN THE MACRO DATA
            merge 1:m country ts using `dataset2', nogenerate
            
            //  BRING BACK THE OTHER OBSERVATIONS
            merge 1:1 obs_no using `the_whole_thing', nogenerate
            sort country ts in_year_duration
            Unless you come up with a different example where the code fails, I'm going to consider this problem solved. The code is actually pretty simple, and I cannot think of what could be in the data that would break it.

            I will just comment that, while I don't know what analyses you plan to do with this, it is hard for me to imagine that they can be done with variables gb1_b gb2_ fb3_b and d1_b, which are value labeled integers: no calculations that you do with them will properly reflect the values that you see when you -list- or -browse- or -display- these. Keeping them as strings has avoided getting an error message, but it by no means solves the problem. You indicated that you found the non-numeric material in them: you have to do something to eliminate that non-numeric material and then successfully -destring- these (after -decode-ing them). Otherwise any calculations you do with them will be very, very wrong.

            Comment


            • #21
              Originally posted by Clyde Schechter View Post
              I am unable to replicate your problem. I have modified the code slightly to reflect that you have update the datasets by doing some of the cleaning from before. But that doesn't change the actual operations of mergeing that underlie the process. When I run the code with these new examples it correctly links up the macro data with the observations having the longest duration of government and preserves the other observations as well.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str15 ccode str14 oecdmember str16 eumember long(ts edate date) str15 country int startyear byte(startmonth t1 t2) float(pm gv start) int time float(end in_year_duration)
              "Australia" "oecd member" "no eu membership" 1990 . . "Australia" 1987 7 1 1 -4.5 -4.5 10064 11051 11051 93
              "Australia" "oecd member" "no eu membership" 1990 . . "Australia" 1990 4 1 1 -14.9 -14.9 11051 11683 11683 271
              "Australia" "oecd member" "no eu membership" 1991 . . "Australia" 1990 4 1 1 -14.9 -14.9 11051 11683 11683 360
              "Australia" "oecd member" "no eu membership" 1991 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 4
              "Australia" "oecd member" "no eu membership" 1992 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 365
              "Australia" "oecd member" "no eu membership" 1993 . . "Australia" 1991 12 1 1 -14.9 -14.9 11683 12136 12136 82
              "Australia" "oecd member" "no eu membership" 1993 34041 199303 "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 282
              "Australia" "oecd member" "no eu membership" 1994 . . "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 364
              "Australia" "oecd member" "no eu membership" 1995 . . "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 364
              "Australia" "oecd member" "no eu membership" 1996 35126 199603 "Australia" 1993 3 1 1 -.165 -.165 12136 13219 13219 70
              "Australia" "oecd member" "no eu membership" 1996 . . "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 295
              "Australia" "oecd member" "no eu membership" 1997 . . "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 364
              "Australia" "oecd member" "no eu membership" 1998 36071 199810 "Australia" 1996 3 3 3 22.593 22.593 13219 14173 14173 293
              "Australia" "oecd member" "no eu membership" 1998 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 71
              "Australia" "oecd member" "no eu membership" 1999 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 364
              "Australia" "oecd member" "no eu membership" 2000 . . "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 365
              "Australia" "oecd member" "no eu membership" 2001 37205 200111 "Australia" 1998 10 2 2 48.458 48.458 14173 -21220 15305 329
              "Australia" "oecd member" "no eu membership" 2001 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 35
              "Australia" "oecd member" "no eu membership" 2002 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 364
              "Australia" "oecd member" "no eu membership" 2003 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 364
              "Australia" "oecd member" "no eu membership" 2004 . . "Australia" 2001 11 2 2 33.333 30.905804 15305 -20155 16370 299
              "Australia" "oecd member" "no eu membership" 2004 38269 200410 "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 66
              "Australia" "oecd member" "no eu membership" 2005 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 364
              "Australia" "oecd member" "no eu membership" 2006 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 364
              "Australia" "oecd member" "no eu membership" 2007 . . "Australia" 2004 10 2 2 31.889 34.52776 16370 -19022 17503 336
              "Australia" "oecd member" "no eu membership" 2007 39410 200711 "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 28
              "Australia" "oecd member" "no eu membership" 2008 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 365
              "Australia" "oecd member" "no eu membership" 2009 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 364
              "Australia" "oecd member" "no eu membership" 2010 . . "Australia" 2007 12 1 1 5.674 5.674 17503 -18088 18437 174
              "Australia" "oecd member" "no eu membership" 2010 40411 201008 "Australia" 2010 6 1 1 5.674 5.674 18437 -18006 18519 82
              "Australia" "oecd member" "no eu membership" 2010 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 108
              "Australia" "oecd member" "no eu membership" 2011 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 364
              "Australia" "oecd member" "no eu membership" 2012 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 365
              "Australia" "oecd member" "no eu membership" 2013 . . "Australia" 2010 9 4 4 -34.113 -34.113 18519 -16989 19536 177
              "Australia" "oecd member" "no eu membership" 2013 41524 201309 "Australia" 2013 6 4 4 -34.113 -34.113 19536 -16906 19619 83
              "Australia" "oecd member" "no eu membership" 2013 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 104
              "Australia" "oecd member" "no eu membership" 2014 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 364
              "Australia" "oecd member" "no eu membership" 2015 . . "Australia" 2013 9 2 2 22.975 18.485498 19619 -16179 20346 257
              "Australia" "oecd member" "no eu membership" 2015 . . "Australia" 2013 9 2 2 22.975 18.485498 19616 -15105 21420 364
              "Australia" "oecd member" "no eu membership" 2016 42553 201607 "Australia" 2013 9 2 2 22.975 18.485498 19616 -15105 21420 365
              "Australia" "oecd member" "no eu membership" 2016 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 365
              "Australia" "oecd member" "no eu membership" 2017 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 364
              "Australia" "oecd member" "no eu membership" 2018 . . "Australia" 2013 9 2 2 12.24 . 19616 -15105 21420 235
              "Australia" "oecd member" "no eu membership" 2018 . . "Australia" 2013 8 2 2 12.24 . 19594 -14976 21549 364
              end
              format %td start
              format %td end
              tempfile original_dataset_1
              save `original_dataset_1'
              
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str48 country str3 countrycode int ts str6 timecode float(growth1 growth2 growth3) str7 gb1 str6(gb2 gb3) str7 d1 long(gb1_b gb2_b gb3_b d1_b)
              "Australia" "AUS" 1990 "YR1990" -.25998396 .8315406 1.496551 "7,924" "0,0965" "5,648" "16.445" 674 26 711 152
              "Australia" "AUS" 1991 "YR1991" 1.3334774 2.1083677 -1.0142618 "8,777" "0,0864" "3,078" "21.650" 705 15 627 207
              "Australia" "AUS" 1992 "YR1992" 3.2923484 3.4185865 2.523101 "8,553" "0,1099" "1,402" "27.678" 697 47 467 281
              "Australia" "AUS" 1993 "YR1993" 3.715279 2.6728065 3.833016 "7,368" "0,1498" "1,779" "30.721" 656 133 517 327
              "Australia" "AUS" 1994 "YR1994" 1.6375794 1.2805543 4.774543 "5,84" "0,1489" "1,133" "31.782" 565 130 427 336
              "Australia" "AUS" 1995 "YR1995" -.9972533 -.9632453 2.801609 "7,754" "0,2159" "2,393" "31.234" 664 252 583 332
              "Australia" "AUS" 1996 "YR1996" 2.770518 3.154481 4.078352 "6,368" "0,1598" "2,206" "29.422" 600 159 562 310
              "Australia" "AUS" 1997 "YR1997" 3.889898 3.9895246 4.4481344 "5,838" "0,1703" "1,387" "25.959" 564 180 465 262
              "Australia" "AUS" 1998 "YR1998" 2.9572344 3.7896104 4.613108 "5,414" "0,1701" "0,726" "23.754" 518 179 358 240
              "Australia" "AUS" 1999 "YR1999" 2.561527 2.339495 4.356505 "4,035" "0,1985" "1,22" "22.595" 368 221 440 221
              "Australia" "AUS" 2000 "YR2000" .3891381 .50802076 2.99929 "5,668" "0,1953" "1,461" "19.552" 550 216 475 187
              "Australia" "AUS" 2001 "YR2001" 1.1405312 3.1164474 2.561032 "5,152" "0,1514" "0,624" "17.170" 489 137 342 158
              "Australia" "AUS" 2002 "YR2002" 2.158647 2.3763344 4.093701 "5,171" "0,1963" "1,053" "15.051" 493 217 414 144
              "Australia" "AUS" 2003 "YR2003" .3566159 .7994667 2.630159 "4,268" "0,1937" "0,971" "13.221" 401 212 397 110
              "Australia" "AUS" 2004 "YR2004" 2.7113535 2.782252 4.1981416 "4,361" "0,2044" "1,299" "11.948" 419 228 453 70
              "Australia" "AUS" 2005 "YR2005" -.5891249 -.4728382 2.836245 "3,639" "0,1525" "1,696" "10.891" 311 139 502 6
              "Australia" "AUS" 2006 "YR2006" .4218182 1.0691733 2.7778394 "3,34" "0,1278" "1,865" "9.967" 275 83 526 1037
              "Australia" "AUS" 2007 "YR2007" 1.1622334 1.1005231 4.278156 "3,99" "0,1159" "1,916" "9.687" 359 58 532 1034
              "Australia" "AUS" 2008 "YR2008" -.08318187 .003607297 2.642911 "4,438" "0,1276" "2,003" "11.753" 429 82 548 69
              "Australia" "AUS" 2009 "YR2009" 1.1294768 2.830966 1.8660793 "3,871" "0,257" "1,046" "16.654" 341 283 413 153
              "Australia" "AUS" 2010 "YR2010" .3700751 -.070456676 2.3563232 "3,88" "0,2605" "2,337" "20.397" 342 287 577 193
              "Australia" "AUS" 2011 "YR2011" 1.048757 1.0357703 2.7214854 "3,451" "0,1435" "2,595" "24.065" 288 120 598 250
              "Australia" "AUS" 2012 "YR2012" 2.587376 2.995731 3.808844 "3,194" "0,2031" "1,899" "27.540" 261 227 529 279
              "Australia" "AUS" 2013 "YR2013" 1.1289984 1.2830683 2.113799 "1,754" "0,295" "1,98" "30.498" 135 310 537 323
              "Australia" "AUS" 2014 "YR2014" 1.6469382 1.930875 2.608791 "2,279" "0,2662" "2,3" "34.025" 215 292 572 360
              "Australia" "AUS" 2015 "YR2015" .3492132 .35471275 2.429927 "0,711" "0,2123" "2,289" "37.690" 60 246 570 420
              "Australia" "AUS" 2016 "YR2016" 1.0832386 1.7193124 2.741536 "0,891" "0,3553" "2,255" "40.458" 76 342 566 479
              "Australia" "AUS" 2017 "YR2017" .036598407 -.01947236 2.3533013 "0,414" "0,3673" "2,588" "41.066" 36 346 597 492
              "Australia" "AUS" 2018 "YR2018" .1107098 .7712589 2.7736175 "0,583" "0,2131" "2,248" "41.369" 49 249 565 496
              end
              label values gb1_b TR10y_yield
              label def TR10y_yield 36 "0,414", modify
              label def TR10y_yield 42 "0,514", modify
              label def TR10y_yield 49 "0,583", modify
              label def TR10y_yield 53 "0,634", modify
              label def TR10y_yield 60 "0,711", modify
              label def TR10y_yield 71 "0,824", modify
              label def TR10y_yield 76 "0,891", modify
              label def TR10y_yield 82 "0,977", modify
              label def TR10y_yield 135 "1,754", modify
              label def TR10y_yield 156 "10", modify
              label def TR10y_yield 202 "2,051", modify
              label def TR10y_yield 215 "2,279", modify
              label def TR10y_yield 232 "2,558", modify
              label def TR10y_yield 261 "3,194", modify
              label def TR10y_yield 275 "3,34", modify
              label def TR10y_yield 279 "3,365", modify
              label def TR10y_yield 288 "3,451", modify
              label def TR10y_yield 311 "3,639", modify
              label def TR10y_yield 312 "3,653", modify
              label def TR10y_yield 322 "3,716", modify
              label def TR10y_yield 328 "3,778", modify
              label def TR10y_yield 341 "3,871", modify
              label def TR10y_yield 342 "3,88", modify
              label def TR10y_yield 347 "3,928", modify
              label def TR10y_yield 354 "3,977", modify
              label def TR10y_yield 359 "3,99", modify
              label def TR10y_yield 368 "4,035", modify
              label def TR10y_yield 370 "4,051", modify
              label def TR10y_yield 394 "4,241", modify
              label def TR10y_yield 401 "4,268", modify
              label def TR10y_yield 412 "4,315", modify
              label def TR10y_yield 415 "4,345", modify
              label def TR10y_yield 419 "4,361", modify
              label def TR10y_yield 429 "4,438", modify
              label def TR10y_yield 435 "4,469", modify
              label def TR10y_yield 489 "5,152", modify
              label def TR10y_yield 493 "5,171", modify
              label def TR10y_yield 495 "5,185", modify
              label def TR10y_yield 501 "5,22", modify
              label def TR10y_yield 518 "5,414", modify
              label def TR10y_yield 522 "5,451", modify
              label def TR10y_yield 550 "5,668", modify
              label def TR10y_yield 554 "5,701", modify
              label def TR10y_yield 564 "5,838", modify
              label def TR10y_yield 565 "5,84", modify
              label def TR10y_yield 568 "5,87", modify
              label def TR10y_yield 600 "6,368", modify
              label def TR10y_yield 601 "6,397", modify
              label def TR10y_yield 618 "6,688", modify
              label def TR10y_yield 656 "7,368", modify
              label def TR10y_yield 664 "7,754", modify
              label def TR10y_yield 665 "7,76", modify
              label def TR10y_yield 674 "7,924", modify
              label def TR10y_yield 693 "8,436", modify
              label def TR10y_yield 697 "8,553", modify
              label def TR10y_yield 705 "8,777", modify
              label def TR10y_yield 707 "8,91", modify
              label def TR10y_yield 719 "9,49", modify
              label values gb2_b TR10y_volatility
              label def TR10y_volatility 15 "0,0864", modify
              label def TR10y_volatility 26 "0,0965", modify
              label def TR10y_volatility 47 "0,1099", modify
              label def TR10y_volatility 58 "0,1159", modify
              label def TR10y_volatility 82 "0,1276", modify
              label def TR10y_volatility 83 "0,1278", modify
              label def TR10y_volatility 120 "0,1435", modify
              label def TR10y_volatility 130 "0,1489", modify
              label def TR10y_volatility 133 "0,1498", modify
              label def TR10y_volatility 137 "0,1514", modify
              label def TR10y_volatility 139 "0,1525", modify
              label def TR10y_volatility 159 "0,1598", modify
              label def TR10y_volatility 179 "0,1701", modify
              label def TR10y_volatility 180 "0,1703", modify
              label def TR10y_volatility 212 "0,1937", modify
              label def TR10y_volatility 216 "0,1953", modify
              label def TR10y_volatility 217 "0,1963", modify
              label def TR10y_volatility 221 "0,1985", modify
              label def TR10y_volatility 227 "0,2031", modify
              label def TR10y_volatility 228 "0,2044", modify
              label def TR10y_volatility 246 "0,2123", modify
              label def TR10y_volatility 249 "0,2131", modify
              label def TR10y_volatility 252 "0,2159", modify
              label def TR10y_volatility 283 "0,257", modify
              label def TR10y_volatility 287 "0,2605", modify
              label def TR10y_volatility 292 "0,2662", modify
              label def TR10y_volatility 310 "0,295", modify
              label def TR10y_volatility 342 "0,3553", modify
              label def TR10y_volatility 346 "0,3673", modify
              label def TR10y_volatility 410 "0.00", modify
              label def TR10y_volatility 411 "0.01", modify
              label def TR10y_volatility 412 "0.02", modify
              label def TR10y_volatility 413 "0.03", modify
              label def TR10y_volatility 414 "0.04", modify
              label def TR10y_volatility 415 "0.05", modify
              label def TR10y_volatility 416 "0.06", modify
              label def TR10y_volatility 417 "0.07", modify
              label def TR10y_volatility 418 "0.08", modify
              label def TR10y_volatility 419 "0.09", modify
              label def TR10y_volatility 420 "0.10", modify
              label values gb3_b spreads
              label def spreads 16 "-0,113", modify
              label def spreads 18 "-0,145", modify
              label def spreads 23 "-0,215", modify
              label def spreads 125 "0,005", modify
              label def spreads 130 "0,018", modify
              label def spreads 131 "0,019", modify
              label def spreads 138 "0,03", modify
              label def spreads 139 "0,031", modify
              label def spreads 144 "0,044", modify
              label def spreads 150 "0,06", modify
              label def spreads 159 "0,079", modify
              label def spreads 165 "0,09", modify
              label def spreads 171 "0,107", modify
              label def spreads 174 "0,114", modify
              label def spreads 180 "0,126", modify
              label def spreads 181 "0,127", modify
              label def spreads 187 "0,145", modify
              label def spreads 195 "0,159", modify
              label def spreads 201 "0,169", modify
              label def spreads 202 "0,17", modify
              label def spreads 216 "0,202", modify
              label def spreads 218 "0,207", modify
              label def spreads 221 "0,21", modify
              label def spreads 222 "0,214", modify
              label def spreads 228 "0,228", modify
              label def spreads 229 "0,229", modify
              label def spreads 235 "0,245", modify
              label def spreads 239 "0,257", modify
              label def spreads 241 "0,261", modify
              label def spreads 249 "0,283", modify
              label def spreads 255 "0,293", modify
              label def spreads 262 "0,316", modify
              label def spreads 264 "0,328", modify
              label def spreads 265 "0,329", modify
              label def spreads 269 "0,338", modify
              label def spreads 270 "0,343", modify
              label def spreads 282 "0,384", modify
              label def spreads 285 "0,394", modify
              label def spreads 300 "0,449", modify
              label def spreads 309 "0,492", modify
              label def spreads 314 "0,531", modify
              label def spreads 323 "0,565", modify
              label def spreads 331 "0,599", modify
              label def spreads 338 "0,617", modify
              label def spreads 342 "0,624", modify
              label def spreads 355 "0,714", modify
              label def spreads 358 "0,726", modify
              label def spreads 362 "0,746", modify
              label def spreads 371 "0,789", modify
              label def spreads 376 "0,834", modify
              label def spreads 379 "0,85", modify
              label def spreads 388 "0,922", modify
              label def spreads 389 "0,927", modify
              label def spreads 397 "0,971", modify
              label def spreads 409 "0.672", modify
              label def spreads 410 "1,008", modify
              label def spreads 413 "1,046", modify
              label def spreads 414 "1,053", modify
              label def spreads 427 "1,133", modify
              label def spreads 440 "1,22", modify
              label def spreads 452 "1,293", modify
              label def spreads 453 "1,299", modify
              label def spreads 465 "1,387", modify
              label def spreads 467 "1,402", modify
              label def spreads 475 "1,461", modify
              label def spreads 502 "1,696", modify
              label def spreads 517 "1,779", modify
              label def spreads 526 "1,865", modify
              label def spreads 529 "1,899", modify
              label def spreads 532 "1,916", modify
              label def spreads 537 "1,98", modify
              label def spreads 548 "2,003", modify
              label def spreads 562 "2,206", modify
              label def spreads 563 "2,238", modify
              label def spreads 565 "2,248", modify
              label def spreads 566 "2,255", modify
              label def spreads 570 "2,289", modify
              label def spreads 572 "2,3", modify
              label def spreads 577 "2,337", modify
              label def spreads 578 "2,34", modify
              label def spreads 583 "2,393", modify
              label def spreads 597 "2,588", modify
              label def spreads 598 "2,595", modify
              label def spreads 627 "3,078", modify
              label def spreads 711 "5,648", modify
              label values d1_b debt
              label def debt 6 "10.891", modify
              label def debt 16 "101.113", modify
              label def debt 22 "102.030", modify
              label def debt 28 "102.592", modify
              label def debt 32 "103.401", modify
              label def debt 34 "104,33", modify
              label def debt 36 "104.332", modify
              label def debt 40 "104.711", modify
              label def debt 46 "105.452", modify
              label def debt 49 "106.109", modify
              label def debt 50 "106.342", modify
              label def debt 55 "107.523", modify
              label def debt 56 "107.592", modify
              label def debt 59 "108.769", modify
              label def debt 69 "11.753", modify
              label def debt 70 "11.948", modify
              label def debt 77 "114.417", modify
              label def debt 85 "118.196", modify
              label def debt 96 "123.205", modify
              label def debt 103 "127.973", modify
              label def debt 108 "129.571", modify
              label def debt 110 "13.221", modify
              label def debt 113 "130.544", modify
              label def debt 115 "131.052", modify
              label def debt 122 "133.923", modify
              label def debt 123 "136.308", modify
              label def debt 126 "138.141", modify
              label def debt 140 "141,31", modify
              label def debt 144 "15.051", modify
              label def debt 152 "16.445", modify
              label def debt 153 "16.654", modify
              label def debt 158 "17.170", modify
              label def debt 165 "170,64", modify
              label def debt 167 "172,3", modify
              label def debt 187 "19.552", modify
              label def debt 193 "20.397", modify
              label def debt 207 "21.650", modify
              label def debt 221 "22.595", modify
              label def debt 240 "23.754", modify
              label def debt 250 "24.065", modify
              label def debt 262 "25.959", modify
              label def debt 279 "27.540", modify
              label def debt 281 "27.678", modify
              label def debt 306 "289,55", modify
              label def debt 310 "29.422", modify
              label def debt 323 "30.498", modify
              label def debt 327 "30.721", modify
              label def debt 332 "31.234", modify
              label def debt 336 "31.782", modify
              label def debt 360 "34.025", modify
              label def debt 420 "37.690", modify
              label def debt 479 "40.458", modify
              label def debt 492 "41.066", modify
              label def debt 496 "41.369", modify
              label def debt 669 "53.819", modify
              label def debt 698 "55.932", modify
              label def debt 700 "56.028", modify
              label def debt 704 "56.134", modify
              label def debt 768 "60.629", modify
              label def debt 772 "61.103", modify
              label def debt 791 "63.056", modify
              label def debt 798 "63.721", modify
              label def debt 817 "64.740", modify
              label def debt 818 "64.849", modify
              label def debt 821 "64.896", modify
              label def debt 831 "65.738", modify
              label def debt 841 "66.355", modify
              label def debt 846 "66.978", modify
              label def debt 848 "67.001", modify
              label def debt 855 "67.574", modify
              label def debt 858 "67.807", modify
              label def debt 861 "67.865", modify
              label def debt 871 "68.318", modify
              label def debt 873 "68.417", modify
              label def debt 881 "68.819", modify
              label def debt 927 "73.754", modify
              label def debt 928 "73.859", modify
              label def debt 945 "76.502", modify
              label def debt 954 "78.489", modify
              label def debt 962 "79.406", modify
              label def debt 963 "79.580", modify
              label def debt 985 "81.011", modify
              label def debt 990 "81.661", modify
              label def debt 993 "82.178", modify
              label def debt 995 "82.418", modify
              label def debt 997 "82.866", modify
              label def debt 1002 "83.758", modify
              label def debt 1006 "84.404", modify
              label def debt 1019 "87.028", modify
              label def debt 1034 "9.687", modify
              label def debt 1037 "9.967", modify
              label def debt 1042 "91.049", modify
              label def debt 1047 "92.531", modify
              label def debt 1052 "94.677", modify
              label def debt 1061 "96,44", modify
              label def debt 1062 "96.524", modify
              label def debt 1078 "99.536", modify
              label def debt 1079 "99.720", modify
              tempfile dataset2
              save `dataset2'
              
              // CODE BEGINS HERE
              use `original_dataset_1', clear
              gen long obs_no = _n
              assert !missing(in_year_duration)
              tempfile the_whole_thing
              save `the_whole_thing'
              
              // REDUCE TO GOVERNMENT WITH LONGEST IN YEAR DURATION
              by country ts (in_year_duration), sort: keep if _n == _N
              
              // BRING IN THE MACRO DATA
              merge 1:m country ts using `dataset2', nogenerate
              
              // BRING BACK THE OTHER OBSERVATIONS
              merge 1:1 obs_no using `the_whole_thing', nogenerate
              sort country ts in_year_duration
              Unless you come up with a different example where the code fails, I'm going to consider this problem solved. The code is actually pretty simple, and I cannot think of what could be in the data that would break it.

              I will just comment that, while I don't know what analyses you plan to do with this, it is hard for me to imagine that they can be done with variables gb1_b gb2_ fb3_b and d1_b, which are value labeled integers: no calculations that you do with them will properly reflect the values that you see when you -list- or -browse- or -display- these. Keeping them as strings has avoided getting an error message, but it by no means solves the problem. You indicated that you found the non-numeric material in them: you have to do something to eliminate that non-numeric material and then successfully -destring- these (after -decode-ing them). Otherwise any calculations you do with them will be very, very wrong.
              First, let me thank you a wholeheartedly for the code and work! I really appreciate it and mostly your time spent on my request and cannot thank you enough! The task is to match/merge the politics variables of the longest in year duration of governments (a variable created with your code) to the macroeconomics variables ,while preserving all other variables of the politics dataset. I have run the code you suggested me and it works till the point of the merge. Once I try to run the merge command .the last line of your code, of the two datasets , a mess is happening. Either I got errors such as " variables country ts do not uniquely identify observations in the master data" or variable obs_no does not uniquely identify observations in the master data or other similar errors. . I do not mean to take advance of your time and kindness. I am working on original datasets for academic research and publication which, for obvious reasons you can perfectly understand, I cannot share on a web forum seeking for help. For the politics vars I have in some years some duplicates and need to preserve them .


              Here ia an example of how the finale dataset should look like.created in excel
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str15 ccode str14 oecdmember str16 eumember long(ts edate date) str15 country int startyear byte(startmonth startday t1 t2) float(pm gv start) int time float(end in_year_duration) double(growth1 growth2) float growth3
              "Australia" "oecd member" "no eu membership" 1990     .      . "Australia" 1987  7 22 1 1   -4.5   -4.5 10064  11051 11051  93          .          .          .
              "Australia" "oecd member" "no eu membership" 1990     .      . "Australia" 1990  4  4 1 1  -14.9  -14.9 11051  11683 11683 271 -259983966  831540573   1.496551
              "Australia" "oecd member" "no eu membership" 1991     .      . "Australia" 1990  4  4 1 1  -14.9  -14.9 11051  11683 11683 360       1.33 2108367675 -1.0142618
              "Australia" "oecd member" "no eu membership" 1991     .      . "Australia" 1991 12 27 1 1  -14.9  -14.9 11683  12136 12136   4          .          .          .
              "Australia" "oecd member" "no eu membership" 1992     .      . "Australia" 1991 12 27 1 1  -14.9  -14.9 11683  12136 12136 365 3292348391       3.41   2.523101
              "Australia" "oecd member" "no eu membership" 1993     .      . "Australia" 1991 12 27 1 1  -14.9  -14.9 11683  12136 12136  82          .          .          .
              "Australia" "oecd member" "no eu membership" 1993 34041 199303 "Australia" 1993  3 24 1 1  -.165  -.165 12136  13219 13219 282 3715278988 2672806511   3.833016
              "Australia" "oecd member" "no eu membership" 1994     .      . "Australia" 1993  3 24 1 1  -.165  -.165 12136  13219 13219 364 1637579471 1280554325   4.774543
              "Australia" "oecd member" "no eu membership" 1995     .      . "Australia" 1993  3 24 1 1  -.165  -.165 12136  13219 13219 364 -997253314 -963245348   2.801609
              "Australia" "oecd member" "no eu membership" 1996 35126 199603 "Australia" 1993  3 24 1 1  -.165  -.165 12136  13219 13219  70          .          .          .
              "Australia" "oecd member" "no eu membership" 1996     .      . "Australia" 1996  3 11 3 3 22.593 22.593 13219  14173 14173 295 2770517787 3154481062   4.078352
              "Australia" "oecd member" "no eu membership" 1997     .      . "Australia" 1996  3 11 3 3 22.593 22.593 13219  14173 14173 364 3889897754 3989524718  4.4481344
              "Australia" "oecd member" "no eu membership" 1998 36071 199810 "Australia" 1996  3 11 3 3 22.593 22.593 13219  14173 14173 293 2957234305 3789610374   4.613108
              "Australia" "oecd member" "no eu membership" 1998     .      . "Australia" 1998 10 21 2 2 48.458 48.458 14173 -21220 15305  71          .          .          .
              "Australia" "oecd member" "no eu membership" 1999     .      . "Australia" 1998 10 21 2 2 48.458 48.458 14173 -21220 15305 364 2561527027 2339494953   4.356505
              "Australia" "oecd member" "no eu membership" 2000     .      . "Australia" 1998 10 21 2 2 48.458 48.458 14173 -21220 15305 365  389138136  508020759    2.99929
              end
              format %td start
              format %td end


              I took the initiative to writing you in a private email at you professional email address.(please also check your spam folder) . I do understand this is something to be addressed on the forums only and if you think this is inappropriate please accept my most sincere my apologies . I do not mean to take advance of your time and kindness.

              Thank you again for your assistance in this matter.I am really indebted to you

              Best regards

              Mario Ferri

              Comment


              • #22
                Either I got errors such as " variables country ts do not uniquely identify observations in the master data" or variable obs_no does not uniquely identify observations in the master data or other similar errors.
                This is absolutely bizarre, and I can't imagine what is going wrong here. The line immediately before the -merge- is
                Code:
                by country ts (in_year_duration), sort: keep if _n == _N
                That line of code guarantees that country and ts will uniquely identify the observations that remain at that point. If you have inserted additional code between those lines, then the problem is arising from that. But if you are running this code exactly as shown, then there is something wrong with your Stata installation, because no matter what is in the data, that command necessarily reduces the data set to one observation per country ts pair.

                Similarly, the variable obs_no is generated by -gen long obs_no = _n-. Now, the only way this can produce duplicate values of obs_no is if the number of observations in your data set exceeds the maximum value of a long integer, 2,147,483,620. Given the nature of the problem you are working on, I'd be astonished if you have over 2 billion observations in your data set. I similarly cannot find any possible explanation for that message in the -merge 1:1 obs_no- command.

                I do understand how the final data set should look, and when I run my code with the example data sets you have given me, that is, in fact, what it looks like. Do you run into these difficulties when you run my code with those example data sets on your installation? If so, then there is something wrong with your Stata. If the example data sets shown work with my code on your installation, can you come up with some other example data sets that produce these problems and post back with those?

                Comment


                • #23
                  Originally posted by Clyde Schechter View Post
                  This is absolutely bizarre, and I can't imagine what is going wrong here. The line immediately before the -merge- is
                  Code:
                  by country ts (in_year_duration), sort: keep if _n == _N
                  That line of code guarantees that country and ts will uniquely identify the observations that remain at that point. If you have inserted additional code between those lines, then the problem is arising from that. But if you are running this code exactly as shown, then there is something wrong with your Stata installation, because no matter what is in the data, that command necessarily reduces the data set to one observation per country ts pair.

                  Similarly, the variable obs_no is generated by -gen long obs_no = _n-. Now, the only way this can produce duplicate values of obs_no is if the number of observations in your data set exceeds the maximum value of a long integer, 2,147,483,620. Given the nature of the problem you are working on, I'd be astonished if you have over 2 billion observations in your data set. I similarly cannot find any possible explanation for that message in the -merge 1:1 obs_no- command.

                  I do understand how the final data set should look, and when I run my code with the example data sets you have given me, that is, in fact, what it looks like. Do you run into these difficulties when you run my code with those example data sets on your installation? If so, then there is something wrong with your Stata. If the example data sets shown work with my code on your installation, can you come up with some other example data sets that produce these problems and post back with those?
                  No, I have never faced a such problem and your code works fine till the second -merge command . I am using a legal copy of stata licensed to the university ,so there should no be any kind of problem, bug or other (version SE. 15.1). I also tried to another copy of stata ,similar errors occurred. I have some few thousands of observations but certainty not 2 billion ones. I have followed meticulously all steps from your latest code . It works perfectly til the second - merge command So , in order to make sure I did not commit any mistake, here what I did.
                  Code:
                    
                   use `original_dataset_1', clear    
                   gen long obs_no = _n assert !missing(in_year_duration) tempfile the_whole_thing save `the_whole_thing'
                  Code:
                    
                   // REDUCE TO GOVERNMENT WITH LONGEST IN YEAR DURATION by country ts (in_year_duration), sort: keep if _n == _N
                  Code:
                  // BRING IN THE MACRO DATA merge 1:m country ts using `dataset2', nogenerate
                  Code:
                  // BRING BACK THE OTHER OBSERVATIONS merge 1:1 obs_no using `the_whole_thing', nogenerate sort country ts in_year_duration
                  The second -merge line turns that
                  Code:
                  . merge 1:1 obs_no using `the_whole_thing', nogenerate variable obs_no does not uniquely identify observations in the master data r(459);
                  It is really frustrating.Not sure what is gong wrong.Is there a way to display who are those obs_no not uniquely identify observations in the master data? Maybe because I am using the full datasets and you just a small sample. I do not think to be this the problem, but just made a thought.

                  I have sent you an email with more details and data(check also you spam folder). Please kindly take a few minutes to check better there a perhaps to reply me by sending me the merged file back(compatible file with my stata versions). It is just the last line of the merge code before finish finalising the dataset.I f there are other way to solve ,i would be grateful!

                  Thank you!

                  Mario Ferri
                  Last edited by Mario Ferri; 10 Feb 2020, 20:33.

                  Comment


                  • #24
                    It is really frustrating.Not sure what is gong wrong.Is there a way to display who are those obs_no not uniquely identify observations in the master data?

                    Yes. Since it is the second -merge- that is failing, do this after the first merge:
                    Code:
                    duplicates tag obs_no, gen(flag_obs_no)
                    browse if flag_obs_no

                    I have sent you an email with more details and data(check also you spam folder).

                    I have not received it (and it is not in my spam folder either). We recently changed email domains (the current one is the one shown on my profile in this forum) and there have been some problems with emails getting lost. If you sent it more than a few hours ago, I suggest re-sending it. Bear in mind that I will not respond privately--but if working with the information you send me I find a solution to the problem, I will post the solution here.

                    Comment


                    • #25
                      Wait, I just noticed something. When I first came up with code in #15, the first -merge- command was -merge 1:1 country ts…-. In #20 it somehow morphed into -merge 1:m country ts…- and I do not know why. But that creates the possibility that multiple observations will have the same value of obs_no after that first -merge-, and that would cause the problem you are experiencing with the second -merge-.

                      BUT, that possibility would only happen if your macro data set contains some observations that repeat some combination of country and ts. Now, does that happen in data set 2? (The problem with the second merge suggests that it does) And, if so, should it? Why would you have two sets of macro data for the same country in the same year?

                      So here's what I think you should do. Open your dataset 2 and run
                      Code:
                      duplicates tag country ts, gen(country_ts_flag)
                      browse if country_ts_flag
                      and see what's going on there. If these observations are complete duplicates in every variable, then you can solve the problem by running -duplicates drop-. (Although, I would then suggest you review the data management that created dataset 2 to try to figure out why you ended up with some duplicate observations--it often indicates that a mistake was made somewhere, and where there is one mistake, there are often others.) If, however, these observations disagree on some variables, you need to figure out which one to keep, or how to combine them, or perhaps both are valid in some sense. If you end up reducing data set 2 to a single observation per country-year combination, then change the -merge 1:m country ts…- command to -merge 1:1 country ts…- and things should be fine. If you decide that you really need to keep these multiple observations, then change the second -merge- command to -merge m:1 obs_no...- and it should run properly.

                      Comment


                      • #26
                        Originally posted by Clyde Schechter View Post
                        Wait, I just noticed something. When I first came up with code in #15, the first -merge- command was -merge 1:1 country ts…-. In #20 it somehow morphed into -merge 1:m country ts…- and I do not know why. But that creates the possibility that multiple observations will have the same value of obs_no after that first -merge-, and that would cause the problem you are experiencing with the second -merge-.

                        BUT, that possibility would only happen if your macro data set contains some observations that repeat some combination of country and ts. Now, does that happen in data set 2? (The problem with the second merge suggests that it does) And, if so, should it? Why would you have two sets of macro data for the same country in the same year?

                        So here's what I think you should do. Open your dataset 2 and run
                        Code:
                        duplicates tag country ts, gen(country_ts_flag)
                        browse if country_ts_flag
                        and see what's going on there. If these observations are complete duplicates in every variable, then you can solve the problem by running -duplicates drop-. (Although, I would then suggest you review the data management that created dataset 2 to try to figure out why you ended up with some duplicate observations--it often indicates that a mistake was made somewhere, and where there is one mistake, there are often others.) If, however, these observations disagree on some variables, you need to figure out which one to keep, or how to combine them, or perhaps both are valid in some sense. If you end up reducing data set 2 to a single observation per country-year combination, then change the -merge 1:m country ts…- command to -merge 1:1 country ts…- and things should be fine. If you decide that you really need to keep these multiple observations, then change the second -merge- command to -merge m:1 obs_no...- and it should run properly.

                        I have just tried. It is failing again. I also opened the dataset2 as suggested and run
                        Code:
                        duplicates tag country ts, gen(country_ts_flag) browse if country_ts_flag
                        and it was totally empty.No duplicates there I guess.

                        Now I also run after the first merged
                        Code:
                        duplicates tag obs_no, gen(flag_obs_no) browse if flag_obs_no
                        There were 117 flag_obs_no adn and a really mess of data. Some data were not corresponding at any variable at all.. A real disaster.
                        I emailed you to your academic email address posted on the School website. ( [email protected]). If this is valid, I will try to resend it. Please also check the pm I 've just send you .

                        Comment


                        • #27
                          Mystery solved! There were two problems contributing to the mess.

                          Problem 1: the real first data set has four observations for which the variable country is missing. This is compatible with the code, but probably you are getting incorrect results, as the information in those observations is not being taken into account in any of the calculations or results, as it is not attributable to any country.. You can find them easily with -list country ts if missing(country)- and you can fix that.

                          Problem 2, the more serious problem: in the real second data set (but not in the examples) there are observations for country-year pairs that do not exist in the first data set. When the first -merge- is done, those surplus observations were brought into the data set, but because they don't match anything in the first data set, they have missing value for obs_no in the merged result. Then the second -merge- breaks because there are 117 (actually I think it was 118) observations that all have the same, missing, value of obs_no. The solution is not to bring those in with the first merge.

                          Working code:
                          Code:
                          clear*
                          use "politics_final copy.dta" // i.e dataset1
                          
                          // CODE BEGINS HERE
                          gen long obs_no = _n
                          assert !missing(in_year_duration)
                          tempfile the_whole_thing
                          save `the_whole_thing'
                          
                          // REDUCE TO GOVERNMENT WITH LONGEST IN YEAR DURATION
                          by country ts (in_year_duration), sort: keep if _n == _N
                          
                          // BRING IN THE MACRO DATA
                          merge 1:1 country ts using "economics2 copy.dta", keep(match master) nogenerate // i.e. dataset2
                          
                          // BRING BACK THE OTHER OBSERVATIONS
                          merge 1:1 obs_no using `the_whole_thing', nogenerate
                          sort country ts in_year_duration
                          The only substantive change is shown in bold italics.

                          Comment


                          • #28
                            Originally posted by Clyde Schechter View Post
                            Mystery solved! There were two problems contributing to the mess.

                            Problem 1: the real first data set has four observations for which the variable country is missing. This is compatible with the code, but probably you are getting incorrect results, as the information in those observations is not being taken into account in any of the calculations or results, as it is not attributable to any country.. You can find them easily with -list country ts if missing(country)- and you can fix that.

                            Problem 2, the more serious problem: in the real second data set (but not in the examples) there are observations for country-year pairs that do not exist in the first data set. When the first -merge- is done, those surplus observations were brought into the data set, but because they don't match anything in the first data set, they have missing value for obs_no in the merged result. Then the second -merge- breaks because there are 117 (actually I think it was 118) observations that all have the same, missing, value of obs_no. The solution is not to bring those in with the first merge.

                            Working code:
                            Code:
                            clear*
                            use "politics_final copy.dta" // i.e dataset1
                            
                            // CODE BEGINS HERE
                            gen long obs_no = _n
                            assert !missing(in_year_duration)
                            tempfile the_whole_thing
                            save `the_whole_thing'
                            
                            // REDUCE TO GOVERNMENT WITH LONGEST IN YEAR DURATION
                            by country ts (in_year_duration), sort: keep if _n == _N
                            
                            // BRING IN THE MACRO DATA
                            merge 1:1 country ts using "economics2 copy.dta", keep(match master) nogenerate // i.e. dataset2
                            
                            // BRING BACK THE OTHER OBSERVATIONS
                            merge 1:1 obs_no using `the_whole_thing', nogenerate
                            sort country ts in_year_duration
                            The only substantive change is shown in bold italics.
                            Thank you for sending me the information about solving my query . I am grateful for the amount of time and effort you put into this helping me. Your insights are beneficial.

                            I have now run the full code and created the dataset in the desired way. Moreover, thanks to you I moved to the next step of the project and cannot thank you enough.

                            I was already aware of the four observations in the politics data set that have no value specified for country. Those are referred to countries gained their independence after 1990 start period of my dataset (Slovakia and Czech Republic in 1993, Slovenia 1992, Baltic countries in 1991). It was expected not to have observation for them for previous years given they were not existed. I have now deleted them

                            On another point, an issue which really did not understand, in my economics dataset four variables are coded as long. I downloaded the data normally. For some of them I needed to make some calculations in order to obtain them since were not available from the original source (annualized daily volatility on their returns). As I already had the stata program I imported them routinely into the software and run it as usual. It was much faster and less time consuming doing that there than in excel. Once they were generated, I copied them back into excel to finish the dataset and when the hole dataset was completed, I imported it into stata. Then Stata read them as strings. I run the destring command first(failed) and then the encode command obtaining them as long. I then tried in vain to converted them into real numbers using your program:
                            Code:
                            foreach v of varlist var1 var2 var3 var4 {
                                decode `v', gen(_`v')
                                drop `v'
                                rename _`v' `v'
                                destring `v', dpcomma replace
                            }
                            It displayed that
                            Code:
                              contains nonnumeric characters; no replace
                            and were converted back to strings. It is odd Stata not to able to read them as real number given that they were produced by it. I pasted them back to excel and it does read them as real numbers being able to do calculation as well. I cannot really figure why that happened and further how to locate the nonnumeric characters in order to correct them. I cannot really see a way to manage this.


                            Thank you once again for your time spend dealing with my inquire and consideration. I am grateful to you and appreciate your help with this situation.


                            Best regards,

                            Mario Ferri

                            Comment


                            • #29
                              So, I looked at your data sets. The variables that are failing to destring have the following "pathology": the values of the variable contain both . and , as decimal points. You can't, in Stata, mix those in a single variable and use -destring-. That's because if you add the -dpcomma- option, it chokes on the . decimal points and if you don't, it chokes on the , decimal points. I think the simplest solution is to convert all the commas to .s, and then you can use destring successfully:

                              Code:
                              foreach v of varlist var1 var2 var3 var4 {
                                  replace `v' = subinstr(`v', ",", ".", .)
                                  destring `v', replace
                              }

                              Comment


                              • #30
                                Originally posted by Clyde Schechter View Post
                                So, I looked at your data sets. The variables that are failing to destring have the following "pathology": the values of the variable contain both . and , as decimal points. You can't, in Stata, mix those in a single variable and use -destring-. That's because if you add the -dpcomma- option, it chokes on the . decimal points and if you don't, it chokes on the , decimal points. I think the simplest solution is to convert all the commas to .s, and then you can use destring successfully:

                                Code:
                                foreach v of varlist var1 var2 var3 var4 {
                                replace `v' = subinstr(`v', ",", ".", .)
                                destring `v', replace
                                }
                                Thank you for everything! I am really indebted to you!

                                Comment

                                Working...
                                X