Announcement

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

  • Nearest matching (without replacements)

    Hi everyone,

    I have the following dataset for which I need to do a 1-by-1 match of companies (wo replacements) from group 1 to companies in the same province from group 2 with the closest assets in the last year from the year in variable 'rendate' (ignoring the differences caused by sequential order of the matching). To give a little more background the dataset spans from 1990-2011 but I have only included variables till 2000 in the example and id is the unique ID for each company. Also, in the variable 'changedname' 1 denotes "group 1" and 0 denotes "group 2".

    I am not sure how I can go about the process except for doing a joinby to get the pairwise combinations (within each province) but I am stuck at incorporating in the code the part to capture the closest assets based on the previous year (from the year stored in 'rendate') and then generating the nearest possible match wo replacement. It would be helpful if I could get some ideas.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 id byte(sector province) float(changed_name rendate) double(total_ass1990 total_ass1991 total_ass1992 total_ass1993 total_ass1994 total_ass1995 total_ass1996 total_ass1997 total_ass1998 total_ass1999 total_ass2000)
    "000002.SZ"  . 12 0     .            .             .  962522585.86 2136158632.31 2675187672.11 3234377489.99  3466124320.3    3959626453 4037977498.58 4494664465.95  5622247214.95
    "000004.SZ"  2 12 1 11746            .  134078566.87  322441154.47  771893618.71  891480074.92 1312675694.74  658472517.27  605124074.32  396901490.29  333497678.96   508206707.12
    "000006.SZ"  8 12 1 13993 825064663.82  120831458.77  228801476.95  555797715.19  502427319.36  692835128.21  780039087.76 2899549948.02 3385797768.44 3495369520.24  3787811194.43
    "000007.SZ"  8 12 1 18284     49076700   75484321.61  132252786.37  313539362.25  456664437.21   520814708.8  601070749.67  791761386.23 1099964374.08 1105732610.47   951889239.55
    "000008.SZ" 16  4 1 14465            .   70185278.95  108267245.06  240876015.74  256263877.64  247472698.64  241015756.63  363332390.73     326598053     236162688      290828409
    "000009.SZ" 16 12 1 12260            .   664292848.4 1546114462.15 3270337312.05    5244665379 6177252917.09 5494766397.41 5644268199.17    4864005898 4893258228.76   4664228632.5
    "000010.SZ" 16 12 1 13663            .             .   59457027.13   109313400.8  126127274.75  218339016.89  283710864.82  339372135.87     362268094  358177155.91    351781927.8
    "000011.SZ"  . 12 0     .            .             . 2289104769.92 3347536701.89  3799416746.2 3993477885.46 3988668899.76 3019536302.89 2731125719.71 2373639836.36  2667288668.25
    "000012.SZ" 10 12 1 12120            .  392429165.33  415984977.77 1093255496.83 1347382872.93 2264709853.41 2850218871.91 3196455654.45 3297791623.19  2865585033.3  2824842003.12
    "000014.SZ"  8 12 1 13020            .   61276714.95  208795322.35  499836155.46  636494180.21  893472484.86  584352530.99  535892242.49  552824549.56  336627359.54   310419895.17
    "000016.SZ" 14 12 1 13041            .   420241089.2  549806513.77 1123181822.15 2025919725.27 2524104875.05 4155779459.95 5385980767.89 7183323011.84 9769410621.91 10063013806.96
    "000017.SZ"  . 12 0     .            .  938615103.79 1643321373.73 2181803372.69 2939942184.66 2916859618.29 2939003941.67 3367232881.84 2717934327.18 2721443364.56     2459306434
    "000019.SZ"  . 12 0     .            . 2036469395.68   328278638.9  424506473.41  434511111.22  437016186.11  426224303.59  433627838.65  455455219.17  470146337.74   426110873.62
    "000020.SZ" 14 12 1 17435            .     425616856     456537239     481716278   441051272.1     526263441     570664941     531579651     446069467  485131946.82   466966587.56
    "000021.SZ"  9 12 1 16748            .             .  518167002.95  818022921.97  861815513.51 1112224158.41 1507024018.26 1767711771.94 3574516741.87 3822434472.45  3874861637.62
    "000022.SZ"  . 12 0     .            .             .             .  889414935.53 1010181201.26 1214102128.51 1468098565.74 1560673252.54 1683972239.31 1747586132.03  2252338533.67
    "000023.SZ" 10 12 1 14515            .             .             .  296988898.47  378689937.56  511187288.54  494044142.95  580612864.24  690334891.43  737676159.73   828554545.17
    "000025.SZ" 19 12 1 12599            .             .  585442204.11  915086175.05 1173009798.11 1260713411.13 1064864004.16  971103093.22  958399452.33  876150224.37   766478943.77
    "000026.SZ" 14 12 1 18691            .             .  167896424.85  317504563.69  384116365.07  455158019.09  590274586.78  806342683.96   837169638.4  821940796.79   781982535.21
    "000027.SZ"  1 12 1 17631            .             . 3091183045.41 4846041683.52  5304487713.7 5310914809.19 5366011360.16 5516897083.49 5296252358.66 5051382472.56  7544313536.46
    "000028.SZ"  3 12 1 15144            .             .  188925591.15  384073645.39  379764584.36  380207166.18  446603126.84  495822042.48  562359580.42  538950594.14   490886574.51
    "000029.SZ"  . 12 0     .            .             . 2648041577.95 4190819734.68 5130202217.88 6431914427.68 6329626384.43 6096803932.97    4877477811 4262149889.01   4113913509.6
    "000030.SZ" 11  5 1 15539            .             .  839790699.85 1352012155.12  1678102781.6 1891154680.07 2052241713.06 2107907866.88 1538471145.19 1620683362.71  1438282665.31
    "000031.SZ"  8 12 1 16916            .             .             . 1250447023.61 1275329812.04 1483092350.93 1776390779.44 1923629260.86 1885974586.54 1661181216.74  1406701409.18
    "000032.SZ"  . 12 0     .            .             .  316294379.16  453356203.51  508276366.49  572431906.08  587866889.12  681038065.85  746124183.68  731148198.57   845296102.08
    "000036.SZ"  8 12 1 14703            .             .   206416447.7   211262010.3  270647112.77  307990719.77   300616724.3  253879064.09 1459904677.86 2227263104.32  2629279494.88
    "000037.SZ"  . 12 0     .            .             .             . 1025858138.36 1076285580.98  957802300.13  1014608930.3 1016387254.78  992783165.94  1004968086.6  1341408120.93
    "000038.SZ"  . 12 0     .            .             .             .   201591739.3  253621946.46  283190594.41  367046475.47  301875801.72  264048515.73  183842392.31   184439703.86
    "000039.SZ" 16 12 1 13148            .             .             .  576121331.31 1241516287.86 1925853402.45 3026051702.33 3868816458.38 4307371678.78 6622510638.31  6639791527.07
    "000040.SZ"  1 12 1 18767            .             .  444270921.19  573421400.75  880299196.79 1083633366.77 1087647399.32 1304922213.18 1477302508.38    2191060128     2369836206
    "000042.SZ"  8 12 1 13821            .             .             .  578016619.76  840248244.21   980256115.6  1041780739.1 2982258867.88 3159010441.11 3031004493.58  3448564853.51
    "000043.SZ"  8 12 1 17481            .  131571772.63  165106812.45  349358914.47  660580637.67  779304281.72  955246965.39 1088961596.98 1194628958.29 1158171502.01  1241916738.81
    "000045.SZ"  . 12 0     .            .  335291322.32  435328300.82  520456062.28  693043051.35  708329206.35  726587278.82  747224135.37  567924503.77  626262913.32   696570046.89
    "000046.SZ"  8  4 1 14453     65234000     187868000     243894000  370839007.97  522103111.26   625973600.9  656734855.63  755240169.05  929189237.29  869563688.02  1304624597.81
    "000048.SZ" 20 12 1 13597            .    1467075800     271758200  646238620.77  792692955.96  964076640.52 1022445592.43 1353838568.69    1414035843    1515703960     1865610798
    "000050.SZ"  9 12 1 17665            .      97847700     220871400  296477151.37  300166955.25  368702710.14  387884868.22  416539277.11  471759123.68  521682494.42   606933626.94
    "000055.SZ" 16 12 1 13756            .             .             .      72999000     163188000  254284260.06  460911323.23  857603222.57     955773713    1120541505     1280868467
    "000056.SZ"  8 12 1 20349            .             .             .     420772644     430344361     558855125  771609327.86  898437911.41 1064098976.14 1233332764.08  1108846910.43
    "000058.SZ"  . 12 0     .            .             .             .    1479133872    1480494612 1525771486.14 2165745745.64 2401721029.38 2977926420.43  3922573943.1  4108583390.77
    "000059.SZ" 15 27 1 15637            .             .             .  189407458.64  197434236.45  198894428.17  579709944.08 2790152448.22  2926599041.6 2835153539.56  2549493886.28
    "000060.SZ" 10 12 1 14382            .             .             .  622556357.53  749957450.85   609870735.1  862446925.86  958347472.48 1204319870.01    4263193000  4579011658.95
    "000061.SZ"  . 12 0     .            .             .             .   90141449.39  138487198.12  242463440.22  334616414.92  867345867.51 1233846003.29 1400736279.48  2246596886.76
    "000062.SZ"  . 12 0     .            .             .             .             .     508029159     781520965  709170179.09  960798858.89 1326196284.24 1378133511.52  1264706635.81
    "000063.SZ"  . 12 0     .            .             .             .             .  145482820.95   236421217.2  382698474.31 1357432249.82 2205555166.17 3384815709.26  6321006764.04
    "000065.SZ" 16  4 1 15055            .             .             .             .             .   63912242.31   68590975.28  117534194.73  169957699.75  238449837.56   306125469.81
    "000069.SZ" 12 12 1 14629            .             .             .             .   216020880.9  301130603.88  368614775.89  802698781.68 1024003743.59 1122580103.33  1302839207.77
    "000070.SZ"  . 12 0     .            .             .             .             .             .             .             .     611626748     650912709     732345466     1369357327
    "000078.SZ"  . 12 0     .            .             .             .             .             .   82527632.09  123001412.82  145625794.95  229965314.43  642281207.12   872297171.28
    "000088.SZ"  . 12 0     .            .             .             .             .  748131503.61  990334334.36 1488798039.52 1895080974.56 1842323340.18 1964393078.76  3404245140.94
    "000089.SZ"  . 12 0     .            .             .             .             .             .             .  416601432.83  649429599.18 1157282976.05  1676679534.4  2623583287.45
    "000090.SZ"  . 12 0     .            .             .             .             .             .             . 1300174287.46 1347694904.29 1464155900.66 2514221261.86   3007678477.9
    "000096.SZ"  . 12 0     .            .             .             .             .             .             .             .   661121395.9   666675205.3   806058987.1  1295618585.72
    "000099.SZ"  . 12 0     .            .             .             .             .             .             .             .  221606782.26   373346557.1  445292874.15  1094266393.25
    "000100.SZ"  . 12 0     .            .             .             .             .             .             .             .             .             .             .     9265920000
    "000151.SZ"  .  4 0     .            .             .             .             .             .             .             .  462834685.06  484745488.51  389408488.18   989095476.22
    "000153.SZ"  2  9 1 15745            .             .             .             .             .             .             .   73000154.94  110326201.36  134632021.05   634786180.28
    "000156.SZ"  7 19 1 16275            .             .             .             .             .             .             .   204670146.3  269627789.07  270045485.22   641256378.03
    "000157.SZ" 16 22 1 18926            .             .             .             .             .             .             .  130500231.25  230741849.04  300457226.24   950439785.37
    "000158.SZ" 14 17 1 21113            .             .             .             .             .             .             . 1615281744.18 1678360962.13  1330578082.4  1885159536.88
    "000159.SZ"  . 14 0     .            .             .             .             .             .             .             .  276350297.97  409373341.74  650308122.98   893303773.92
    "000166.SZ"  . 14 0     .            .             .             .             .             .             .             .             .             .             .              .
    "000301.SZ" 10 15 1 17736            .             .             .             .             .  667410937.48  673540316.73  813965822.63 1224630748.04 1242302930.82  1788129796.29
    "000333.SZ"  . 12 0     .            .             .             .             .             .             .             .             .             .             .              .
    "000338.SZ"  . 10 0     .            .             .             .             .             .             .             .             .             .             .              .
    "000400.SZ"  . 18 0     .            .             .             .             .     295376024     373070455     432759980     843244387    1013083681 1821797524.08  2078863979.41
    "000401.SZ"  . 17 0     .            .             .             .     594639000    1022266000 1241887389.36 1895597253.71 2281666117.39 2129918084.56 2041973225.09  2238243947.94
    "000402.SZ"  8  4 1 14822            .             .             .      85060000     125019000  164157966.33   302540589.7  342729136.01  330614604.24  317480080.07   599748761.33
    "000403.SZ"  2 11 1 14700            .             .             .  190953094.94  191545627.53  199427641.21  330759321.68     397327686  523908003.43  960746690.97  1178575293.78
    "000404.SZ" 16 16 1 21375            .             .             .     217699481     244286999     350869502     547938316  670597596.31  667597319.42  577692813.07   699795935.52
    "000407.SZ"  . 10 0     .            .             .             .      34727350     117059400  166016731.54   306291563.4  548247821.85  766309711.87  831467178.61  1034388362.68
    "000408.SZ" 10 30 1 16693            .             .             .     137860320     139585280  162251803.61  269899320.14  386652854.84  723554820.36  740949165.03   732385463.21
    "000409.SZ" 16 10 1 14152            .             .             .     177206861     189722313   208103155.8  313250007.91  395281797.28  315621164.28  263189611.28   321769721.93
    "000410.SZ"  . 27 0     .            .             .             .  923229071.69 1005109450.28 1278263093.66 1601581747.04 1900568647.68  2861636150.1 3345798900.66  2864944013.91
    "000411.SZ"  3 19 1 15544            .             .             .     240069100     288353600     383419300  488644143.53  547136238.28  525070914.52  430359007.77   292303970.72
    "000413.SZ"  9 17 1 19726            .             .             .             .     853258000  973267383.87  3998124379.2    3458074596    2204641282    2045540187     1616898335
    "000417.SZ" 19  9 1 15523            .             .             .     141291718     119492584     146490516  258589753.52  414096604.95  656912186.63  751736317.66   974241901.62
    "000418.SZ"  . 15 0     .            .             .             .  443037417.07  589783597.58  978591175.81  1439634990.8 2529218748.43    2809954907 3184002508.34  3407149351.94
    "000419.SZ" 19 22 1 14073            .             .             .   67093062.67  111908264.34  164064602.87   282307452.1  336665380.35  890980862.09  896084788.07  1109852621.31
    "000420.SZ"  .  5 0     .            .             .             .     576318750     771605910     742209140 1061631452.91 1036581057.84 1174711265.04 1431184234.47  2123121460.87
    "000421.SZ"  1 15 1 20622            .             .             .     157795842     184123445   189991685.9  300202345.57  414011438.54  581981810.12   695974324.3   824670102.12
    "000422.SZ"  . 21 0     .            .             .             .     153063130     158624410     161630460   293115568.7  378918707.66  647795873.77  847255781.06  1031462675.44
    "000423.SZ"  2 10 1 20124            .             .             .  155283879.23  154962942.21  214450242.72  314143768.28  342938890.45  462739567.79   535212776.8   871023250.11
    "000425.SZ" 16 15 1 14769            .             .             .     701375850     971263497 1062106207.26 1171010756.74 1266057667.21 1258964484.62  1530003027.5  1510767778.25
    "000426.SZ" 10  3 1 16105            .             .             .      80177750     115996000     133827730  240372422.87  300902528.93  790611835.23  984186985.51  1067870879.29
    "000428.SZ" 12 22 1 18445            .             .             .     110696343     136126838     197944109  476655150.12  500280141.14  641308615.82  627148920.04   704438168.17
    "000429.SZ"  . 12 0     .            .             .             .             .    1166869292    1204720998    1813903588  1910994516.7 2585806036.06 3056506789.09  3791087549.15
    "000430.SZ" 12 22 1 18786            .             .             .   66123605.34  123239341.71   145761059.1  223403309.12  260056902.53     300506351   325334406.8   389930086.93
    "000488.SZ"  . 10 0     .            .             .             .             .             .  232848833.61             .    1812990508 2490568401.35 3316785771.14  5384263585.32
    "000498.SZ" 16 10 1 19344            .             .             .             .  502157700.32  596376964.55  603219472.47  901966678.37 1076618098.01  1208337267.4  1532828641.75
    "000501.SZ" 19 21 1 12542 119604436.81  164756591.71  456479300.11 1079189353.76  1277615595.8 1548751156.87 2111705383.47 2405126869.47 2880869933.14 2980031212.93  3248718676.47
    "000502.SZ"  8 12 1 15713            .  141725747.81  477448174.04  910176895.41  884231420.18  700901437.57  787852525.28  619601427.45  330114913.83   420118911.5   428848445.99
    "000503.SZ" 17 20 1 12696            .             .  142258481.52  452658413.06  669844447.26  588116034.19   790787420.2  615020750.65  730777917.99  764296525.23    967103642.4
    "000504.SZ"  2 22 1 14456            .  223405101.36     455240845     782336932  831098636.09  861025048.74  766943131.46  743862481.55  631047539.07  479487219.54    597048948.6
    "000505.SZ" 20 20 1 14668  56665014.63  143209563.95  234478484.09   729302923.6 1112735908.08 1306763065.23 1298880546.47 1139606084.35   859751225.2  899242971.78    804014293.6
    "000506.SZ"  8 10 1 13979            .             .  498814641.48   680717639.7  840076494.15  845949773.73  830685989.32  894715100.47  895243945.67  584138082.95   727033513.09
    "000507.SZ" 18 12 1 18518            .             .  474686837.38 1230849577.35 1337650590.64 1313616853.71 1314869563.35 1164223266.46 1013071612.45 1058965575.44  1133731277.25
    "000509.SZ" 16  6 1 14606            .             .  207641314.45  397473525.88  520031068.17  542495576.22  570599185.26  477560283.85  546952122.62  616456238.62   686734973.65
    "000510.SZ" 10  6 1 13574 105677229.37  137148729.95  230284764.13  462128501.38  516789716.73  617450052.55  854247217.14 1174230790.01 1067716438.34  951833121.16   1001453494.7
    "000513.SZ"  2 12 1 13592 268845315.91  352508053.64  594264876.52  797271604.35  870122308.98 1109809679.91 1316717981.45 1510123563.11 1560922291.37 1557847725.23  1588290694.11
    "000514.SZ"  8 28 1 14602 180160088.25  1117905063.7  181315706.38  265348454.13  271804321.17  311150782.54  331979859.37  334551249.84   500593457.5  502159437.59   497659239.54
    "000516.SZ" 19 29 1 13544   8260166.91    9346183.34   32580936.78  182376925.51  295852582.51     321478158  382776723.26  410466119.16   434557125.4  507228650.88   635785503.27
    "000517.SZ"  8 19 1 14560  37093320.18   37917275.28    52290730.8     167756728  245907197.29  271419989.04  271676603.79  266056903.69  309924230.67  284208197.69   570706203.43
    "000518.SZ"  2 15 1 14418            .             .             .  254614668.11  283191321.56  261386145.82  273912336.02  299236197.42  180325080.68  216714913.75   274672391.51
    "000519.SZ" 16 22 1 13948            .             .  114796113.56  181027982.49  203469784.28  214446543.64  227919025.72  245751366.55  307564617.68  366735651.77   397504771.31
    "000520.SZ" 18 21 1 14916            .             .             .  379762643.54  540250527.15  633110860.39  718939564.77  975285131.88 1358330183.86 1359483418.05  1386633792.81
    "000521.SZ" 14  9 1 21367    259907817     339572262     407868529  645946873.35  850486982.99 1299299786.92 2198284694.84 2327384217.61 2433647588.93 2545961019.55  2526682141.54
    "000523.SZ"  . 12 0     .    165686000     195810000     285110000  454125659.05   525601426.8  622640593.79  666652498.22   672040829.4  654708998.14  702147302.56   649289756.09
    "000524.SZ" 12 12 1 20248            .             .             .  479595133.98   530035431.9  534224720.36  564706243.21  860748929.94  803179558.72  770847348.66   801668798.23
    "000525.SZ" 10 15 1 14627            .             .     114423821     237536051  263735627.44  303007191.12  274390840.66  280901509.82  326497148.84  324519649.52   634380381.56
    "000526.SZ" 12 24 1 12828            .             .   70694569.32  138949653.05  150906079.32  158311240.75  170605750.54     192262412  188543588.52  206850580.17   227445135.39
    "000528.SZ"  . 13 0     .            .             .     432186424     910116857     912086929    1081379077 1137243060.39 1217033276.42 1226979365.85 1145327137.24  1205027128.23
    "000529.SZ" 16 12 1 12599    293518625     293216739     719615258 1345179878.72 1574423405.28 1886838990.81 2306127401.62 2752052749.68 3032345929.33 2822499969.23   2746059720.9
    "000530.SZ"  . 27 0     .            .             .             .  607575396.57  654926424.58  707914192.78  814797704.05 1102419785.45 1547026481.76 1615977240.16  1767519306.58
    "000531.SZ"  1 12 1 12935   83711498.7   83913591.37  116095701.28  242388258.57  283281880.44  882410243.71 1117829231.69 1328512673.97 1594260389.19 1492291916.08  1442024259.15
    "000532.SZ"  9 12 1 15916            .             .     449047701  746085395.14 1292634796.23 1428830228.92  860120654.51  868060143.71  643918640.77  566369639.67   410388441.63
    "000533.SZ"  . 12 0     .            .             .             .    1781281528    2175591265    2457865523    2888580425 2803012369.76 3283957021.57 3258460631.23  3696908330.53
    "000534.SZ"  8 12 1 18150            .             .  412862796.04  591104864.86  541062890.11  555896073.39  592090648.77  595773429.55  616255740.06  558880817.57   571016140.72
    "000536.SZ"  9 24 1 18659            .             .             .  416513094.25  490977019.47  572178615.62  592309972.83  596534623.28  458951024.77  415022059.49   377061649.39
    "000537.SZ"  8  7 1 14525            .             .             .  549292719.82  680894785.79  817061247.87  752094596.27  490823429.59  575338349.87  617599265.87  1148168943.63
    "000538.SZ"  2  2 1 13511     55717710      59415510      63192800  176268443.89  200830533.05   246173776.2  259255594.88  315438264.85  351994768.71  761622684.72   792850527.31
    "000539.SZ"  . 12 0     .            .             .             .    2482083494    2805472644    4592873696    5663322115 7033459507.48 7820548144.33 8587589344.58  11299123769.9
    "000540.SZ"  8 26 1 14138  56042455.12   62106617.43  114643447.79  299610581.59     363214274     373902398  389766468.36  412355594.71  483486771.89  564200911.03  1082508597.83
    "000541.SZ"  . 12 0     .            .             .             .  681029891.43  888178181.27 1301987644.52 1425449802.55 1384736364.19 1415953476.79 1462921963.07  2171997490.15
    "000543.SZ"  1  9 1 14610    378118879     366897264     359665526 1119505125.03 1407921197.26 1481865108.08 1990077300.67    2079625203 2397054507.17 2679910145.64  2790321817.13
    "000544.SZ"  1 18 1 12843            .             .             .     787597970  865764791.51 1064347649.82 1219829179.01 1427249399.54 1542659223.64 1283244239.33   1275388293.9
    "000545.SZ" 10  5 1 14634            .             .  179042455.69  337203541.76  341024480.24  422336370.28  437789886.78  420231945.46  487178758.52  336495575.44   340642077.51
    "000546.SZ" 10  5 1 12964            .             .             .  264236234.91  451404115.93  514895586.17  673929725.64   867052342.5  909800190.33  577958226.28   396024004.95
    "000547.SZ" 16 24 1 15278     48175308      56071132      92914981  161964874.09  196280834.25  308412042.18  362504004.25  363721847.62  524856742.47  758850314.68   996927913.79
    "000550.SZ"  . 16 0     .            .             .             . 2595246375.38 3941915278.18 4516989818.66 4592643303.54 4535306217.76 4826601547.24 4501188265.57  4302038522.66
    "000551.SZ" 16 15 1 14692            .             .             .  476691499.12  842345465.56 1152042114.47 1263644374.52  1160825727.6 1042481984.06  813419140.39   1197238183.8
    "000552.SZ" 15 23 1 15494            .             .             .  362445890.95  561071701.15  612757087.12  676901951.36  712330327.23   664607751.6  393895108.03   520333328.41
    "000553.SZ"  . 21 0     .            .             .     245800585     418658866     469037007     736238858  931774340.42 1485069503.84 1614999429.73 1787995430.62  1897377517.05
    "000554.SZ" 15 10 1 14806            .             .             .  476862113.59  770079720.35  950367498.89 1030646910.49 1093383616.68 1195150761.69  1040183628.1  1197169139.85
    "000555.SZ" 17 12 1 15022            .             .             .     129599326     145842926     174162208  153783492.76  164658204.17  154208153.79  122566852.17   114498048.89
    "000558.SZ" 12 19 1 15425            .             .   355690456.7   416800793.6  656939356.11  705403915.92  725721703.61  629570555.39     380943104  546304922.31   531812820.91
    "000559.SZ"  . 19 0     .     63564571      73905649     111154341     234815603  284018753.71  348675483.53  748417403.29  912581170.58 1233234000.74 1378464935.97  1592732879.67
    "000561.SZ"  9 29 1 18332            .             .     397746957     601548967    1005952851    1271135652    1772022462    2170993343 2276370451.76 1927404270.36  1648605753.83
    "000563.SZ"  6 29 1 17801 295701138.07  269457984.44  534976125.14  901616794.98 1614541969.75 1869839876.67 2043758740.72 2273557531.62 2352058118.01 2715994227.66  3618322867.13
    "000564.SZ" 19 29 1 12662  40900924.68   45684870.31  231747714.78  327176108.28  498631264.69  717962138.82   914272094.1 1029220080.38 1165690493.29 1067887833.51   991703998.74
    "000565.SZ"  . 28 0     .    131626480     151143941     165876075  218753117.04  262552815.07   290745361.6  347886244.54  385411464.58  408542117.01   386048906.8   482445942.45
    "000566.SZ"  2 20 1 14052     61561808     136700483  286151053.17  583436491.75  787463143.83  844047598.76  953112675.99  925613406.69  973847427.69  914732521.83   885584043.55
    "000567.SZ"  6 20 1 15894 114497936.82  122476051.98  256897325.56  393754208.41  477034319.31  548339079.53  538556515.76  606773477.88  685540639.94 1644751176.48   425145728.97
    "000568.SZ"  .  6 0     .            .  267996711.13  384756827.94  504176672.95  686802071.04  857606344.94 1064188527.81 1352865778.75 1781117677.49  1823514920.7  2038171431.77
    "000570.SZ"  . 15 0     .    149407800     162280900     255109400  429471562.07   632530833.5  1008787301.8 2608523334.02 2963071758.56  3502864815.1 3414049959.82     3574547384
    "000571.SZ" 16 20 1 15326            .             .  134993965.33  353643972.72  598359695.92 1105430024.93 1619852882.52 2856568210.76 2845558422.41    2759968326  2911717294.62
    "000572.SZ" 11 20 1 17178            .             .             .  576629957.55  776829750.91  922617097.73 1004241183.34 1057181998.82 1073099698.89 1054834457.11   913255999.57
    "000573.SZ"  . 12 0     .            .             .             .  644577935.86 1351481367.94 1530617638.17 1560263399.28 1593008480.36 1869700244.35 2014204053.91  2270217401.18
    "000576.SZ"  . 12 0     .            .     241435807     402056540  552549549.67  692679303.21  784244463.28 1081012865.64 1208763052.89 1510508382.35 1475052729.98  1663512918.44
    "000581.SZ" 11 15 1 14951            .             .             .             .             .     533976000  636989437.89   717032142.8 1553650555.96 1699616407.23  1992760589.74
    "000582.SZ" 18 13 1 16582            .             .             .             .  401881559.65  468514642.88  514091487.33  568024066.98  632518435.53  918835320.14   935749462.16
    end
    format %td rendate
    label values changed_name group
    label def group 0 "Group 2", modify
    label def group 1 "Group 1", modify

    Thank you very much,
    Rachita
    Last edited by rachita vig; 19 Dec 2020, 02:10.

  • #2
    Code:
    //  EASIER TO DO THIS IN LONG LAYOUT
    reshape long total_ass, i(id) j(year)
    drop if missing(total_ass, province, year)
    //  SEPARATE DATA INTO CASES AND CONTROLS
    preserve
    keep if changed_name == 0
    keep id province year total_ass
    rename (id total_ass) =_ctrl
    tempfile controls
    save `controls'
    restore
    keep if changed_name == 1
    keep if year == year(rendate) - 1   // WORK ONLY WITH THE CORRECT YEAR FOR CASES
    keep id province year total_ass
    rename (id total_ass) =_case
    
    //  JOIN THE CASES AND CONTROLS
    joinby province year using `controls'
    
    //  CALCULATE DIFFERENCE IN ASSETS
    gen delta = abs(total_ass_case - total_ass_ctrl)
    
    //  NOW SELECT BEST MATCHES WITHOUT REPLACEMENT
    sort id_case delta
    local i = 1
    while `i' <= `=_N' {
        drop if id_case == id_case[`i'] & _n > `i'
        drop if id_ctrl == id_ctrl[`i'] & _n > `i'
        local ++i
    }
    keep id_case id_ctrl
    At the end of this code you will have in memory a data set where each observation identifies the id_case and id_ctrl of a matched pair that meets your requirements. Each pair appears onceYou will then need to merge this back with the original data twice, once on id_case and again on id_ctrl to bring in the complete information.

    Note, by the way, how simple this becomes once you put the data into long layout. This is typical in Stata: nearly everything in Stata data management and analysis works better (or only) with data in long layout. The wide layout is useful for visual displays of the data, some graphing commands, and a handful of other purposes. But as a general rule, if you have wide data, the first thing you should do is -reshape- it to long unless you are quite sure you will be doing one of the few things that is best with wide layout.

    A couple of things to think about:

    1. Do you really need to match without replacement? There is no statistical reason to do that. It seems that many people prefer it, I suppose for aesthetic reasons, but it in no way improves subsequent analysis. In fact, it damages it--and your example data is a case in point. You have 42 distinct case firms in the example, and if replacement is allowed, all 42 of them can match. But matching without replacement only matches 33 of them: the others end up with no match at all because all their potential matches got used up by another firm that was "ahead of them in line."

    2. What do you mean by closest assets? In the code above, I take that to mean minimum absolute value of the difference in assets. But I have seen, on this forum, others who have done this sort of thing but defined closest as being ratio of assets closest to 1. I don't know if there are standard conventions about this in your field, or if there are situations where ratio is preferred to difference and vice versa. But if you need to use a ratio measure, just change the code accordingly in the command that calculates the delta variable.

    Added: If your full data set is very large, this is going to take a long time to run. Be patient. Find something to occupy yourself.
    Last edited by Clyde Schechter; 19 Dec 2020, 09:43.

    Comment


    • #3
      Thanks a lot, Clyde! By closest assets I meant choosing the pair having the smallest difference in assets which is what you have done.

      Comment


      • #4
        How do you alter this code to get 1:3 matching without replacement

        Comment


        • #5
          The code is unchanged until the part headed // NOW SELECT BEST MATCHES WITHOUT REPLACEMENT.

          Code:
          //  NOW SELECT BEST MATCHES WITHOUT REPLACEMENT
          sort id_case delta, stable
          local i = 1
          while `i' <= `=_N' {
              drop if id_case == id_case[`i'] & _n > `i' + 2
              quietly count if id_case == id_case[`i']
              local available `r(N)'
              quietly levelsof id_ctrl in `i'/`=`i'+`available'-1', separate(",") local(match_list)
              drop if inlist(id_ctrl, `match_list') & _n >= `i' + `available'
              quietly count if id_case == id_case[`i']
              local i = `i' + `r(N)'
          }
          keep id_case id_ctrl
          Use at your peril. There is no statistical reason to sample without replacement, and doing so comes at a terrible price. It's already moderately problematic for 1:1 matching, but for n:1 matching (n > 1) it is much worse. You get a large number of cases who fail to find any matches at all, and many cases that, with replacement, would find n matches, end up with fewer.

          So, I've shown you how to make the rope. It's up to you whether you use it to hang yourself.


          Comment


          • #6
            Thank you Dr. Clyde Schechter , I was able to find controls without replacement for the same length of stay, however how do I alter the code to match for the same or greater then LOS. I have pasted my code chunk below for reference. Thank you


            preserve
            keep if case == 0
            tempfile controls
            save `controls'
            restore
            keep if case==1
            rangejoin LOS_cumulative_days 0 . using `controls', by(adm_month adm_year)



            // NOW SELECT BEST MATCHES WITHOUT REPLACEMENT 1:3 ****
            sort Person_visit_ID, stable
            local i = 1
            while `i' <= `=_N' {
            drop if Person_visit_ID == Person_visit_ID[`i'] & _n > `i' + 2
            quietly count if Person_visit_ID== Person_visit_ID[`i']
            local available `r(N)'
            quietly levelsof Person_visit_ID_U in `i'/`=`i'+`available'-1', separate(",") local(match_list)
            drop if inlist(Person_visit_ID_U, `match_list') & _n > `i' + `available'
            quietly count if Person_visit_ID == Person_visit_ID[`i']
            local i = `i' + `r(N)'
            }

            Comment

            Working...
            X