Dear ChrisNZ, I think I messed up in my question. Actually, I am trying to obtain 10 control firms for each of my sample firms with year and industry fixed effect. Here. fyear indicates financial year, cusip indicates company id., sic2 indicates industry id, wBAHR1hat_rank indicates ranking of each company in the corresponding financial year based on some measure. Now, my matching procedure pairs the sample firms with adjacent control firms from control firm file in terms of the wBAHR1hat_rank. It means that company cusip 123 will mached to the nearest ranked control firms in the same year and the same industry. The rank of the control firms should be less than the rank of the sample firm. I may require 2 control firms in some cases and also require 10 control firms in some other cases. If i need two control firms, then cusip=123 in year 1990 and industry 12 with its ranking=12 should be matched with cusip 124 and 125 in year 1990 and industry 12. cusip=859 in year 2000 and industry 11 with its ranking=15 should be matched with cusip 868 and 869 in year 1990 and industry 12. I included my desired sample output in case of two control firms here. my sample firm file looks like the following: fyear cusip SIC2 wBAHR1hat_rank 1990 123 12 12 2000 859 11 15 my control firm file: fyear cusip SIC2 wBAHR1hat_rank 1990 124 12 13 1990 125 12 14 1990 126 12 15 1990 127 12 16 1990 128 12 17 1990 129 12 18 1990 759 13 19 1990 760 15 20 1990 761 12 21 1990 762 12 22 1990 763 12 23 1990 764 12 24 2000 860 11 21 2000 861 11 12 2000 863 11 23 2000 864 11 13 2000 867 11 14 2000 868 11 16 2000 869 11 17 2000 870 12 18 2000 872 14 19 2000 873 11 20 2000 874 11 21 2000 876 11 22 2000 877 11 23 my expected output in case of two control firms: fyear cusip SIC2 wBAHR1hat_rank CUSIP_Matching1 CUSIP_Matching2 1990 123 12 12 124 125 2000 859 11 15 868 869 I am using the following code. But with this code I am getting only one control firm. proc sql; create table match1 as select a.*, b.CUSIP as CUSIP_Matching from joint8 a left join joint9 b on a.fyear=b.fyear and a.sic2=b.sic2 where b.wBAHR1hat_Rank >= a.wBAHR1hat_Rank group by a.wBAHR1hat_Rank,a.fyear, a.sic2 having abs(a.wBAHR1hat_Rank-b.wBAHR1hat_Rank)=min(abs(a.wBAHR1hat_Rank-b.wBAHR1hat_Rank)) order by a.fyear, b.wBAHR1hat_Rank descending ; quit;
... View more