Dear Statalisters,

I try to match my event firms (#21067) on a control firm on size and book-to-market. To identify a size and book-to-market matched control firm, I first want to identify all firms with market value between 70 percent and 130 percent of the market value of equity of the sample firm; from this set of firms, I choose the firm with the book-to-market ratio closest to that of the sample firm. I want to base the values at event date. Only benchmark firms that are listed during the event period are eligible.

I eventually want to generate the difference in returns for these firms over an event period (event date, +750 trading days).

I have the events in a panel data file that goes from event date to +750 trading dates (dates are formatted with a business calendar):


Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input float eventid double(date PERMNO) float(mcap2 btm) int tau float compldate
1 1933 50876 4356.1934  .4958016 657 1276
1 1691 50876 4356.1934  .4572884 415 1276
1 1969 50876 4356.1934  .4267233 693 1276
1 1328 50876 4356.1934   .428662  52 1276
1 1643 50876 4356.1934  .3834704 367 1276
1 1979 50876 4356.1934  .4258096 703 1276
1 1299 50876 4356.1934  .3943298  23 1276
1 1947 50876 4356.1934  .4487956 671 1276
1 1392 50876 4356.1934  .4107157 116 1276
1 1658 50876 4356.1934    .40468 382 1276
1 1610 50876 4356.1934  .3674152 334 1276
1 1560 50876 4356.1934  .3630499 284 1276
1 1853 50876 4356.1934  .3960487 577 1276
1 1705 50876 4356.1934    .47871 429 1276
1 1410 50876 4356.1934  .3932796 134 1276
1 1648 50876 4356.1934  .3908448 372 1276
1 1611 50876 4356.1934  .3623324 335 1276
1 1375 50876 4356.1934  .3989248  99 1276
1 1807 50876 4356.1934 .42940915 531 1276
1 1763 50876 4356.1934  .4118523 487 1276
1 1404 50876 4356.1934  .3859967 128 1276
1 1911 50876 4356.1934  .4437867 635 1276
1 1805 50876 4356.1934  .4322096 529 1276
1 1943 50876 4356.1934 .47224805 667 1276
1 1825 50876 4356.1934  .4437867 549 1276
1 1603 50876 4356.1934  .3623324 327 1276
1 1931 50876 4356.1934 .51910293 655 1276
1 2025 50876 4356.1934  .4744685 749 1276
1 1397 50876 4356.1934  .4168764 121 1276
1 1533 50876 4356.1934  .3659485 257 1276
1 1528 50876 4356.1934  .3580863 252 1276
1 1387 50876 4356.1934  .4168764 111 1276
1 1897 50876 4356.1934  .4447795 621 1276
1 1489 50876 4356.1934  .3554119 213 1276
1 1500 50876 4356.1934  .3523935 224 1276
1 1337 50876 4356.1934  .4308053  61 1276
1 1336 50876 4356.1934  .4329701  60 1276
1 1399 50876 4356.1934  .4057192 123 1276
1 1754 50876 4356.1934  .4118631 478 1276
1 1752 50876 4356.1934 .41093755 476 1276
1 1659 50876 4356.1934  .4055773 383 1276
1 1326 50876 4356.1934  .4452768  50 1276
1 1977 50876 4356.1934  .4212989 701 1276
1 1942 50876 4356.1934  .4733725 666 1276
1 1558 50876 4356.1934  .3532566 282 1276
1 1362 50876 4356.1934  .4200266  86 1276
1 1562 50876 4356.1934  .3674152 286 1276
1 1289 50876 4356.1934  .3812436  13 1276
1 1929 50876 4356.1934 .51373756 653 1276
1 1895 50876 4356.1934   .434097 619 1276
1 1482 50876 4356.1934  .3763658 206 1276
1 1298 50876 4356.1934  .3952342  22 1276
1 1742 50876 4356.1934  .4460176 466 1276
1 1471 50876 4356.1934   .410835 195 1276
1 1641 50876 4356.1934  .3717792 365 1276
1 1538 50876 4356.1934  .3749288 262 1276
1 1292 50876 4356.1934  .4007491  16 1276
1 1813 50876 4356.1934 .45600095 537 1276
1 1872 50876 4356.1934  .4203307 596 1276
1 1836 50876 4356.1934  .4303386 560 1276
1 1690 50876 4356.1934  .4630769 414 1276
1 1349 50876 4356.1934   .431885  73 1276
1 1282 50876 4356.1934  .3795641   6 1276
1 2002 50876 4356.1934  .4160106 726 1276
1 1912 50876 4356.1934  .4437867 636 1276
1 1999 50876 4356.1934  .4267233 723 1276
1 1840 50876 4356.1934  .4133398 564 1276
1 1834 50876 4356.1934 .42940915 558 1276
1 1316 50876 4356.1934   .431885  40 1276
1 1465 50876 4356.1934   .398984 189 1276
1 1799 50876 4356.1934  .4303386 523 1276
1 1824 50876 4356.1934 .45704925 548 1276
1 1355 50876 4356.1934 .43515685  79 1276
1 1896 50876 4356.1934  .4477847 620 1276
1 1584 50876 4356.1934  .3580863 308 1276
1 2016 50876 4356.1934  .4360813 740 1276
1 1483 50876 4356.1934  .3655889 207 1276
1 1593 50876 4356.1934  .3376431 317 1276
1 1996 50876 4356.1934  .4195212 720 1276
1 1512 50876 4356.1934   .338452 236 1276
1 1735 50876 4356.1934  .4302758 459 1276
1 1904 50876 4356.1934  .4418143 628 1276
1 1376 50876 4356.1934  .3970252 100 1276
1 1304 50876 4356.1934  .4142358  28 1276
1 1882 50876 4356.1934  .4159339 606 1276
1 1343 50876 4356.1934  .4275983  67 1276
1 2011 50876 4356.1934 .43896925 735 1276
1 1487 50876 4356.1934  .3623959 211 1276
1 1723 50876 4356.1934  .4594654 447 1276
1 2012 50876 4356.1934  .4478673 736 1276
1 1657 50876 4356.1934  .4028973 381 1276
1 1327 50876 4356.1934  .4275983  51 1276
1 1585 50876 4356.1934  .3498859 309 1276
1 1484 50876 4356.1934  .3577098 208 1276
1 1769 50876 4356.1934 .44779655 493 1276
1 1475 50876 4356.1934  .4038378 199 1276
1 1831 50876 4356.1934   .434097 555 1276
1 1808 50876 4356.1934 .43315125 532 1276
1 1740 50876 4356.1934  .4471081 464 1276
1 1466 50876 4356.1934  .4018822 190 1276
end
format %tbtrdates date
format %tbtrdates compldate

compldate stands for completion date, which is the start of the event period.

The benchmark firms are splitted into 4 files as they are too big for my computer to run at once. They all have the same format and range from:
pfile1: 1975-1989
pfile2: 1990-1999
pfile3: 2000-2010
pfile4: 2011-2020

example:

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input double PERMNO float date double RET float(mcap2 btm)
10001 3913  .025641025975346565  10.52675 .6004702
10001 3886                    0     10.27  .615482
10001 3989                    0     9.918 .6373261
10001 3791                    0         .        .
10001 3795  .006211180239915848     10.22 .6184931
10001 3950   .03846153989434242  10.05225 .6288145
10001 3973                    0    9.7945 .6453622
10001 3840   .03846153989434242    9.9645 .6343519
10001 4008  .012987012974917889     9.918 .6373261
10001 3870                    0     10.27  .615482
10001 3889  .025641025975346565     10.27  .615482
10001 3800  -.02469135820865631  10.34775 .6108575
10001 3932                    0 10.438875  .605525
10001 3956 -.025641025975346565  10.05225 .6288145
10001 3958              -.03125    9.7945 .6453622
10001 3954  -.01886792480945587     10.31 .6130941
10001 3819 -.006289307959377766 10.156125 .6223831
10001 3978                    0 10.181125 .6208548
10001 3896  .025641025975346565     10.27  .615482
10001 3871                    0 10.141625 .6232729
10001 3942                    0     10.31 .6130941
10001 4042                    0     9.657 .6545511
10001 3945  -.02500000037252903  10.56775 .5981406
10001 3847 -.018518518656492233    9.9645 .6343519
10001 3925                    0  10.56775 .5981406
10001 3940  .025641025975346565  10.56775 .5981406
10001 3965   .02064516209065914  9.987812 .6328713
10001 3839  .012820512987673283  10.34775 .6108575
10001 3926                    0  10.56775 .5981406
10001 3807  .012658228166401386     10.22 .6184931
10001 3809 -.012500000186264515     10.22 .6184931
10001 4020   .02631578966975212    10.179 .6209844
10001 3796                    0 10.283875 .6146516
10001 4029   .06891892105340958     9.657 .6545511
10001 3994                    0     9.918 .6373261
10001 3957   .05263157933950424  10.05225 .6288145
10001 3977 -.037974681705236435    9.7945 .6453622
10001 3832  .012658228166401386     10.22 .6184931
10001 3854                    0 10.156125 .6223831
10001 3881  .012658228166401386 10.141625 .6232729
10001 4025                    0   10.0485 .6290491
10001 3816  .012658228166401386     10.22 .6184931
10001 4007   .01315789483487606     9.918 .6373261
10001 3922  .012345679104328156  10.56775 .5981406
10001 3803 -.012345679104328156  10.34775 .6108575
10001 3891  .012820512987673283     10.27  .615482
10001 3962                    0    9.7945 .6453622
10001 4033                    0     9.918 .6373261
10001 3990 -.012987012974917889   10.0485 .6290491
10001 3812  .006329114083200693     10.22 .6184931
10001 3793                    0  10.34775 .6108575
10001 4037                    0     9.918 .6373261
10001 3981                    0    10.179 .6209844
10001 3860                    0     10.27  .615482
10001 3872  .012658228166401386 10.141625 .6232729
10001 3900                    0  10.01325 .6312636
10001 3833 -.012500000186264515  10.09225 .6263222
10001 3975 -.012987012974917889  10.05225 .6288145
10001 3877  -.02500000037252903 10.141625 .6232729
10001 3887  .025641025975346565  10.01325 .6312636
10001 3955                    0 10.245563   .61695
10001 3967 -.025641025975346565  10.05225 .6288145
10001 3934  .037974681705236435     10.31 .6130941
10001 3971 -.012987012974917889    9.7945 .6453622
10001 3830  .025641025975346565    9.9645 .6343519
10001 4000 -.012987012974917889   10.0485 .6290491
10001 3943                    0  10.56775 .5981406
10001 3931 -.012345679104328156     10.31 .6130941
10001 3901                    0     10.27  .615482
10001 3810  .012658228166401386     10.22 .6184931
10001 4019 -.025641025975346565    9.7875 .6458237
10001 3798                    0  10.34775 .6108575
10001 3997                    0   10.0485 .6290491
10001 4026   .02631578966975212     9.918 .6373261
10001 3846   .03846153989434242  10.34775 .6108575
10001 3998 -.012820512987673283    10.179 .6209844
10001 3912  -.04878048598766327  10.01325 .6312636
10001 3805                    0     10.22 .6184931
10001 3888  -.02500000037252903  10.01325 .6312636
10001 3892  .012658228166401386  10.01325 .6312636
10001 4006                    0   10.0485 .6290491
10001 3935  -.03658536449074745 10.181125 .6208548
10001 4043                    0     9.918 .6373261
10001 3908                    0     10.27  .615482
10001 3996  .012987012974917889     9.918 .6373261
10001 3995   .01315789483487606     9.918 .6373261
10001 3822 -.012500000186264515  10.09225 .6263222
10001 3849   .03846153989434242 10.156125 .6223831
10001 3845  -.03703703731298447 10.156125 .6223831
10001 4040  -.02631578966975212     9.657 .6545511
10001 4039  .027027027681469917     9.657 .6545511
10001 3960 -.037974681705236435  9.987812 .6328713
10001 3987 -.012987012974917889   10.0485 .6290491
10001 4028                    0    10.179 .6209844
10001 3835 -.018518518656492233  10.09225 .6263222
10001 3815 -.012500000186264515  10.09225 .6263222
10001 3867 -.012658228166401386     10.27  .615482
10001 3939  -.04878048598766327     10.31 .6130941
10001 3857  .025641025975346565 10.141625 .6232729
10001 3898 -.012658228166401386     10.27  .615482
end
format %tbtrdates date
Many thanks for your input!