<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Merging two tables by choosing the CLOSEST dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372417#M276020</link>
    <description>&lt;P&gt;Dear ChrisNz,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;After I run the following code, I did not get consistent &amp;nbsp;result. I have included the picture of my output file at the bottom of the post. As you can see, firm 859 in year 2011 should not have any matched firm but my output shows matched firms. &amp;nbsp;also firm 859 in year 2000 should not have any cusip_matching9 and&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;cusip_matching9.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to keep all the sampling firms including those for which there is no matching firms. Your last code keeps all the sampling firms but gives inconsistent results.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have included the snapshot of the output file in this post.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;cards;&lt;BR /&gt;1990 123 12 12&lt;BR /&gt;2000 859 11 15&lt;BR /&gt;2001 859 11 15&lt;BR /&gt;;&lt;BR /&gt;data control;&lt;BR /&gt;input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;cards;&lt;BR /&gt;1990 124 12 13&lt;BR /&gt;1990 125 12 14&lt;BR /&gt;1990 126 12 15&lt;BR /&gt;1990 127 12 16&lt;BR /&gt;1990 128 12 17&lt;BR /&gt;1990 129 12 18&lt;BR /&gt;1990 759 13 19&lt;BR /&gt;1990 760 15 20&lt;BR /&gt;1990 761 12 21&lt;BR /&gt;1990 762 12 22&lt;BR /&gt;1990 763 12 23&lt;BR /&gt;1990 764 12 24&lt;BR /&gt;2000 860 11 21&lt;BR /&gt;2000 861 11 12&lt;BR /&gt;2000 863 11 23&lt;BR /&gt;2000 864 11 13&lt;BR /&gt;2000 867 11 14&lt;BR /&gt;2000 868 11 16&lt;BR /&gt;2000 869 11 17&lt;BR /&gt;2000 870 12 18&lt;BR /&gt;2000 872 14 19&lt;BR /&gt;2000 873 11 20&lt;BR /&gt;2000 874 11 21&lt;BR /&gt;2000 876 11 22&lt;BR /&gt;2000 877 11 23&lt;BR /&gt;;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table _V as&lt;BR /&gt;select a.*&lt;BR /&gt;,abs(a.WBAHR1HAT_RANK-b.WBAHR1HAT_RANK) as DIF&lt;BR /&gt;,b.CUSIP as CUSIPB&lt;BR /&gt;from SAMPLE a&lt;BR /&gt;left join&lt;BR /&gt;CONTROL b&lt;BR /&gt;on a.FYEAR = b.FYEAR&lt;BR /&gt;and a.SIC2 = b.SIC2&lt;BR /&gt;and b.WBAHR1HAT_RANK &amp;gt;= a.WBAHR1HAT_RANK&lt;BR /&gt;order by 1,2,3,4,5 ;&lt;BR /&gt;quit;&lt;BR /&gt;proc sort data=_V; by FYEAR CUSIP SIC2 WBAHR1HAT_RANK; run;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set _V;&lt;BR /&gt;retain N 0 CUSIP_MATCHING1-CUSIP_MATCHING12;&lt;BR /&gt;by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;&lt;BR /&gt;N=N*^first.WBAHR1HAT_RANK+1;&lt;BR /&gt;if N= 1 then CUSIP_MATCHING1 = CUSIPB;&lt;BR /&gt;if N= 2 then CUSIP_MATCHING2 = CUSIPB;&lt;BR /&gt;if N= 3 then CUSIP_MATCHING3 = CUSIPB;&lt;BR /&gt;if N= 4 then CUSIP_MATCHING4 = CUSIPB;&lt;BR /&gt;if N= 5 then CUSIP_MATCHING5 = CUSIPB;&lt;BR /&gt;if N= 6 then CUSIP_MATCHING6 = CUSIPB;&lt;BR /&gt;if N= 7 then CUSIP_MATCHING7 = CUSIPB;&lt;BR /&gt;if N= 8 then CUSIP_MATCHING8 = CUSIPB;&lt;BR /&gt;if N= 9 then CUSIP_MATCHING9 = CUSIPB;&lt;BR /&gt;if N=10 then CUSIP_MATCHING10 = CUSIPB;&lt;BR /&gt;if N=11 then CUSIP_MATCHING11 = CUSIPB;&lt;BR /&gt;if N=12 then CUSIP_MATCHING12 = CUSIPB;&lt;BR /&gt;if N=12 or last.WBAHR1HAT_RANK then output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, your first code gives me consistent results but it removes the sampling firms that have no matching firms.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10063iC955FEA93E31612A/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="1.PNG" title="1.PNG" /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10064i4F9E622C33C0362A/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="2.PNG" title="2.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 01 Jul 2017 14:14:08 GMT</pubDate>
    <dc:creator>nazmul</dc:creator>
    <dc:date>2017-07-01T14:14:08Z</dc:date>
    <item>
      <title>Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372314#M276005</link>
      <description>&lt;DIV class="lia-message-heading lia-component-message-header"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="lia-component-body"&gt;&lt;DIV class="lia-message-body"&gt;&lt;DIV class="lia-message-body-content"&gt;&lt;P&gt;Dear Everyone;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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,&amp;nbsp;&lt;SPAN&gt;wBAHR1hat_rank indicates ranking of each company in the corresponding financial year based on some measure. Now,&amp;nbsp;my&amp;nbsp;matching procedure pairs the sample&amp;nbsp;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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&amp;nbsp;year 1990 and industry 12.&amp;nbsp;cusip=859 in year 2000 and industry 11 with its ranking=15 should be matched with cusip 868&amp;nbsp;and 869 in&amp;nbsp;year 1990 and industry 12. I included my desired sample output in case of two control firms here.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my sample firm file looks like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fyear cusip SIC2 wBAHR1hat_rank&lt;BR /&gt;1990 123 12 12&lt;BR /&gt;2000 859 11 15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my control firm file:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fyear cusip SIC2 wBAHR1hat_rank&lt;BR /&gt;1990 124 12 13&lt;BR /&gt;1990 125 12 14&lt;BR /&gt;1990 126 12 15&lt;BR /&gt;1990 127 12 16&lt;BR /&gt;1990 128 12 17&lt;BR /&gt;1990 129 12 18&lt;BR /&gt;1990 759 13 19&lt;BR /&gt;1990 760 15 20&lt;BR /&gt;1990 761 12 21&lt;BR /&gt;1990 762 12 22&lt;BR /&gt;1990 763 12 23&lt;BR /&gt;1990 764 12 24&lt;BR /&gt;2000 860 11 21&lt;BR /&gt;2000 861 11 12&lt;BR /&gt;2000 863 11 23&lt;BR /&gt;2000 864 11 13&lt;BR /&gt;2000 867 11 14&lt;BR /&gt;2000 868 11 16&lt;BR /&gt;2000 869 11 17&lt;BR /&gt;2000 870 12 18&lt;BR /&gt;2000 872 14 19&lt;BR /&gt;2000 873 11 20&lt;BR /&gt;2000 874 11 21&lt;BR /&gt;2000 876 11 22&lt;BR /&gt;2000 877 11 23&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my expected output in case of two control firms:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fyear cusip SIC2 wBAHR1hat_rank CUSIP_Matching1 CUSIP_Matching2&lt;BR /&gt;1990 123 12 12 124 125&lt;BR /&gt;2000 859 11 15 868 869&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the following code. But with this code I am getting only one control firm.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table match1 as&lt;BR /&gt;select a.*, b.CUSIP as CUSIP_Matching&lt;BR /&gt;from joint8 a&lt;BR /&gt;left join&lt;BR /&gt;joint9 b&lt;BR /&gt;on a.fyear=b.fyear and a.sic2=b.sic2&lt;BR /&gt;where b.wBAHR1hat_Rank &amp;gt;= a.wBAHR1hat_Rank&lt;BR /&gt;group by a.wBAHR1hat_Rank,a.fyear, a.sic2&lt;BR /&gt;having abs(a.wBAHR1hat_Rank-b.wBAHR1hat_Rank)=min(abs(a.wBAHR1hat_Rank-b.wBAHR1hat_Rank))&lt;BR /&gt;order by a.fyear, b.wBAHR1hat_Rank descending&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 01 Jul 2017 02:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372314#M276005</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T02:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372344#M276006</link>
      <description>&lt;P&gt;Your question is unclear and the desired output is missing.&lt;/P&gt;
&lt;P&gt;Which rows out of the possible crossings below do you keep and why?&lt;/P&gt;
&lt;P&gt;i don't see how 1/12/2006 makes the cut.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
  select a.* , b.DATE as DATEB, abs(a.DATE- b.DATE) as DAYS 
  from A
        left join 
       B 
        on a.TICKER=b.TICKER 
  order by a.TICKER, a.DATE, DAYS ; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;DATE&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;TICKER&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;DATEB&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;DAYS&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;04/06/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2008&lt;/TD&gt;
&lt;TD class="r data"&gt;180&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;04/06/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2007&lt;/TD&gt;
&lt;TD class="r data"&gt;186&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;04/06/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;545&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;04/06/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2006&lt;/TD&gt;
&lt;TD class="r data"&gt;551&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;04/06/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2010&lt;/TD&gt;
&lt;TD class="r data"&gt;910&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;12/12/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2008&lt;/TD&gt;
&lt;TD class="r data"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;12/12/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;354&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;12/12/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2007&lt;/TD&gt;
&lt;TD class="r data"&gt;377&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;12/12/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2010&lt;/TD&gt;
&lt;TD class="r data"&gt;719&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;12/12/2008&lt;/TD&gt;
&lt;TD class="l data"&gt;ABC&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2006&lt;/TD&gt;
&lt;TD class="r data"&gt;742&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;06/02/2002&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2001&lt;/TD&gt;
&lt;TD class="r data"&gt;67&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;06/02/2002&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2007&lt;/TD&gt;
&lt;TD class="r data"&gt;2124&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;06/02/2002&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2008&lt;/TD&gt;
&lt;TD class="r data"&gt;2490&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;06/02/2002&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;2855&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;06/02/2002&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2010&lt;/TD&gt;
&lt;TD class="r data"&gt;3220&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;25/03/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2009&lt;/TD&gt;
&lt;TD class="r data"&gt;114&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;25/03/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2010&lt;/TD&gt;
&lt;TD class="r data"&gt;251&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;25/03/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2008&lt;/TD&gt;
&lt;TD class="r data"&gt;479&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;25/03/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2007&lt;/TD&gt;
&lt;TD class="r data"&gt;845&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;25/03/2010&lt;/TD&gt;
&lt;TD class="l data"&gt;DEF&lt;/TD&gt;
&lt;TD class="r data"&gt;01/12/2001&lt;/TD&gt;
&lt;TD class="r data"&gt;3036&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 00:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372344#M276006</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-07-01T00:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372348#M276007</link>
      <description>&lt;P&gt;Dear ChrisNZ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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,&amp;nbsp;&lt;SPAN&gt;wBAHR1hat_rank indicates ranking of each company in the corresponding financial year based on some measure. Now,&amp;nbsp;my&amp;nbsp;matching procedure pairs the sample&amp;nbsp;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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&amp;nbsp;year 1990 and industry 12.&amp;nbsp;cusip=859 in year 2000 and industry 11 with its ranking=15 should be matched with cusip 868&amp;nbsp;and 869 in&amp;nbsp;year 1990 and industry 12. I included my desired sample output in case of two control firms here.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my sample firm file looks like the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fyear cusip SIC2 wBAHR1hat_rank&lt;BR /&gt;1990 123 12 12&lt;BR /&gt;2000 859 11 15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my control firm file:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fyear cusip SIC2 wBAHR1hat_rank&lt;BR /&gt;1990 124 12 13&lt;BR /&gt;1990 125 12 14&lt;BR /&gt;1990 126 12 15&lt;BR /&gt;1990 127 12 16&lt;BR /&gt;1990 128 12 17&lt;BR /&gt;1990 129 12 18&lt;BR /&gt;1990 759 13 19&lt;BR /&gt;1990 760 15 20&lt;BR /&gt;1990 761 12 21&lt;BR /&gt;1990 762 12 22&lt;BR /&gt;1990 763 12 23&lt;BR /&gt;1990 764 12 24&lt;BR /&gt;2000 860 11 21&lt;BR /&gt;2000 861 11 12&lt;BR /&gt;2000 863 11 23&lt;BR /&gt;2000 864 11 13&lt;BR /&gt;2000 867 11 14&lt;BR /&gt;2000 868 11 16&lt;BR /&gt;2000 869 11 17&lt;BR /&gt;2000 870 12 18&lt;BR /&gt;2000 872 14 19&lt;BR /&gt;2000 873 11 20&lt;BR /&gt;2000 874 11 21&lt;BR /&gt;2000 876 11 22&lt;BR /&gt;2000 877 11 23&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my expected output in case of two control firms:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fyear cusip SIC2 wBAHR1hat_rank CUSIP_Matching1 CUSIP_Matching2&lt;BR /&gt;1990 123 12 12 124 125&lt;BR /&gt;2000 859 11 15 868 869&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the following code. But with this code I am getting only one control firm.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table match1 as&lt;BR /&gt;select a.*, b.CUSIP as CUSIP_Matching&lt;BR /&gt;from joint8 a&lt;BR /&gt;left join&lt;BR /&gt;joint9 b&lt;BR /&gt;on a.fyear=b.fyear and a.sic2=b.sic2&lt;BR /&gt;where b.wBAHR1hat_Rank &amp;gt;= a.wBAHR1hat_Rank&lt;BR /&gt;group by a.wBAHR1hat_Rank,a.fyear, a.sic2&lt;BR /&gt;having abs(a.wBAHR1hat_Rank-b.wBAHR1hat_Rank)=min(abs(a.wBAHR1hat_Rank-b.wBAHR1hat_Rank))&lt;BR /&gt;order by a.fyear, b.wBAHR1hat_Rank descending&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 01:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372348#M276007</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T01:05:31Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372356#M276008</link>
      <description>&lt;P&gt;SQL is not very good at this.&lt;/P&gt;
&lt;P&gt;I tried the monotonic function in vain, and SAS does not have a rank over() function.&lt;/P&gt;
&lt;P&gt;So like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
  create view _V as 
  select a.*
  ,abs(a.WBAHR1HAT_RANK-b.WBAHR1HAT_RANK) as DIF 
  ,b.CUSIP as CUSIPB 
  from SAMPLE   a
  left join
  CONTROL       b
  on  a.FYEAR           = b.FYEAR 
  and a.SIC2            = b.SIC2
  and b.WBAHR1HAT_RANK &amp;gt;= a.WBAHR1HAT_RANK
  order by 1,2,3,4,5  ;
quit;
data WANT;
  set _V;
  retain N 0 CUSIP_MATCHING1;
  by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;
  N=N*^first.WBAHR1HAT_RANK+1;
  if N=1 then CUSIP_MATCHING1 = CUSIPB;
  if N=2 then do;
    CUSIP_MATCHING2 = CUSIPB;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;FYEAR&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;CUSIP&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;SIC2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;WBAHR1HAT_RANK&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;CUSIP_MATCHING1&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;CUSIP_MATCHING2&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1990&lt;/TD&gt;
&lt;TD class="r data"&gt;123&lt;/TD&gt;
&lt;TD class="r data"&gt;12&lt;/TD&gt;
&lt;TD class="r data"&gt;12&lt;/TD&gt;
&lt;TD class="r data"&gt;124&lt;/TD&gt;
&lt;TD class="r data"&gt;125&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2000&lt;/TD&gt;
&lt;TD class="r data"&gt;859&lt;/TD&gt;
&lt;TD class="r data"&gt;11&lt;/TD&gt;
&lt;TD class="r data"&gt;15&lt;/TD&gt;
&lt;TD class="r data"&gt;868&lt;/TD&gt;
&lt;TD class="r data"&gt;869&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 02:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372356#M276008</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-07-01T02:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372360#M276009</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;SPAN class="login-bold"&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961" target="_self"&gt;ChrisNZ&lt;/A&gt;. Your code perfectly works. Could you please tell me what changes I should make if I want 3 or 4 matching firms instead of 2 matchng firms?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;I have to obtain up to 10 matching firms in different situations of my study.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 03:17:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372360#M276009</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T03:17:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372362#M276010</link>
      <description>&lt;P&gt;Here it is for 4:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set _V;
  retain N 0 CUSIP_MATCHING1-CUSIP_MATCHING3;
  by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;
  N=N*^first.WBAHR1HAT_RANK+1;
  if N=1 then CUSIP_MATCHING1 = CUSIPB;
  if N=2 then CUSIP_MATCHING2 = CUSIPB;
  if N=3 then CUSIP_MATCHING3 = CUSIPB;
  if N=4 then do;
    CUSIP_MATCHING4 = CUSIPB;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can of course an an array to remove the repetitive bits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 03:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372362#M276010</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-07-01T03:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372363#M276011</link>
      <description>&lt;P&gt;Thank you so much ChrisNZ. It was a great help. I am grateful to you.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 03:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372363#M276011</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T03:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372364#M276012</link>
      <description>&lt;P&gt;No worries&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 03:32:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372364#M276012</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-07-01T03:32:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372365#M276013</link>
      <description>&lt;P&gt;Dear&amp;nbsp;&lt;SPAN class="login-bold"&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961" target="_self"&gt;ChrisNZ&lt;/A&gt;,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;I made the following changes to obtain 12 matching firms. But I am getting only 4 matching firms. Could you please check the code and fix the problem&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;data test;&lt;BR /&gt;set _V;&lt;BR /&gt;retain N 0 CUSIP_MATCHING1-CUSIP_MATCHING3;&lt;BR /&gt;by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;&lt;BR /&gt;N=N*^first.WBAHR1HAT_RANK+1;&lt;BR /&gt;if N=1 then CUSIP_MATCHING1 = CUSIPB;&lt;BR /&gt;if N=2 then CUSIP_MATCHING2 = CUSIPB;&lt;BR /&gt;if N=3 then CUSIP_MATCHING3 = CUSIPB;&lt;BR /&gt;if N=4 then CUSIP_MATCHING4 = CUSIPB;&lt;BR /&gt;if N=5 then CUSIP_MATCHING5 = CUSIPB;&lt;BR /&gt;if N=6 then CUSIP_MATCHING6 = CUSIPB;&lt;BR /&gt;if N=7 then CUSIP_MATCHING7 = CUSIPB;&lt;BR /&gt;if N=8 then CUSIP_MATCHING8 = CUSIPB;&lt;BR /&gt;if N=9 then CUSIP_MATCHING9 = CUSIPB;&lt;BR /&gt;if N=10 then CUSIP_MATCHING10 = CUSIPB;&lt;BR /&gt;if N=11 then CUSIP_MATCHING11 = CUSIPB;&lt;BR /&gt;if N=12 then do;&lt;BR /&gt;CUSIP_MATCHING4 = CUSIPB;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 04:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372365#M276013</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T04:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372367#M276014</link>
      <description>&lt;P&gt;I got it. Thank you. I appreciate your help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 04:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372367#M276014</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T04:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372368#M276015</link>
      <description>&lt;P&gt;I am sorry. I have one more question. The code you provided only keeps those sample firms which have matching firms from control firm file. But It removes those sample firms that have no matching firms. But I want to keep all the sample firms in my final output file. How can I do it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My sample file has 5100 observations. But after I apply your code, the output file has 4600 observations because the code is removing those sample firms which have no matching firms from the control firm file.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 04:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372368#M276015</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T04:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372379#M276016</link>
      <description>&lt;P&gt;The code only keeps those sample firms which have &lt;STRONG&gt;12&lt;/STRONG&gt; matching firms from control firm file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe this does what you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
set _V;
retain N 0 CUSIP_MATCHING1-CUSIP_MATCHING12;
by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;
N=N*^first.WBAHR1HAT_RANK+1;
if N= 1 then CUSIP_MATCHING1  = CUSIPB;
if N= 2 then CUSIP_MATCHING2  = CUSIPB;
if N= 3 then CUSIP_MATCHING3  = CUSIPB;
if N= 4 then CUSIP_MATCHING4  = CUSIPB;
if N= 5 then CUSIP_MATCHING5  = CUSIPB;
if N= 6 then CUSIP_MATCHING6  = CUSIPB;
if N= 7 then CUSIP_MATCHING7  = CUSIPB;
if N= 8 then CUSIP_MATCHING8  = CUSIPB;
if N= 9 then CUSIP_MATCHING9  = CUSIPB;
if N=10 then CUSIP_MATCHING10 = CUSIPB;
if N=11 then CUSIP_MATCHING11 = CUSIPB;
if N=12 then CUSIP_MATCHING12 = CUSIPB;
if N=12 or last.WBAHR1HAT_RANK then output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please spend the requitred time crafting your question to ensure you have a clear and comprehensive example next time, to save everybody's time. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 06:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372379#M276016</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-07-01T06:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372387#M276017</link>
      <description>&lt;P&gt;Dear &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961" target="_self"&gt;&lt;SPAN class="login-bold"&gt;ChrisNZ&lt;/SPAN&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am sorry for the inconvenience. I tried to make my question comprehensively but somehow missed the point. I ran your code but got the following error. &lt;STRONG&gt;Also,&amp;nbsp;I need to&amp;nbsp;keep all the sample firms including those which have&amp;nbsp;no&amp;nbsp;matching firm.&amp;nbsp;&lt;/STRONG&gt;Please do not mind if I am asking too many questions &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ERROR 161-185: No matching DO/SELECT statement.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;242 data sample;&lt;BR /&gt;243 input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;244 cards;&lt;/P&gt;&lt;P&gt;NOTE: The data set WORK.SAMPLE has 3 observations and 4 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;248 ;&lt;BR /&gt;249&lt;BR /&gt;250 data control;&lt;BR /&gt;251 input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;252 cards;&lt;/P&gt;&lt;P&gt;NOTE: Invalid data for fyear in line 253 1-5.&lt;BR /&gt;NOTE: Invalid data for cusip in line 253 7-11.&lt;BR /&gt;NOTE: Invalid data for SIC2 in line 253 13-16.&lt;BR /&gt;NOTE: Invalid data for wBAHR1hat_rank in line 253 18-31.&lt;BR /&gt;RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9-&lt;BR /&gt;253 fyear cusip SIC2 wBAHR1hat_rank&lt;BR /&gt;fyear=. cusip=. SIC2=. wBAHR1hat_rank=. _ERROR_=1 _N_=1&lt;BR /&gt;NOTE: The data set WORK.CONTROL has 26 observations and 4 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;279 ;&lt;BR /&gt;280&lt;BR /&gt;281 proc sql;&lt;BR /&gt;282 create table _V as&lt;BR /&gt;283 select a.*&lt;BR /&gt;284 ,abs(a.WBAHR1HAT_RANK-b.WBAHR1HAT_RANK) as DIF&lt;BR /&gt;285 ,b.CUSIP as CUSIPB&lt;BR /&gt;286 from SAMPLE a&lt;BR /&gt;287 left join&lt;BR /&gt;288 CONTROL b&lt;BR /&gt;289 on a.FYEAR = b.FYEAR&lt;BR /&gt;290 and a.SIC2 = b.SIC2&lt;BR /&gt;291 and b.WBAHR1HAT_RANK &amp;gt;= a.WBAHR1HAT_RANK&lt;BR /&gt;292 order by 1,2,3,4,5 ;&lt;BR /&gt;NOTE: Invalid (or missing) arguments to the ABS function have caused the function to return a missing&lt;BR /&gt;value.&lt;BR /&gt;NOTE: Table WORK._V created, with 19 rows and 6 columns.&lt;/P&gt;&lt;P&gt;293 quit;&lt;BR /&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;real time 0.04 seconds&lt;BR /&gt;cpu time 0.03 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;294 proc sort data=_V; by FYEAR CUSIP SIC2 WBAHR1HAT_RANK; run;&lt;/P&gt;&lt;P&gt;NOTE: Input data set is already sorted, no sorting done.&lt;BR /&gt;NOTE: PROCEDURE SORT used (Total process time):&lt;BR /&gt;real time 0.00 seconds&lt;BR /&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;295 data test;&lt;BR /&gt;296 set _V;&lt;BR /&gt;297 retain N 0 CUSIP_MATCHING1-CUSIP_MATCHING12;&lt;BR /&gt;298 by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;&lt;BR /&gt;299 N=N*^first.WBAHR1HAT_RANK+1;&lt;BR /&gt;300 if N= 1 then CUSIP_MATCHING1 = CUSIPB;&lt;BR /&gt;301 if N= 2 then CUSIP_MATCHING2 = CUSIPB;&lt;BR /&gt;302 if N= 3 then CUSIP_MATCHING3 = CUSIPB;&lt;BR /&gt;303 if N= 4 then CUSIP_MATCHING4 = CUSIPB;&lt;BR /&gt;304 if N= 5 then CUSIP_MATCHING5 = CUSIPB;&lt;BR /&gt;305 if N= 6 then CUSIP_MATCHING6 = CUSIPB;&lt;BR /&gt;306 if N= 7 then CUSIP_MATCHING7 = CUSIPB;&lt;BR /&gt;307 if N= 8 then CUSIP_MATCHING8 = CUSIPB;&lt;BR /&gt;308 if N= 9 then CUSIP_MATCHING9 = CUSIPB;&lt;BR /&gt;309 if N=10 then CUSIP_MATCHING10 = CUSIPB;&lt;BR /&gt;310 if N=11 then CUSIP_MATCHING11 = CUSIPB;&lt;BR /&gt;311 if N=12 then CUSIP_MATCHING12 = CUSIPB;&lt;BR /&gt;312 if N=12 or last.WBAHR1HAT_RANK then output;&lt;BR /&gt;313 end;&lt;BR /&gt;---&lt;BR /&gt;161&lt;BR /&gt;ERROR 161-185: No matching DO/SELECT statement.&lt;/P&gt;&lt;P&gt;314 run;&lt;/P&gt;&lt;P&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;WARNING: The data set WORK.TEST may be incomplete. When this step was stopped there were 0&lt;BR /&gt;observations and 19 variables.&lt;BR /&gt;WARNING: Data set WORK.TEST was not replaced because this step was stopped.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 06:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372387#M276017</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T06:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372393#M276018</link>
      <description>&lt;P&gt;I just modified the code in the following way and the error is not showing up. I have added one extra sample firm in my sample file which has no matched sample . After I run the code, I did not get consistent &amp;nbsp;result. I have included the picture of my output file at the bottom of the post. As you can see, firm 859 in year 2011 should not have any matched firm but my output shows matched firms. &amp;nbsp;also firm 859 in year 2000 should not have any cusip_matching9 and&amp;nbsp;&lt;SPAN&gt;cusip_matching9.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;cards;&lt;BR /&gt;1990 123 12 12&lt;BR /&gt;2000 859 11 15&lt;BR /&gt;2001 859 11 15&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data control;&lt;BR /&gt;input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;cards;&lt;BR /&gt;1990 124 12 13&lt;BR /&gt;1990 125 12 14&lt;BR /&gt;1990 126 12 15&lt;BR /&gt;1990 127 12 16&lt;BR /&gt;1990 128 12 17&lt;BR /&gt;1990 129 12 18&lt;BR /&gt;1990 759 13 19&lt;BR /&gt;1990 760 15 20&lt;BR /&gt;1990 761 12 21&lt;BR /&gt;1990 762 12 22&lt;BR /&gt;1990 763 12 23&lt;BR /&gt;1990 764 12 24&lt;BR /&gt;2000 860 11 21&lt;BR /&gt;2000 861 11 12&lt;BR /&gt;2000 863 11 23&lt;BR /&gt;2000 864 11 13&lt;BR /&gt;2000 867 11 14&lt;BR /&gt;2000 868 11 16&lt;BR /&gt;2000 869 11 17&lt;BR /&gt;2000 870 12 18&lt;BR /&gt;2000 872 14 19&lt;BR /&gt;2000 873 11 20&lt;BR /&gt;2000 874 11 21&lt;BR /&gt;2000 876 11 22&lt;BR /&gt;2000 877 11 23&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table _V as&lt;BR /&gt;select a.*&lt;BR /&gt;,abs(a.WBAHR1HAT_RANK-b.WBAHR1HAT_RANK) as DIF&lt;BR /&gt;,b.CUSIP as CUSIPB&lt;BR /&gt;from SAMPLE a&lt;BR /&gt;left join&lt;BR /&gt;CONTROL b&lt;BR /&gt;on a.FYEAR = b.FYEAR&lt;BR /&gt;and a.SIC2 = b.SIC2&lt;BR /&gt;and b.WBAHR1HAT_RANK &amp;gt;= a.WBAHR1HAT_RANK&lt;BR /&gt;order by 1,2,3,4,5 ;&lt;BR /&gt;quit;&lt;BR /&gt;proc sort data=_V; by FYEAR CUSIP SIC2 WBAHR1HAT_RANK; run;&lt;BR /&gt;data test;&lt;BR /&gt;set _V;&lt;BR /&gt;retain N 0 CUSIP_MATCHING1-CUSIP_MATCHING12;&lt;BR /&gt;by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;&lt;BR /&gt;N=N*^first.WBAHR1HAT_RANK+1;&lt;BR /&gt;if N= 1 then CUSIP_MATCHING1 = CUSIPB;&lt;BR /&gt;if N= 2 then CUSIP_MATCHING2 = CUSIPB;&lt;BR /&gt;if N= 3 then CUSIP_MATCHING3 = CUSIPB;&lt;BR /&gt;if N= 4 then CUSIP_MATCHING4 = CUSIPB;&lt;BR /&gt;if N= 5 then CUSIP_MATCHING5 = CUSIPB;&lt;BR /&gt;if N= 6 then CUSIP_MATCHING6 = CUSIPB;&lt;BR /&gt;if N= 7 then CUSIP_MATCHING7 = CUSIPB;&lt;BR /&gt;if N= 8 then CUSIP_MATCHING8 = CUSIPB;&lt;BR /&gt;if N= 9 then CUSIP_MATCHING9 = CUSIPB;&lt;BR /&gt;if N=10 then CUSIP_MATCHING10 = CUSIPB;&lt;BR /&gt;if N=11 then CUSIP_MATCHING11 = CUSIPB;&lt;BR /&gt;if N=12 then CUSIP_MATCHING12 = CUSIPB;&lt;BR /&gt;if N=12 or last.WBAHR1HAT_RANK then do;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10061iCB9E2A40D37B45B2/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="1.PNG" title="1.PNG" /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10062iD92E60073778D6ED/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="2.PNG" title="2.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 07:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372393#M276018</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T07:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372399#M276019</link>
      <description>&lt;P&gt;Haha just to keep you on your toes...&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Good on you fixing the error!&lt;/P&gt;
&lt;P&gt;You could also just have removed the troublesome &lt;FONT face="courier new,courier"&gt;end&lt;/FONT&gt; statement.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 08:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372399#M276019</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-07-01T08:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372417#M276020</link>
      <description>&lt;P&gt;Dear ChrisNz,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;After I run the following code, I did not get consistent &amp;nbsp;result. I have included the picture of my output file at the bottom of the post. As you can see, firm 859 in year 2011 should not have any matched firm but my output shows matched firms. &amp;nbsp;also firm 859 in year 2000 should not have any cusip_matching9 and&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;cusip_matching9.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to keep all the sampling firms including those for which there is no matching firms. Your last code keeps all the sampling firms but gives inconsistent results.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have included the snapshot of the output file in this post.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data sample;&lt;BR /&gt;input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;cards;&lt;BR /&gt;1990 123 12 12&lt;BR /&gt;2000 859 11 15&lt;BR /&gt;2001 859 11 15&lt;BR /&gt;;&lt;BR /&gt;data control;&lt;BR /&gt;input fyear cusip SIC2 wBAHR1hat_rank;&lt;BR /&gt;cards;&lt;BR /&gt;1990 124 12 13&lt;BR /&gt;1990 125 12 14&lt;BR /&gt;1990 126 12 15&lt;BR /&gt;1990 127 12 16&lt;BR /&gt;1990 128 12 17&lt;BR /&gt;1990 129 12 18&lt;BR /&gt;1990 759 13 19&lt;BR /&gt;1990 760 15 20&lt;BR /&gt;1990 761 12 21&lt;BR /&gt;1990 762 12 22&lt;BR /&gt;1990 763 12 23&lt;BR /&gt;1990 764 12 24&lt;BR /&gt;2000 860 11 21&lt;BR /&gt;2000 861 11 12&lt;BR /&gt;2000 863 11 23&lt;BR /&gt;2000 864 11 13&lt;BR /&gt;2000 867 11 14&lt;BR /&gt;2000 868 11 16&lt;BR /&gt;2000 869 11 17&lt;BR /&gt;2000 870 12 18&lt;BR /&gt;2000 872 14 19&lt;BR /&gt;2000 873 11 20&lt;BR /&gt;2000 874 11 21&lt;BR /&gt;2000 876 11 22&lt;BR /&gt;2000 877 11 23&lt;BR /&gt;;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table _V as&lt;BR /&gt;select a.*&lt;BR /&gt;,abs(a.WBAHR1HAT_RANK-b.WBAHR1HAT_RANK) as DIF&lt;BR /&gt;,b.CUSIP as CUSIPB&lt;BR /&gt;from SAMPLE a&lt;BR /&gt;left join&lt;BR /&gt;CONTROL b&lt;BR /&gt;on a.FYEAR = b.FYEAR&lt;BR /&gt;and a.SIC2 = b.SIC2&lt;BR /&gt;and b.WBAHR1HAT_RANK &amp;gt;= a.WBAHR1HAT_RANK&lt;BR /&gt;order by 1,2,3,4,5 ;&lt;BR /&gt;quit;&lt;BR /&gt;proc sort data=_V; by FYEAR CUSIP SIC2 WBAHR1HAT_RANK; run;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set _V;&lt;BR /&gt;retain N 0 CUSIP_MATCHING1-CUSIP_MATCHING12;&lt;BR /&gt;by FYEAR CUSIP SIC2 WBAHR1HAT_RANK;&lt;BR /&gt;N=N*^first.WBAHR1HAT_RANK+1;&lt;BR /&gt;if N= 1 then CUSIP_MATCHING1 = CUSIPB;&lt;BR /&gt;if N= 2 then CUSIP_MATCHING2 = CUSIPB;&lt;BR /&gt;if N= 3 then CUSIP_MATCHING3 = CUSIPB;&lt;BR /&gt;if N= 4 then CUSIP_MATCHING4 = CUSIPB;&lt;BR /&gt;if N= 5 then CUSIP_MATCHING5 = CUSIPB;&lt;BR /&gt;if N= 6 then CUSIP_MATCHING6 = CUSIPB;&lt;BR /&gt;if N= 7 then CUSIP_MATCHING7 = CUSIPB;&lt;BR /&gt;if N= 8 then CUSIP_MATCHING8 = CUSIPB;&lt;BR /&gt;if N= 9 then CUSIP_MATCHING9 = CUSIPB;&lt;BR /&gt;if N=10 then CUSIP_MATCHING10 = CUSIPB;&lt;BR /&gt;if N=11 then CUSIP_MATCHING11 = CUSIPB;&lt;BR /&gt;if N=12 then CUSIP_MATCHING12 = CUSIPB;&lt;BR /&gt;if N=12 or last.WBAHR1HAT_RANK then output;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, your first code gives me consistent results but it removes the sampling firms that have no matching firms.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10063iC955FEA93E31612A/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="1.PNG" title="1.PNG" /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10064i4F9E622C33C0362A/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="2.PNG" title="2.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 14:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372417#M276020</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T14:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two tables by choosing the CLOSEST dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372432#M276021</link>
      <description>&lt;P&gt;Dear ChrisNz,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just successfully modified your code to retain all the sample companies. My problem is solved. Thank you for your help.&lt;/P&gt;</description>
      <pubDate>Sat, 01 Jul 2017 17:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-two-tables-by-choosing-the-CLOSEST-dates/m-p/372432#M276021</guid>
      <dc:creator>nazmul</dc:creator>
      <dc:date>2017-07-01T17:15:14Z</dc:date>
    </item>
  </channel>
</rss>

