Announcement

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

  • nearest neighbor matching

    Dear all,

    I know that there are a lot of topics on this matter, but unfortunately I cant figure out how to do it in my particular case. This is my last resort, I really hope somebody can help me out.

    I have a dataset of 105 firms that issued a green bond, the treated firms. Furthermore, I constructed a dataset of firms which did not issue a green bond, but which are in the same country and same industry, the control firms( I did this manually, so for each treated firm I gathered every firm which is in the same country & industry).

    Now I want to match these 105 treated firms to the nearest neighbor in this control firm dataset, and drop the treated firms of which I cant find a match. For this I want to use several variables:
    treated and control must be in same country and industry, and be the nearest neighbor based on: ESG score, environmental pillar score, env innovation score, total assets, net income, market value, total debt and Return on Assets.

    Now I read a lot about psmatch2, teffects nnmatch, teffects psmatch and joinby/joinrange, but I still cant figure it out. My problem is with identifiying the matches, I want to know specifically which treated firm is matched with which control firm, to do further analysis on these pairs.

    My initial dataset from excel is in wide format, thus variables are arranged as follows: ESG2012 ESG 2013 ESG2014.... But I guess I need it in long format thus;
    ID ESG
    2012 ... ...
    2013 ... ...
    2014 ... ...


    Furthermore, I am aware of the fact that for the variables issuedate & matchyear, I have only inputs for the treated firms and not for the control firms (see data example). This is because my goal is to match the firms in the year before issuance ( this matchyear= issue date- 1 year).


    I used the following code so far:

    Code:
    clear all
    
    cd "..."
    
    sysdir set PLUS "...."
    
    import excel "....."
    
    
    qui destring ESGscore* envpillarscore* envinnovation* CO2* totalassets* netincome* Marketvalue* totaldebt* ROA*, force replace
    
    gen ID= _n
    order ID, first
    
    save temp_fileALL.dta, replace
    
    use  temp_fileALL.dta, clear
    
    
    reshape long ESGscore envpillarscore envinnovation CO2 totalassets netincome Marketvalue totaldebt ROA, i(ID) j(year)
    
    
    drop in 329/672
    
    logit treated  IndustryCode countrycode
    predict pscore
    teffects psmatch (pscore) (treated ESGscore envpillarscore envinnovation CO2 totalassets netincome Marketvalue totaldebt ROA), osample(Unobserved) vce(robust)
    drop if Unobserved == 1  
    teffects psmatch (pscore) (treated ESGscore envpillarscore envinnovation CO2 totalassets netincome Marketvalue totaldebt ROA), vce(robust) gen(match) atet nn(1)

    the problem with the last row is that I get matches, but they are not in the same country/industry, so not correct. Furthermore, it does not take into account the fact that I want to match in the 'matchyear'.

    Does somebody has a solution/ better idea how to solve for this? I will be forever thankful.



    Here a small sample of my dataset of both the control and treated combined:
    I have also separate datasets of the treated and control firms, in the case that is more easy.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float ID int year byte treated int(issuedate matchyear) byte(countrycode IndustryCode) double(ESGscore totalassets ROA)
     1 2012 0     .    .  1 27 66.2457824760244    4.1758e+10    .5911
     1 2013 0     .    .  1 27 62.9380001633186   4.25283e+10    .7055
     1 2014 0     .    .  1 27 61.4641120634756    4.6905e+10    .7876
     1 2015 0     .    .  1 27 67.4532742537313    4.8018e+10   .83656
     1 2016 0     .    .  1 27 66.8599233529028    5.0853e+10    .7486
     1 2017 0     .    .  1 27 67.9544809247538    5.1658e+10    .7957
     1 2018 0     .    .  1 27  68.622678475577     5.298e+10   .73638
     1 2019 0     .    .  1 27 65.8091548459849             .        .
     2 2012 0     .    .  1 27 67.6176384045335   5.72378e+10   .60256
     2 2013 0     .    .  1 27 76.3366252245631   6.02725e+10    .6244
     2 2014 0     .    .  1 27 75.4096938951136   6.50629e+10    .6644
     2 2015 0     .    .  1 27 73.7283675373134   6.60288e+10   .67444
     2 2016 0     .    .  1 27 75.7162997390737   6.85727e+10   .64889
     2 2017 0     .    .  1 27 76.7183939873417   7.14155e+10   .70089
     2 2018 0     .    .  1 27 76.3741814278046   7.14398e+10   .64675
     2 2019 0     .    .  1 27 73.9373118088116             .        .
     3 2012 0     .    . 21 27 70.9345094086021  2.449591e+12   .57636
     3 2013 0     .    . 21 27 66.0918692317691  2.383951e+12   .59542
     3 2014 0     .    . 21 27 68.1731720725673  2.484721e+12   .58689
     3 2015 0     .    . 21 27 62.2513681592039  2.816676e+12   .54409
     3 2016 0     .    . 21 27 65.2514313312258  2.522133e+12    .6347
     3 2017 0     .    . 21 27 73.5190324419568   2.62758e+12   .60322
     3 2018 0     .    . 21 27 69.8513486312399  2.766977e+12   .61433
     3 2019 0     .    . 21 27 74.7859366447728             .        .
     4 2012 0     .    . 21 27 77.8569220430107   7.01131e+11   .42839
     4 2013 0     .    . 21 27 76.6504127128558   6.68178e+11   .49477
     4 2014 0     .    . 21 27 72.3085486531061   6.30434e+11     .531
     4 2015 0     .    . 21 27 73.9584577114427   6.69342e+11   .56946
     4 2016 0     .    . 21 27 81.4865823674042   6.46868e+11   .51708
     4 2017 0     .    . 21 27 82.8377772595356   6.15659e+11   .51911
     4 2018 0     .    . 21 27 79.4671900161031   5.81612e+11     .574
     4 2019 0     .    . 21 27 79.4671900161031             .        .
     5 2012 0     .    . 21 27 72.8064180107526  1.857065e+12    .7325
     5 2013 0     .    . 21 27 77.3264493695567   1.84686e+12   .79046
     5 2014 0     .    . 21 27 76.1074308227964  1.824102e+12   .89767
     5 2015 0     .    . 21 27 79.5152294085129  2.121297e+12   .75654
     5 2016 0     .    . 21 27 79.6658236740428  2.148855e+12   .84533
     5 2017 0     .    . 21 27 83.2784579705638  2.154203e+12    .8593
     5 2018 0     .    . 21 27 84.3029388083735  2.212636e+12    .9338
     5 2019 0     .    . 21 27 79.7103345648604             .        .
     6 2012 0     .    . 22 27 33.8208280416561 1594613438000    .5575
     6 2013 0     .    . 22 27 41.3013908240445 1613823532000   .55867
     6 2014 0     .    . 22 27 41.3013908240445 1700055541000     .641
     6 2015 0     .    . 22 27 39.1983644859813 1815872967000   .63733
     6 2016 0     .    . 22 27 55.1990507089641 1906257219000     .682
     6 2017 0     .    . 22 27 55.1990507089641 2005151504000    .6235
     6 2018 0     .    . 22 27 59.6559716921119 2.0362584e+12      .59
     6 2019 0     .    . 22 27 64.9517876817647             .        .
     7 2012 0     .    . 22 27 41.3559869685964  333947379000        .
     7 2013 0     .    . 22 27  37.755397510795  521698790000     1.51
     7 2014 0     .    . 22 27 39.5017291998407  647943022000    3.255
     7 2015 0     .    . 22 27 61.4538965477953  919701218000   1.3065
     7 2016 0     .    . 22 27 75.4347426786143  866719175000       .7
     7 2017 0     .    . 22 27 63.5285861959287  892197509000        .
     7 2018 0     .    . 22 27 63.5285861959287 2392524390000        .
     7 2019 0     .    . 22 27 73.4050797283062             .        .
     8 2012 0     .    . 22 27 35.6617442854091 1979259175000     .446
     8 2013 0     .    . 22 27 37.1076209195859 2062013289000   .48667
     8 2014 0     .    . 22 27 35.7937395159357 2164729432000      .57
     8 2015 0     .    . 22 27 35.7937395159357 2259951646000     .639
     8 2016 0     .    . 22 27 38.4938931297709 2350288574000    .6205
     8 2017 0     .    . 22 27 65.2002889100933 2545941962000   .50667
     8 2018 0     .    . 22 27 64.9154649909798 2573419750000     .636
     8 2019 0     .    . 22 27 64.9154649909798             .        .
     9 2012 0     .    . 22 27 34.7293878587757 2087837113000   .50462
     9 2013 0     .    . 22 27 49.7325213972929 2124278888000   .58133
     9 2014 0     .    . 22 27  41.614312245387 2263385352000   .58491
     9 2015 0     .    . 22 27 59.7961091489119 2355708683000   .65433
     9 2016 0     .    . 22 27  65.324368283723 2500095812000    .6944
     9 2017 0     .    . 22 27  65.324368283723 2541156335000    .6376
     9 2018 0     .    . 22 27 69.4354259435962 2634058605000    .7015
     9 2019 0     .    . 22 27 76.1333436247623             .        .
    10 2012 0     .    . 22 27  40.682626575518 2629122262000   .60409
    10 2013 0     .    . 22 27 34.2325120650241 2727365846000  1.79688
    10 2014 0     .    . 22 27 56.4053019410496 2870986686000     .626
    10 2015 0     .    . 22 27 56.7156374615472 1382953378000    .9968
    10 2016 0     .    . 22 27 73.7465238438562 1520230642000    .8734
    10 2017 0     .    . 22 27 71.0063049989397 1576985727000    .8114
    10 2018 0     .    . 22 27 74.3978696741854 1677520316000    .8298
    10 2019 0     .    . 22 27 74.3978696741854             .        .
    11 2012 0     .    . 22 27 69.3539624079248 2025068494000   .88108
    11 2013 0     .    . 22 27 65.5116689888535 2113515574000   .94967
    11 2014 0     .    . 22 27 68.8810148574167 2424720079000  1.39575
    11 2015 0     .    . 22 27 68.8810148574167 3654680064000   .93727
    11 2016 0     .    . 22 27 73.5106471459496 4596226754000   .68257
    11 2017 0     .    . 22 27 76.0836852404985 4839251254000   .70283
    11 2018 0     .    . 22 27 76.2589505163875 5340733544000   .74263
    11 2019 0     .    . 22 27 76.2589505163875             .        .
    12 2012 0     .    . 22 27 68.9373634747269 1156814176000    .5859
    12 2013 0     .    . 22 27 66.8674114251592 1245954037000  1.39846
    12 2014 0     .    . 22 27 64.8836867960699 1381036599000   .74008
    12 2015 0     .    . 22 27 64.8836867960699 1566419858000     .806
    12 2016 0     .    . 22 27 63.4519824541415 1775283931000   .79778
    12 2017 0     .    . 22 27 77.1522675466497 1884300190000   .78925
    12 2018 0     .    . 22 27 77.1522675466497 2074388287000   .94017
    12 2019 0     .    . 22 27 81.2228185197292             .        .
    13 2012 0     .    .  1 38 65.0827815315315    9.6062e+10  1.17786
    13 2013 0     .    .  1 38 54.1462128879892    9.5928e+10  1.38657
    13 2014 0     .    .  1 38 60.5439569623816    9.4429e+10    1.452
    13 2015 0     .    .  1 38 62.8988888888888    9.5651e+10  1.21263
    13 2016 0     .    .  1 38 65.3443783068783    9.5748e+10  1.34063
    13 2017 0     .    .  1 38 71.1719052712901    9.7109e+10  1.26075
    13 2018 0     .    .  1 38 67.1157407407407    9.9333e+10   1.2366
    13 2019 0     .    .  1 38 73.3401056481105             .        .
    14 2012 0     .    .  1 38 85.1108333333333    2.5132e+10    3.069
    14 2013 0     .    .  1 38 78.3646676788124    2.4859e+10   3.8815
    14 2014 0     .    .  1 38 82.8485281697575    2.9748e+10    3.774
    14 2015 0     .    .  1 38 87.6689891975308    3.1402e+10   3.3208
    14 2016 0     .    .  1 38 86.8681216931216     3.003e+10  3.06722
    14 2017 0     .    .  1 38  86.059018244488    2.9597e+10  3.37656
    14 2018 0     .    .  1 38 83.2547067901234    2.9766e+10  3.46714
    14 2019 0     .    .  1 38 81.0143968124971             .        .
    15 2012 0     .    .  1 44 54.2064072116671    3.7138e+09  6.51067
    15 2013 0     .    .  1 44 49.3855580693816    3.7732e+09    6.377
    15 2014 0     .    .  1 44 58.0994475138121    3.7732e+09        .
    15 2015 0     .    .  1 44 58.0994475138121    3.7732e+09        .
    15 2016 0     .    .  1 44 58.0994475138121    3.7732e+09        .
    15 2017 0     .    .  1 44 58.0994475138121    3.7732e+09        .
    15 2018 0     .    .  1 44 58.0994475138121    3.7732e+09        .
    15 2019 0     .    .  1 44 58.0994475138121             .        .
    16 2012 1 20242 2014  1 27 90.6414232781168   6.42127e+11    .9764
    16 2013 1 20242 2014  1 27 90.3483331291851   7.02995e+11    .9761
    16 2014 1 20242 2014  1 27 92.4878143233335   7.72092e+11    .9422
    16 2015 1 20242 2014  1 27 92.9414272388059     8.899e+11    .8555
    16 2016 1 20242 2014  1 27 92.1333577951728   9.14869e+11    .7717
    16 2017 1 20242 2014  1 27 93.3905436884669   8.97326e+11   .81556
    16 2018 1 20242 2014  1 27 92.7779656468062   9.43156e+11   .78613
    16 2019 1 20242 2014  1 27  92.398009328669             .        .
    17 2012 1 20909 2016  1 27 80.8471392763731   7.18859e+11    1.037
    17 2013 1 20909 2016  1 27 82.1811560917851   7.53857e+11  1.83822
    17 2014 1 20909 2016  1 27 82.3430878986592   7.91451e+11  1.76333
    17 2015 1 20909 2016  1 27 79.4056063432835   8.73446e+11   1.1044
    17 2016 1 20909 2016  1 27  85.172203196347   9.33001e+11  1.08522
    17 2017 1 20909 2016  1 27 87.0337827443741   9.76318e+11   1.1044
    17 2018 1 20909 2016  1 27 85.2217391304347   9.75165e+11  1.07567
    17 2019 1 20909 2016  1 27 81.5128953380452             .        .
    18 2012 1 20914 2016  1 43 29.4409110915493    2.5026e+09  6.42717
    18 2013 1 20914 2016  1 43 53.1428167420814    2.7238e+09  5.87029
    18 2014 1 20914 2016  1 43 62.3312531391261    3.1425e+09  5.79714
    18 2015 1 20914 2016  1 43 62.3312531391261    3.3212e+09   5.2196
    18 2016 1 20914 2016  1 43 56.2171855105678    3.7834e+09   4.8382
    18 2017 1 20914 2016  1 43 56.2171855105678     3.923e+09  4.90767
    18 2018 1 20914 2016  1 43 55.9665859005422    4.4519e+09        .
    18 2019 1 20914 2016  1 43 55.9665859005422             .        .
    19 2012 1 20073 2013  1 27  89.243820836966    7.6309e+11    .7802
    19 2013 1 20073 2013  1 27 84.8257747427731    8.0987e+11   1.6307
    19 2014 1 20073 2013  1 27 88.2014380131151   8.83301e+11    .7572
    19 2015 1 20073 2013  1 27 86.0320615671641   9.55052e+11      .84
    19 2016 1 20073 2013  1 27 90.2010600130463    7.7671e+11    .8387
    19 2017 1 20073 2013  1 27  91.378210706751   7.88325e+11    .8622
    19 2018 1 20073 2013  1 27 90.4247450348899    8.0651e+11   .83144
    19 2019 1 20073 2013  1 27 88.5423746460103             .        .
    20 2012 1 20885 2016  1 27  89.243820836966    7.6309e+11    .7802
    20 2013 1 20885 2016  1 27 84.8257747427731    8.0987e+11   1.6307
    20 2014 1 20885 2016  1 27 88.2014380131151   8.83301e+11    .7572
    20 2015 1 20885 2016  1 27 86.0320615671641   9.55052e+11      .84
    20 2016 1 20885 2016  1 27 90.2010600130463    7.7671e+11    .8387
    20 2017 1 20885 2016  1 27  91.378210706751   7.88325e+11    .8622
    20 2018 1 20885 2016  1 27 90.4247450348899    8.0651e+11   .83144
    20 2019 1 20885 2016  1 27 88.5423746460103             .        .
    21 2012 1 21426 2017  1 27  89.243820836966    7.6309e+11    .7802
    21 2013 1 21426 2017  1 27 84.8257747427731    8.0987e+11   1.6307
    21 2014 1 21426 2017  1 27 88.2014380131151   8.83301e+11    .7572
    21 2015 1 21426 2017  1 27 86.0320615671641   9.55052e+11      .84
    21 2016 1 21426 2017  1 27 90.2010600130463    7.7671e+11    .8387
    21 2017 1 21426 2017  1 27  91.378210706751   7.88325e+11    .8622
    21 2018 1 21426 2017  1 27 90.4247450348899    8.0651e+11   .83144
    21 2019 1 21426 2017  1 27 88.5423746460103             .        .
    22 2012 1 20933 2016  1 38 62.9688400900901    4.6737e+10  2.02463
    22 2013 1 20933 2016  1 38 64.5759531039136    5.0748e+10   .31689
    22 2014 1 20933 2016  1 38 61.6874274202062    4.7271e+10  1.97967
    22 2015 1 20933 2016  1 38 61.6874274202062     4.500e+10   2.0514
    22 2016 1 20933 2016  1 38 65.0246031746031    4.2176e+10  1.79733
    22 2017 1 20933 2016  1 38 65.0246031746031    4.1583e+10   .54478
    22 2018 1 20933 2016  1 38 64.1606495513163    4.3862e+10  1.75843
    22 2019 1 20933 2016  1 38 68.4523148148148             .        .
    23 2012 1 20608 2015  1 27 82.4275174367916   6.74965e+11   1.0355
    23 2013 1 20608 2015  1 27 85.6581689939572   7.01097e+11   1.0775
    23 2014 1 20608 2015  1 27 84.9926421231992   7.70842e+11   1.0088
    23 2015 1 20608 2015  1 27 85.7501958955224   8.12156e+11  1.04536
    23 2016 1 20608 2015  1 27 86.2237361382909   8.39202e+11    .9855
    23 2017 1 20608 2015  1 27 86.0688620780591   8.51875e+11   1.0105
    23 2018 1 20608 2015  1 27 85.8255233494364   8.79592e+11   .97422
    23 2019 1 20608 2015  1 27 86.4759703481592             .        .
    24 2012 1 21145 2016  1 27 82.4275174367916   6.74965e+11   1.0355
    24 2013 1 21145 2016  1 27 85.6581689939572   7.01097e+11   1.0775
    24 2014 1 21145 2016  1 27 84.9926421231992   7.70842e+11   1.0088
    24 2015 1 21145 2016  1 27 85.7501958955224   8.12156e+11  1.04536
    24 2016 1 21145 2016  1 27 86.2237361382909   8.39202e+11    .9855
    24 2017 1 21145 2016  1 27 86.0688620780591   8.51875e+11   1.0105
    24 2018 1 21145 2016  1 27 85.8255233494364   8.79592e+11   .97422
    24 2019 1 21145 2016  1 27 86.4759703481592             .        .
    25 2012 0     .    . 22 37 46.8552386030391   95794131000 12.36467
    25 2013 0     .    . 22 37 59.8285481083157   99215640000 12.45313
    25 2014 0     .    . 22 37 53.3679208472686  120223776000    9.907
    25 2015 0     .    . 22 37 61.2587468193384  123708187000  9.60882
    25 2016 0     .    . 22 37 60.0940331598226  136881171000  9.37675
    25 2017 0     .    . 22 37 60.0940331598226  132807322000  9.00338
    25 2018 0     .    . 22 37 74.2151160108861  132706355000  8.26614
    25 2019 0     .    . 22 37 74.2151160108861             .        .
    26 2012 0     .    . 22 37 68.4003424876768   97631316000 17.25538
    26 2013 0     .    . 22 37 68.4003424876768  100390225000 17.29271
    26 2014 0     .    . 22 37 66.8963935319767  138352379000   11.175
    26 2015 0     .    . 22 37 69.6903095843935  153539693000 10.56227
    26 2016 0     .    . 22 37 69.6903095843935  156085673000 10.67275
    26 2017 0     .    . 22 37 76.7180692114902  151377405000 10.46186
    26 2018 0     .    . 22 37 79.9576166779399  154522754000  9.50771
    26 2019 0     .    . 22 37 80.8380690991282             .        .
    27 2012 0     .    . 21 45 59.2591359289617   2.09107e+10  6.26667
    27 2013 0     .    . 21 45       71.6796875   2.08997e+10  4.36667
    27 2014 0     .    . 21 45 63.0687169972884    2.2968e+10  1.93333
    27 2015 0     .    . 21 45 43.0974400871459     2.304e+10    4.325
    27 2016 0     .    . 21 45 49.3100761217948    2.8213e+10    5.725
    27 2017 0     .    . 21 45 59.8665582976208    2.9854e+10  4.53333
    27 2018 0     .    . 21 45 59.8665582976208    3.1546e+10      7.1
    27 2019 0     .    . 21 45 59.9508477633477             .        .
    28 2012 0     .    . 21 45 61.3660348360655    1.8583e+10      4.7
    28 2013 0     .    . 21 45 64.7093620665049    2.0449e+10  5.53333
    28 2014 0     .    . 21 45 64.7093620665049     2.319e+10     6.15
    28 2015 0     .    . 21 45 67.0509259259259    2.4678e+10     5.85
    28 2016 0     .    . 21 45 67.0509259259259    2.9033e+10     3.85
    28 2017 0     .    . 21 45 75.4799078525641    3.3414e+10  4.33333
    28 2018 0     .    . 21 45 67.4998196248196    3.9217e+10      4.6
    28 2019 0     .    . 21 45 67.4998196248196             .        .
    29 2012 0     .    . 21 45                .    2.2695e+10    3.525
    29 2013 0     .    . 21 45                .   2.36531e+10      4.9
    29 2014 0     .    . 21 45 70.2362994148708   2.64757e+10  4.63333
    29 2015 0     .    . 21 45 67.6771786492374   2.84153e+10    5.725
    29 2016 0     .    . 21 45 67.6771786492374   3.23226e+10      7.4
    29 2017 0     .    . 21 45  67.356109775641   3.77088e+10      4.6
    29 2018 0     .    . 21 45 68.5662036352889   4.02949e+10      8.7
    29 2019 0     .    . 21 45 64.2382756132756             .        .
    30 2012 0     .    . 21 45 74.3295765027322    3.0711e+10   .03333
    30 2013 0     .    . 21 45 72.7096354166666    3.3709e+10      3.9
    30 2014 0     .    . 21 45 74.6013071895424    3.5431e+10  3.96667
    30 2015 0     .    . 21 45 72.9674836601307    3.6018e+10      4.9
    30 2016 0     .    . 21 45 72.9674836601307    4.1751e+10     4.25
    30 2017 0     .    . 21 45 77.6842548076923    5.0223e+10      4.4
    30 2018 0     .    . 21 45 77.2033951529481    5.9384e+10       12
    30 2019 0     .    . 21 45 72.7929292929292             .        .
    31 2012 0     .    . 21 45                .    1.8978e+10     6.05
    31 2013 0     .    . 21 45                .    2.3843e+10      4.6
    31 2014 0     .    . 21 45                .    3.0041e+10  5.66667
    31 2015 0     .    . 21 45                .    4.0185e+10    5.775
    31 2016 0     .    . 21 45                .    7.3376e+10  2.16667
    31 2017 0     .    . 21 45      29.45078125    9.2623e+10     4.05
    31 2018 0     .    . 21 45 31.8309073444657    1.0626e+11      9.3
    31 2019 0     .    . 21 45 30.1359126984127             .        .
    32 2012 1 21192 2017 22 37 61.4911864710093  330361707000     5.95
    32 2013 1 21192 2017 22 37 63.6958451704545  420341223000  3.75875
    32 2014 1 21192 2017 22 37 66.6200320512821  472999472000      2.1
    32 2015 1 21192 2017 22 37 63.4216793893129  496604290000  2.03767
    32 2016 1 21192 2017 22 37 67.4659304511278  518765122000   2.3475
    32 2017 1 21192 2017 22 37 76.6623754690128  513460158000    3.188
    32 2018 1 21192 2017 22 37 77.1474458204334  516766280000   3.2872
    32 2019 1 21192 2017 22 37 77.1474458204334             .        .
    33 2012 1 21189 2017 22 27                . 1267851991000        .
    33 2013 1 21189 2017 22 27 19.0450224311044 1291268290000        .
    33 2014 1 21189 2017 22 27 25.2912832494608 1326368076000        .
    33 2015 1 21189 2017 22 27 27.9748319471345 1392390567000        .
    33 2016 1 21189 2017 22 27 27.9748319471345 1475891595000        .
    33 2017 1 21189 2017 22 27 27.9748319471345 1498728982000        .
    33 2018 1 21189 2017 22 27 40.9380471727263 1584093971000        .
    33 2019 1 21189 2017 22 27 44.3002544529262             .        .
    34 2012 1 20731 2015 21 45 61.8535156249999    3.4171e+10  4.76667
    34 2013 1 20731 2015 21 45 69.8253532182103    3.6631e+10  5.43333
    34 2014 1 20731 2015 21 45 73.7369281045751    3.8113e+10  5.16667
    34 2015 1 20731 2015 21 45 66.7726034858387    3.8088e+10  5.73333
    34 2016 1 20731 2015 21 45 78.8646033653846    4.2652e+10        5
    34 2017 1 20731 2015 21 45 86.1205482488547    7.8313e+10     3.85
    34 2018 1 20731 2015 21 45 82.5423881673881    8.3712e+10      5.1
    34 2019 1 20731 2015 21 45 81.1087211399711             .        .
    35 2012 1 20866 2016 21 45 74.3295765027322    3.0711e+10   .03333
    35 2013 1 20866 2016 21 45 72.7096354166666    3.3709e+10      3.9
    35 2014 1 20866 2016 21 45 74.6013071895424    3.5431e+10  3.96667
    35 2015 1 20866 2016 21 45 72.9674836601307    3.6018e+10      4.9
    35 2016 1 20866 2016 21 45 77.6842548076923    4.1751e+10     4.25
    35 2017 1 20866 2016 21 45 77.6842548076923    5.0223e+10      4.4
    35 2018 1 20866 2016 21 45 72.7929292929292    5.9384e+10       12
    35 2019 1 20866 2016 21 45 72.7929292929292             .        .
    36 2012 1 21243 2017 21 45 74.3295765027322    3.0711e+10   .03333
    36 2013 1 21243 2017 21 45 72.7096354166666    3.3709e+10      3.9
    36 2014 1 21243 2017 21 45 74.6013071895424    3.5431e+10  3.96667
    36 2015 1 21243 2017 21 45 72.9674836601307    3.6018e+10      4.9
    36 2016 1 21243 2017 21 45 77.6842548076923    4.1751e+10     4.25
    36 2017 1 21243 2017 21 45 77.6842548076923    5.0223e+10      4.4
    36 2018 1 21243 2017 21 45 72.7929292929292    5.9384e+10       12
    36 2019 1 21243 2017 21 45 72.7929292929292             .        .
    37 2012 1 21264 2017 21 45       71.6796875   2.09107e+10  6.26667
    37 2013 1 21264 2017 21 45 63.0687169972884   2.08997e+10  4.36667
    37 2014 1 21264 2017 21 45 59.5702614379085    2.2968e+10  1.93333
    37 2015 1 21264 2017 21 45 43.0974400871459     2.304e+10    4.325
    37 2016 1 21264 2017 21 45 49.3100761217948    2.8213e+10    5.725
    37 2017 1 21264 2017 21 45 59.8665582976208    2.9854e+10  4.53333
    37 2018 1 21264 2017 21 45 59.8665582976208    3.1546e+10      7.1
    37 2019 1 21264 2017 21 45 59.9508477633477             .        .
    38 2012 1 20867 2016 21 27 70.3916330645161  2.359381e+12   .49723
    38 2013 1 20867 2016 21 27 73.7615852073313  2.453456e+12   .58592
    38 2014 1 20867 2016 21 27 68.8428165658787  2.484834e+12     .693
    38 2015 1 20867 2016 21 27 73.7299613045881  2.641246e+12   .63855
    38 2016 1 20867 2016 21 27 75.0277397260274  2.495964e+12   .49815
    38 2017 1 20867 2016 21 27 73.0229969941956  2.620646e+12   .67744
    38 2018 1 20867 2016 21 27 70.5724637681159  2.556908e+12   .82375
    38 2019 1 20867 2016 21 27 71.9122365900383             .        .
    39 2012 1 21368 2017 21 27 70.9345094086021  2.449591e+12   .57636
    39 2013 1 21368 2017 21 27 66.0918692317691  2.383951e+12   .59542
    39 2014 1 21368 2017 21 27 68.1731720725673  2.484721e+12   .58689
    39 2015 1 21368 2017 21 27 62.2513681592039  2.816676e+12   .54409
    39 2016 1 21368 2017 21 27 65.2514313312258  2.522133e+12    .6347
    39 2017 1 21368 2017 21 27 73.5190324419568   2.62758e+12   .60322
    39 2018 1 21368 2017 21 27 69.8513486312399  2.766977e+12   .61433
    39 2019 1 21368 2017 21 27 74.7859366447728             .        .
    40 2012 1 21130 2016 21 27 72.8064180107526  1.857065e+12    .7325
    40 2013 1 21130 2016 21 27 77.3264493695567   1.84686e+12   .79046
    40 2014 1 21130 2016 21 27 76.1074308227964  1.824102e+12   .89767
    40 2015 1 21130 2016 21 27 79.5152294085129  2.121297e+12   .75654
    40 2016 1 21130 2016 21 27 79.6658236740428  2.148855e+12   .84533
    40 2017 1 21130 2016 21 27 83.2784579705638  2.154203e+12    .8593
    40 2018 1 21130 2016 21 27 84.3029388083735  2.212636e+12    .9338
    40 2019 1 21130 2016 21 27 79.7103345648604             .        .
    41 2012 1 21272 2017 21 27 72.8064180107526  1.857065e+12    .7325
    41 2013 1 21272 2017 21 27 77.3264493695567   1.84686e+12   .79046
    41 2014 1 21272 2017 21 27 76.1074308227964  1.824102e+12   .89767
    41 2015 1 21272 2017 21 27 79.5152294085129  2.121297e+12   .75654
    41 2016 1 21272 2017 21 27 79.6658236740428  2.148855e+12   .84533
    41 2017 1 21272 2017 21 27 83.2784579705638  2.154203e+12    .8593
    41 2018 1 21272 2017 21 27 84.3029388083735  2.212636e+12    .9338
    41 2019 1 21272 2017 21 27 79.7103345648604             .        .
    end
    format %tdnn/dd/CCYY issuedate



    Last edited by Paul Green; 15 May 2019, 05:53.

  • #2
    Can you clarify some aspects of the matching you want? You have observations in multiple years for each ID. Consequently the best match for a given ID may (and probably will) differ from one year to the next. Do you want to do that? Or do you want to have a "focal" year for each treated firm (perhaps the year of issue?) and choose the control firm whose data from the year in the matchyear variable is closest to that of the case, and then keep those two IDs matched for all years before and after that?

    Also, it is not really meaningful to speak of a nearest neighbor match on more than one variable. In general, the observation that is nearest on one variable may be not the nearest on another. When you run -teffects psmatch-, your list of matching variables gets crunched up into a single variable, the propensity score that is calculated from the variables you specified (it is the predicted probability of being a treated case from a logistic regression using your matching variables). Is that the sense in which you want a nearest neighbor from these variables? If not, then clarify how you would want Stata to decide which observation is the closest match given all the possibilities.

    Comment


    • #3
      Dear Clyde,

      Thanks a lot for your reply and thinking along. Regarding your first question, I want to match at, as you call it, a 'focal' year. The green bond firm should be matched with a control firm in the year before issuance, and then keep these two IDs matched for all years. Actually, I already have a variable ' match year' , which indicates the year before issuance.

      Regarding your second point, I don't think I fully understand you. I want to match based on the nearest neighbor of all these variables together. With nearest neighbor I mean the control firm with the lowest Mahalanobis distance (from all variables taken together). Is this the same as the calculated propensity score?

      Thus to be clear, I want to match 1 green bond firm with 1 control firm in the same country and industry and from that ' pool' the firm should be matched to the nearest neighbor in the year before issuance (based on the lowest mahalanobis distance). Then this match should stay the same for all periods.


      I hope my question is now somewhat more clear. Again, many thanks for thinking along and I hope you can help me out!


      Comment


      • #4
        Very clear now.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float ID int year byte treated int(issuedate matchyear) byte(countrycode IndustryCode) double(ESGscore totalassets ROA)
         1 2012 0     .    .  1 27 66.2457824760244    4.1758e+10    .5911
         1 2013 0     .    .  1 27 62.9380001633186   4.25283e+10    .7055
         1 2014 0     .    .  1 27 61.4641120634756    4.6905e+10    .7876
         1 2015 0     .    .  1 27 67.4532742537313    4.8018e+10   .83656
         1 2016 0     .    .  1 27 66.8599233529028    5.0853e+10    .7486
         1 2017 0     .    .  1 27 67.9544809247538    5.1658e+10    .7957
         1 2018 0     .    .  1 27  68.622678475577     5.298e+10   .73638
         1 2019 0     .    .  1 27 65.8091548459849             .        .
         2 2012 0     .    .  1 27 67.6176384045335   5.72378e+10   .60256
         2 2013 0     .    .  1 27 76.3366252245631   6.02725e+10    .6244
         2 2014 0     .    .  1 27 75.4096938951136   6.50629e+10    .6644
         2 2015 0     .    .  1 27 73.7283675373134   6.60288e+10   .67444
         2 2016 0     .    .  1 27 75.7162997390737   6.85727e+10   .64889
         2 2017 0     .    .  1 27 76.7183939873417   7.14155e+10   .70089
         2 2018 0     .    .  1 27 76.3741814278046   7.14398e+10   .64675
         2 2019 0     .    .  1 27 73.9373118088116             .        .
         3 2012 0     .    . 21 27 70.9345094086021  2.449591e+12   .57636
         3 2013 0     .    . 21 27 66.0918692317691  2.383951e+12   .59542
         3 2014 0     .    . 21 27 68.1731720725673  2.484721e+12   .58689
         3 2015 0     .    . 21 27 62.2513681592039  2.816676e+12   .54409
         3 2016 0     .    . 21 27 65.2514313312258  2.522133e+12    .6347
         3 2017 0     .    . 21 27 73.5190324419568   2.62758e+12   .60322
         3 2018 0     .    . 21 27 69.8513486312399  2.766977e+12   .61433
         3 2019 0     .    . 21 27 74.7859366447728             .        .
         4 2012 0     .    . 21 27 77.8569220430107   7.01131e+11   .42839
         4 2013 0     .    . 21 27 76.6504127128558   6.68178e+11   .49477
         4 2014 0     .    . 21 27 72.3085486531061   6.30434e+11     .531
         4 2015 0     .    . 21 27 73.9584577114427   6.69342e+11   .56946
         4 2016 0     .    . 21 27 81.4865823674042   6.46868e+11   .51708
         4 2017 0     .    . 21 27 82.8377772595356   6.15659e+11   .51911
         4 2018 0     .    . 21 27 79.4671900161031   5.81612e+11     .574
         4 2019 0     .    . 21 27 79.4671900161031             .        .
         5 2012 0     .    . 21 27 72.8064180107526  1.857065e+12    .7325
         5 2013 0     .    . 21 27 77.3264493695567   1.84686e+12   .79046
         5 2014 0     .    . 21 27 76.1074308227964  1.824102e+12   .89767
         5 2015 0     .    . 21 27 79.5152294085129  2.121297e+12   .75654
         5 2016 0     .    . 21 27 79.6658236740428  2.148855e+12   .84533
         5 2017 0     .    . 21 27 83.2784579705638  2.154203e+12    .8593
         5 2018 0     .    . 21 27 84.3029388083735  2.212636e+12    .9338
         5 2019 0     .    . 21 27 79.7103345648604             .        .
         6 2012 0     .    . 22 27 33.8208280416561 1594613438000    .5575
         6 2013 0     .    . 22 27 41.3013908240445 1613823532000   .55867
         6 2014 0     .    . 22 27 41.3013908240445 1700055541000     .641
         6 2015 0     .    . 22 27 39.1983644859813 1815872967000   .63733
         6 2016 0     .    . 22 27 55.1990507089641 1906257219000     .682
         6 2017 0     .    . 22 27 55.1990507089641 2005151504000    .6235
         6 2018 0     .    . 22 27 59.6559716921119 2.0362584e+12      .59
         6 2019 0     .    . 22 27 64.9517876817647             .        .
         7 2012 0     .    . 22 27 41.3559869685964  333947379000        .
         7 2013 0     .    . 22 27  37.755397510795  521698790000     1.51
         7 2014 0     .    . 22 27 39.5017291998407  647943022000    3.255
         7 2015 0     .    . 22 27 61.4538965477953  919701218000   1.3065
         7 2016 0     .    . 22 27 75.4347426786143  866719175000       .7
         7 2017 0     .    . 22 27 63.5285861959287  892197509000        .
         7 2018 0     .    . 22 27 63.5285861959287 2392524390000        .
         7 2019 0     .    . 22 27 73.4050797283062             .        .
         8 2012 0     .    . 22 27 35.6617442854091 1979259175000     .446
         8 2013 0     .    . 22 27 37.1076209195859 2062013289000   .48667
         8 2014 0     .    . 22 27 35.7937395159357 2164729432000      .57
         8 2015 0     .    . 22 27 35.7937395159357 2259951646000     .639
         8 2016 0     .    . 22 27 38.4938931297709 2350288574000    .6205
         8 2017 0     .    . 22 27 65.2002889100933 2545941962000   .50667
         8 2018 0     .    . 22 27 64.9154649909798 2573419750000     .636
         8 2019 0     .    . 22 27 64.9154649909798             .        .
         9 2012 0     .    . 22 27 34.7293878587757 2087837113000   .50462
         9 2013 0     .    . 22 27 49.7325213972929 2124278888000   .58133
         9 2014 0     .    . 22 27  41.614312245387 2263385352000   .58491
         9 2015 0     .    . 22 27 59.7961091489119 2355708683000   .65433
         9 2016 0     .    . 22 27  65.324368283723 2500095812000    .6944
         9 2017 0     .    . 22 27  65.324368283723 2541156335000    .6376
         9 2018 0     .    . 22 27 69.4354259435962 2634058605000    .7015
         9 2019 0     .    . 22 27 76.1333436247623             .        .
        10 2012 0     .    . 22 27  40.682626575518 2629122262000   .60409
        10 2013 0     .    . 22 27 34.2325120650241 2727365846000  1.79688
        10 2014 0     .    . 22 27 56.4053019410496 2870986686000     .626
        10 2015 0     .    . 22 27 56.7156374615472 1382953378000    .9968
        10 2016 0     .    . 22 27 73.7465238438562 1520230642000    .8734
        10 2017 0     .    . 22 27 71.0063049989397 1576985727000    .8114
        10 2018 0     .    . 22 27 74.3978696741854 1677520316000    .8298
        10 2019 0     .    . 22 27 74.3978696741854             .        .
        11 2012 0     .    . 22 27 69.3539624079248 2025068494000   .88108
        11 2013 0     .    . 22 27 65.5116689888535 2113515574000   .94967
        11 2014 0     .    . 22 27 68.8810148574167 2424720079000  1.39575
        11 2015 0     .    . 22 27 68.8810148574167 3654680064000   .93727
        11 2016 0     .    . 22 27 73.5106471459496 4596226754000   .68257
        11 2017 0     .    . 22 27 76.0836852404985 4839251254000   .70283
        11 2018 0     .    . 22 27 76.2589505163875 5340733544000   .74263
        11 2019 0     .    . 22 27 76.2589505163875             .        .
        12 2012 0     .    . 22 27 68.9373634747269 1156814176000    .5859
        12 2013 0     .    . 22 27 66.8674114251592 1245954037000  1.39846
        12 2014 0     .    . 22 27 64.8836867960699 1381036599000   .74008
        12 2015 0     .    . 22 27 64.8836867960699 1566419858000     .806
        12 2016 0     .    . 22 27 63.4519824541415 1775283931000   .79778
        12 2017 0     .    . 22 27 77.1522675466497 1884300190000   .78925
        12 2018 0     .    . 22 27 77.1522675466497 2074388287000   .94017
        12 2019 0     .    . 22 27 81.2228185197292             .        .
        13 2012 0     .    .  1 38 65.0827815315315    9.6062e+10  1.17786
        13 2013 0     .    .  1 38 54.1462128879892    9.5928e+10  1.38657
        13 2014 0     .    .  1 38 60.5439569623816    9.4429e+10    1.452
        13 2015 0     .    .  1 38 62.8988888888888    9.5651e+10  1.21263
        13 2016 0     .    .  1 38 65.3443783068783    9.5748e+10  1.34063
        13 2017 0     .    .  1 38 71.1719052712901    9.7109e+10  1.26075
        13 2018 0     .    .  1 38 67.1157407407407    9.9333e+10   1.2366
        13 2019 0     .    .  1 38 73.3401056481105             .        .
        14 2012 0     .    .  1 38 85.1108333333333    2.5132e+10    3.069
        14 2013 0     .    .  1 38 78.3646676788124    2.4859e+10   3.8815
        14 2014 0     .    .  1 38 82.8485281697575    2.9748e+10    3.774
        14 2015 0     .    .  1 38 87.6689891975308    3.1402e+10   3.3208
        14 2016 0     .    .  1 38 86.8681216931216     3.003e+10  3.06722
        14 2017 0     .    .  1 38  86.059018244488    2.9597e+10  3.37656
        14 2018 0     .    .  1 38 83.2547067901234    2.9766e+10  3.46714
        14 2019 0     .    .  1 38 81.0143968124971             .        .
        15 2012 0     .    .  1 44 54.2064072116671    3.7138e+09  6.51067
        15 2013 0     .    .  1 44 49.3855580693816    3.7732e+09    6.377
        15 2014 0     .    .  1 44 58.0994475138121    3.7732e+09        .
        15 2015 0     .    .  1 44 58.0994475138121    3.7732e+09        .
        15 2016 0     .    .  1 44 58.0994475138121    3.7732e+09        .
        15 2017 0     .    .  1 44 58.0994475138121    3.7732e+09        .
        15 2018 0     .    .  1 44 58.0994475138121    3.7732e+09        .
        15 2019 0     .    .  1 44 58.0994475138121             .        .
        16 2012 1 20242 2014  1 27 90.6414232781168   6.42127e+11    .9764
        16 2013 1 20242 2014  1 27 90.3483331291851   7.02995e+11    .9761
        16 2014 1 20242 2014  1 27 92.4878143233335   7.72092e+11    .9422
        16 2015 1 20242 2014  1 27 92.9414272388059     8.899e+11    .8555
        16 2016 1 20242 2014  1 27 92.1333577951728   9.14869e+11    .7717
        16 2017 1 20242 2014  1 27 93.3905436884669   8.97326e+11   .81556
        16 2018 1 20242 2014  1 27 92.7779656468062   9.43156e+11   .78613
        16 2019 1 20242 2014  1 27  92.398009328669             .        .
        17 2012 1 20909 2016  1 27 80.8471392763731   7.18859e+11    1.037
        17 2013 1 20909 2016  1 27 82.1811560917851   7.53857e+11  1.83822
        17 2014 1 20909 2016  1 27 82.3430878986592   7.91451e+11  1.76333
        17 2015 1 20909 2016  1 27 79.4056063432835   8.73446e+11   1.1044
        17 2016 1 20909 2016  1 27  85.172203196347   9.33001e+11  1.08522
        17 2017 1 20909 2016  1 27 87.0337827443741   9.76318e+11   1.1044
        17 2018 1 20909 2016  1 27 85.2217391304347   9.75165e+11  1.07567
        17 2019 1 20909 2016  1 27 81.5128953380452             .        .
        18 2012 1 20914 2016  1 43 29.4409110915493    2.5026e+09  6.42717
        18 2013 1 20914 2016  1 43 53.1428167420814    2.7238e+09  5.87029
        18 2014 1 20914 2016  1 43 62.3312531391261    3.1425e+09  5.79714
        18 2015 1 20914 2016  1 43 62.3312531391261    3.3212e+09   5.2196
        18 2016 1 20914 2016  1 43 56.2171855105678    3.7834e+09   4.8382
        18 2017 1 20914 2016  1 43 56.2171855105678     3.923e+09  4.90767
        18 2018 1 20914 2016  1 43 55.9665859005422    4.4519e+09        .
        18 2019 1 20914 2016  1 43 55.9665859005422             .        .
        19 2012 1 20073 2013  1 27  89.243820836966    7.6309e+11    .7802
        19 2013 1 20073 2013  1 27 84.8257747427731    8.0987e+11   1.6307
        19 2014 1 20073 2013  1 27 88.2014380131151   8.83301e+11    .7572
        19 2015 1 20073 2013  1 27 86.0320615671641   9.55052e+11      .84
        19 2016 1 20073 2013  1 27 90.2010600130463    7.7671e+11    .8387
        19 2017 1 20073 2013  1 27  91.378210706751   7.88325e+11    .8622
        19 2018 1 20073 2013  1 27 90.4247450348899    8.0651e+11   .83144
        19 2019 1 20073 2013  1 27 88.5423746460103             .        .
        20 2012 1 20885 2016  1 27  89.243820836966    7.6309e+11    .7802
        20 2013 1 20885 2016  1 27 84.8257747427731    8.0987e+11   1.6307
        20 2014 1 20885 2016  1 27 88.2014380131151   8.83301e+11    .7572
        20 2015 1 20885 2016  1 27 86.0320615671641   9.55052e+11      .84
        20 2016 1 20885 2016  1 27 90.2010600130463    7.7671e+11    .8387
        20 2017 1 20885 2016  1 27  91.378210706751   7.88325e+11    .8622
        20 2018 1 20885 2016  1 27 90.4247450348899    8.0651e+11   .83144
        20 2019 1 20885 2016  1 27 88.5423746460103             .        .
        21 2012 1 21426 2017  1 27  89.243820836966    7.6309e+11    .7802
        21 2013 1 21426 2017  1 27 84.8257747427731    8.0987e+11   1.6307
        21 2014 1 21426 2017  1 27 88.2014380131151   8.83301e+11    .7572
        21 2015 1 21426 2017  1 27 86.0320615671641   9.55052e+11      .84
        21 2016 1 21426 2017  1 27 90.2010600130463    7.7671e+11    .8387
        21 2017 1 21426 2017  1 27  91.378210706751   7.88325e+11    .8622
        21 2018 1 21426 2017  1 27 90.4247450348899    8.0651e+11   .83144
        21 2019 1 21426 2017  1 27 88.5423746460103             .        .
        22 2012 1 20933 2016  1 38 62.9688400900901    4.6737e+10  2.02463
        22 2013 1 20933 2016  1 38 64.5759531039136    5.0748e+10   .31689
        22 2014 1 20933 2016  1 38 61.6874274202062    4.7271e+10  1.97967
        22 2015 1 20933 2016  1 38 61.6874274202062     4.500e+10   2.0514
        22 2016 1 20933 2016  1 38 65.0246031746031    4.2176e+10  1.79733
        22 2017 1 20933 2016  1 38 65.0246031746031    4.1583e+10   .54478
        22 2018 1 20933 2016  1 38 64.1606495513163    4.3862e+10  1.75843
        22 2019 1 20933 2016  1 38 68.4523148148148             .        .
        23 2012 1 20608 2015  1 27 82.4275174367916   6.74965e+11   1.0355
        23 2013 1 20608 2015  1 27 85.6581689939572   7.01097e+11   1.0775
        23 2014 1 20608 2015  1 27 84.9926421231992   7.70842e+11   1.0088
        23 2015 1 20608 2015  1 27 85.7501958955224   8.12156e+11  1.04536
        23 2016 1 20608 2015  1 27 86.2237361382909   8.39202e+11    .9855
        23 2017 1 20608 2015  1 27 86.0688620780591   8.51875e+11   1.0105
        23 2018 1 20608 2015  1 27 85.8255233494364   8.79592e+11   .97422
        23 2019 1 20608 2015  1 27 86.4759703481592             .        .
        24 2012 1 21145 2016  1 27 82.4275174367916   6.74965e+11   1.0355
        24 2013 1 21145 2016  1 27 85.6581689939572   7.01097e+11   1.0775
        24 2014 1 21145 2016  1 27 84.9926421231992   7.70842e+11   1.0088
        24 2015 1 21145 2016  1 27 85.7501958955224   8.12156e+11  1.04536
        24 2016 1 21145 2016  1 27 86.2237361382909   8.39202e+11    .9855
        24 2017 1 21145 2016  1 27 86.0688620780591   8.51875e+11   1.0105
        24 2018 1 21145 2016  1 27 85.8255233494364   8.79592e+11   .97422
        24 2019 1 21145 2016  1 27 86.4759703481592             .        .
        25 2012 0     .    . 22 37 46.8552386030391   95794131000 12.36467
        25 2013 0     .    . 22 37 59.8285481083157   99215640000 12.45313
        25 2014 0     .    . 22 37 53.3679208472686  120223776000    9.907
        25 2015 0     .    . 22 37 61.2587468193384  123708187000  9.60882
        25 2016 0     .    . 22 37 60.0940331598226  136881171000  9.37675
        25 2017 0     .    . 22 37 60.0940331598226  132807322000  9.00338
        25 2018 0     .    . 22 37 74.2151160108861  132706355000  8.26614
        25 2019 0     .    . 22 37 74.2151160108861             .        .
        26 2012 0     .    . 22 37 68.4003424876768   97631316000 17.25538
        26 2013 0     .    . 22 37 68.4003424876768  100390225000 17.29271
        26 2014 0     .    . 22 37 66.8963935319767  138352379000   11.175
        26 2015 0     .    . 22 37 69.6903095843935  153539693000 10.56227
        26 2016 0     .    . 22 37 69.6903095843935  156085673000 10.67275
        26 2017 0     .    . 22 37 76.7180692114902  151377405000 10.46186
        26 2018 0     .    . 22 37 79.9576166779399  154522754000  9.50771
        26 2019 0     .    . 22 37 80.8380690991282             .        .
        27 2012 0     .    . 21 45 59.2591359289617   2.09107e+10  6.26667
        27 2013 0     .    . 21 45       71.6796875   2.08997e+10  4.36667
        27 2014 0     .    . 21 45 63.0687169972884    2.2968e+10  1.93333
        27 2015 0     .    . 21 45 43.0974400871459     2.304e+10    4.325
        27 2016 0     .    . 21 45 49.3100761217948    2.8213e+10    5.725
        27 2017 0     .    . 21 45 59.8665582976208    2.9854e+10  4.53333
        27 2018 0     .    . 21 45 59.8665582976208    3.1546e+10      7.1
        27 2019 0     .    . 21 45 59.9508477633477             .        .
        28 2012 0     .    . 21 45 61.3660348360655    1.8583e+10      4.7
        28 2013 0     .    . 21 45 64.7093620665049    2.0449e+10  5.53333
        28 2014 0     .    . 21 45 64.7093620665049     2.319e+10     6.15
        28 2015 0     .    . 21 45 67.0509259259259    2.4678e+10     5.85
        28 2016 0     .    . 21 45 67.0509259259259    2.9033e+10     3.85
        28 2017 0     .    . 21 45 75.4799078525641    3.3414e+10  4.33333
        28 2018 0     .    . 21 45 67.4998196248196    3.9217e+10      4.6
        28 2019 0     .    . 21 45 67.4998196248196             .        .
        29 2012 0     .    . 21 45                .    2.2695e+10    3.525
        29 2013 0     .    . 21 45                .   2.36531e+10      4.9
        29 2014 0     .    . 21 45 70.2362994148708   2.64757e+10  4.63333
        29 2015 0     .    . 21 45 67.6771786492374   2.84153e+10    5.725
        29 2016 0     .    . 21 45 67.6771786492374   3.23226e+10      7.4
        29 2017 0     .    . 21 45  67.356109775641   3.77088e+10      4.6
        29 2018 0     .    . 21 45 68.5662036352889   4.02949e+10      8.7
        29 2019 0     .    . 21 45 64.2382756132756             .        .
        30 2012 0     .    . 21 45 74.3295765027322    3.0711e+10   .03333
        30 2013 0     .    . 21 45 72.7096354166666    3.3709e+10      3.9
        30 2014 0     .    . 21 45 74.6013071895424    3.5431e+10  3.96667
        30 2015 0     .    . 21 45 72.9674836601307    3.6018e+10      4.9
        30 2016 0     .    . 21 45 72.9674836601307    4.1751e+10     4.25
        30 2017 0     .    . 21 45 77.6842548076923    5.0223e+10      4.4
        30 2018 0     .    . 21 45 77.2033951529481    5.9384e+10       12
        30 2019 0     .    . 21 45 72.7929292929292             .        .
        31 2012 0     .    . 21 45                .    1.8978e+10     6.05
        31 2013 0     .    . 21 45                .    2.3843e+10      4.6
        31 2014 0     .    . 21 45                .    3.0041e+10  5.66667
        31 2015 0     .    . 21 45                .    4.0185e+10    5.775
        31 2016 0     .    . 21 45                .    7.3376e+10  2.16667
        31 2017 0     .    . 21 45      29.45078125    9.2623e+10     4.05
        31 2018 0     .    . 21 45 31.8309073444657    1.0626e+11      9.3
        31 2019 0     .    . 21 45 30.1359126984127             .        .
        32 2012 1 21192 2017 22 37 61.4911864710093  330361707000     5.95
        32 2013 1 21192 2017 22 37 63.6958451704545  420341223000  3.75875
        32 2014 1 21192 2017 22 37 66.6200320512821  472999472000      2.1
        32 2015 1 21192 2017 22 37 63.4216793893129  496604290000  2.03767
        32 2016 1 21192 2017 22 37 67.4659304511278  518765122000   2.3475
        32 2017 1 21192 2017 22 37 76.6623754690128  513460158000    3.188
        32 2018 1 21192 2017 22 37 77.1474458204334  516766280000   3.2872
        32 2019 1 21192 2017 22 37 77.1474458204334             .        .
        33 2012 1 21189 2017 22 27                . 1267851991000        .
        33 2013 1 21189 2017 22 27 19.0450224311044 1291268290000        .
        33 2014 1 21189 2017 22 27 25.2912832494608 1326368076000        .
        33 2015 1 21189 2017 22 27 27.9748319471345 1392390567000        .
        33 2016 1 21189 2017 22 27 27.9748319471345 1475891595000        .
        33 2017 1 21189 2017 22 27 27.9748319471345 1498728982000        .
        33 2018 1 21189 2017 22 27 40.9380471727263 1584093971000        .
        33 2019 1 21189 2017 22 27 44.3002544529262             .        .
        34 2012 1 20731 2015 21 45 61.8535156249999    3.4171e+10  4.76667
        34 2013 1 20731 2015 21 45 69.8253532182103    3.6631e+10  5.43333
        34 2014 1 20731 2015 21 45 73.7369281045751    3.8113e+10  5.16667
        34 2015 1 20731 2015 21 45 66.7726034858387    3.8088e+10  5.73333
        34 2016 1 20731 2015 21 45 78.8646033653846    4.2652e+10        5
        34 2017 1 20731 2015 21 45 86.1205482488547    7.8313e+10     3.85
        34 2018 1 20731 2015 21 45 82.5423881673881    8.3712e+10      5.1
        34 2019 1 20731 2015 21 45 81.1087211399711             .        .
        35 2012 1 20866 2016 21 45 74.3295765027322    3.0711e+10   .03333
        35 2013 1 20866 2016 21 45 72.7096354166666    3.3709e+10      3.9
        35 2014 1 20866 2016 21 45 74.6013071895424    3.5431e+10  3.96667
        35 2015 1 20866 2016 21 45 72.9674836601307    3.6018e+10      4.9
        35 2016 1 20866 2016 21 45 77.6842548076923    4.1751e+10     4.25
        35 2017 1 20866 2016 21 45 77.6842548076923    5.0223e+10      4.4
        35 2018 1 20866 2016 21 45 72.7929292929292    5.9384e+10       12
        35 2019 1 20866 2016 21 45 72.7929292929292             .        .
        36 2012 1 21243 2017 21 45 74.3295765027322    3.0711e+10   .03333
        36 2013 1 21243 2017 21 45 72.7096354166666    3.3709e+10      3.9
        36 2014 1 21243 2017 21 45 74.6013071895424    3.5431e+10  3.96667
        36 2015 1 21243 2017 21 45 72.9674836601307    3.6018e+10      4.9
        36 2016 1 21243 2017 21 45 77.6842548076923    4.1751e+10     4.25
        36 2017 1 21243 2017 21 45 77.6842548076923    5.0223e+10      4.4
        36 2018 1 21243 2017 21 45 72.7929292929292    5.9384e+10       12
        36 2019 1 21243 2017 21 45 72.7929292929292             .        .
        37 2012 1 21264 2017 21 45       71.6796875   2.09107e+10  6.26667
        37 2013 1 21264 2017 21 45 63.0687169972884   2.08997e+10  4.36667
        37 2014 1 21264 2017 21 45 59.5702614379085    2.2968e+10  1.93333
        37 2015 1 21264 2017 21 45 43.0974400871459     2.304e+10    4.325
        37 2016 1 21264 2017 21 45 49.3100761217948    2.8213e+10    5.725
        37 2017 1 21264 2017 21 45 59.8665582976208    2.9854e+10  4.53333
        37 2018 1 21264 2017 21 45 59.8665582976208    3.1546e+10      7.1
        37 2019 1 21264 2017 21 45 59.9508477633477             .        .
        38 2012 1 20867 2016 21 27 70.3916330645161  2.359381e+12   .49723
        38 2013 1 20867 2016 21 27 73.7615852073313  2.453456e+12   .58592
        38 2014 1 20867 2016 21 27 68.8428165658787  2.484834e+12     .693
        38 2015 1 20867 2016 21 27 73.7299613045881  2.641246e+12   .63855
        38 2016 1 20867 2016 21 27 75.0277397260274  2.495964e+12   .49815
        38 2017 1 20867 2016 21 27 73.0229969941956  2.620646e+12   .67744
        38 2018 1 20867 2016 21 27 70.5724637681159  2.556908e+12   .82375
        38 2019 1 20867 2016 21 27 71.9122365900383             .        .
        39 2012 1 21368 2017 21 27 70.9345094086021  2.449591e+12   .57636
        39 2013 1 21368 2017 21 27 66.0918692317691  2.383951e+12   .59542
        39 2014 1 21368 2017 21 27 68.1731720725673  2.484721e+12   .58689
        39 2015 1 21368 2017 21 27 62.2513681592039  2.816676e+12   .54409
        39 2016 1 21368 2017 21 27 65.2514313312258  2.522133e+12    .6347
        39 2017 1 21368 2017 21 27 73.5190324419568   2.62758e+12   .60322
        39 2018 1 21368 2017 21 27 69.8513486312399  2.766977e+12   .61433
        39 2019 1 21368 2017 21 27 74.7859366447728             .        .
        40 2012 1 21130 2016 21 27 72.8064180107526  1.857065e+12    .7325
        40 2013 1 21130 2016 21 27 77.3264493695567   1.84686e+12   .79046
        40 2014 1 21130 2016 21 27 76.1074308227964  1.824102e+12   .89767
        40 2015 1 21130 2016 21 27 79.5152294085129  2.121297e+12   .75654
        40 2016 1 21130 2016 21 27 79.6658236740428  2.148855e+12   .84533
        40 2017 1 21130 2016 21 27 83.2784579705638  2.154203e+12    .8593
        40 2018 1 21130 2016 21 27 84.3029388083735  2.212636e+12    .9338
        40 2019 1 21130 2016 21 27 79.7103345648604             .        .
        41 2012 1 21272 2017 21 27 72.8064180107526  1.857065e+12    .7325
        41 2013 1 21272 2017 21 27 77.3264493695567   1.84686e+12   .79046
        41 2014 1 21272 2017 21 27 76.1074308227964  1.824102e+12   .89767
        41 2015 1 21272 2017 21 27 79.5152294085129  2.121297e+12   .75654
        41 2016 1 21272 2017 21 27 79.6658236740428  2.148855e+12   .84533
        41 2017 1 21272 2017 21 27 83.2784579705638  2.154203e+12    .8593
        41 2018 1 21272 2017 21 27 84.3029388083735  2.212636e+12    .9338
        41 2019 1 21272 2017 21 27 79.7103345648604             .        .
        end
        format %tdnn/dd/CCYY issuedate
        
        //    FIRST CALCULATE INVERSE COVARIANCE MATRIX TO USE IN COMPUTING
        //    MAHALANOBIS DISTANCE
        corr ESGscore totalassets ROA, cov
        matrix V = r(C)
        matrix V = syminv(V)
        
        //    SEPARATE CONTROLS INTO A DIFFERENT FILE
        preserve
        assert inlist(treated, 0, 1)
        keep if !treated
        assert missing(matchyear)
        replace matchyear = year
        assert missing(issuedate)
        drop issuedate year
        ds matchyear IndustryCode countrycode, not
        rename (`r(varlist)') ctrl_=
        tempfile controls
        save `controls'
        
        //    NOW FOR THE CASES, USE ONLY THE OBSERVATIONS FROM
        //    THE ISSUE YEAR, WHICH WE WILL MATCH TO THE CONTROLS IN 
        //    MATCH YEAR
        restore
        keep if treated & year == matchyear + 1
        isid ID
        
        //    PAIR UP ALL EXACT MATCHES ON MATCHYEAR, INDUSTRY CODE
        //    RETAIN UNMATCHED TREATED CASES, BUT NOT UNMATCHED CONTROLS
        joinby matchyear IndustryCode countrycode using `controls', unmatched(master)
        drop _merge
        
        //    NOW CALCULATE MAHALANOBIS DISTANCE BETWEEN EACH CASE AND ALL
        //    CANDIDATE MATCHED CONTROLS
        quietly gen mahalanobis = .
        forvalues i = 1/`=_N' {
            matrix X = (ESGscore[`i'], totalassets[`i'], ROA[`i'])
            matrix Y = (ctrl_ESGscore[`i'], ctrl_totalassets[`i'], ctrl_ROA[`i'])
            matrix DIFF = Y - X
            matrix M = DIFF*V*DIFF'
            quietly replace mahalanobis = M[1, 1] in `i'
        }
        
        //    NOW FROM ALL CANDIDATE PAIRS, PICK THE ONE WITH THE
        //    SMALLEST VALUE OF MAHALANOBIS DISTANCE
        by ID (mahalanobis), sort: keep if _n == 1
        Your response about finding the closest match on Mahalanobis distance is precisely the kind of thing I was looking for. It isn't really correct to say that this constitutes a closest match on all of the variables. It constitutes a closest match on a formula calculated from the variables, but you will find that it is sometimes the best match on any given variable and sometimes not. I would not have guessed from your original code attempts that you were interested in the Mahalanobis distance, as that is not what your -teffects psmatch- code would have done. Anyway, the above should do it.

        One small note: it is possible that there will be cases that have two or more controls that are tied for smallest Mahalanobis distance. The code above breaks such ties randomly and irreproducibly. If you prefer to have a more systematic way of handling that situation, post back with what criteria you would use to break such ties.

        Comment


        • #5
          Dear Clyde,

          I cannot express how thankful I am for your help. This problem cost me so many days. Your code works perfectly and is exactly where I was looking for. You are really one of a kind.

          Originally posted by Clyde Schechter View Post
          I would not have guessed from your original code attempts that you were interested in the Mahalanobis distance, as that is not what your -teffects psmatch- code would have done. Anyway, the above should do it.
          I see the confusion here, -teffects psmatch- does indeed not match on Mahalanobis distance. However, -teffects psmatch- does. Do you think is possible to achieve the same as your code with teffects psmatch?


          Again, many thanks for your help. The world needs more people like you.


          All the best.

          Comment


          • #6
            Do you think is possible to achieve the same as your code with teffects psmatch?
            I don't know. I don't use -teffects psmatch-. Also the help file for -teffects psmatch- does not mention Mahalanobis distance at all. Did you mean to ask about using -psmatch2-, which does allow that?

            But as far as I know, neither psmatch2 nor teffects psmatch supports exact matching on selected variables. So in order to do that you would have to first break up your data set into subsets defined by country, industry code, and match year and then do a separate -psmatch2- in each subset.

            Comment


            • #7
              Sorry, where I said teffects psmatch, I meant teffects nnmatch. According to help teffects nnmatch, it does match on mahalanobis distance.

              So in order to do that you would have to first break up your data set into subsets defined by country, industry code, and match year and then do a separate -psmatch2- in each subset.
              I like this solution, i''l give it a try!

              Comment

              Working...
              X