<?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 PROC sql: how to join to nearest year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-sql-how-to-join-to-nearest-year/m-p/460180#M116955</link>
    <description>&lt;P&gt;I'm trying to left join table2 to table 1, but it needs to join by the closest match, instead of exact.&lt;/P&gt;
&lt;P&gt;For example,&lt;/P&gt;
&lt;P&gt;Table 1 has a column year with values 1990, 2000, 2010, 2020.&lt;/P&gt;
&lt;P&gt;Table 2 has column year with values 1991, 2002,2015,2030.&lt;/P&gt;
&lt;P&gt;1990 should join with 1991, 2000 to 2002, 2010 to 2015 and 2020 to 2030.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table 1 should match the years in table 2 that is closest and not higher than Table1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how would I do this?&lt;/P&gt;</description>
    <pubDate>Fri, 04 May 2018 21:02:52 GMT</pubDate>
    <dc:creator>mrdlau</dc:creator>
    <dc:date>2018-05-04T21:02:52Z</dc:date>
    <item>
      <title>PROC sql: how to join to nearest year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-sql-how-to-join-to-nearest-year/m-p/460180#M116955</link>
      <description>&lt;P&gt;I'm trying to left join table2 to table 1, but it needs to join by the closest match, instead of exact.&lt;/P&gt;
&lt;P&gt;For example,&lt;/P&gt;
&lt;P&gt;Table 1 has a column year with values 1990, 2000, 2010, 2020.&lt;/P&gt;
&lt;P&gt;Table 2 has column year with values 1991, 2002,2015,2030.&lt;/P&gt;
&lt;P&gt;1990 should join with 1991, 2000 to 2002, 2010 to 2015 and 2020 to 2030.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table 1 should match the years in table 2 that is closest and not higher than Table1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;how would I do this?&lt;/P&gt;</description>
      <pubDate>Fri, 04 May 2018 21:02:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-sql-how-to-join-to-nearest-year/m-p/460180#M116955</guid>
      <dc:creator>mrdlau</dc:creator>
      <dc:date>2018-05-04T21:02:52Z</dc:date>
    </item>
    <item>
      <title>Re: PROC sql: how to join to nearest year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-sql-how-to-join-to-nearest-year/m-p/460227#M116965</link>
      <description>&lt;P&gt;This might work for you:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
do y = 1990, 2000, 2010, 2020, 2025, 2040; output; end;
run;

data b;
do y = 1991, 2002, 2015, 2030; output; end;
run;

proc sql;
select 
    a.y, 
    b.y as closest_y
from
    a left join
    b on b.y &amp;gt;= a.y
group by a.y
having min(b.y) = b.y;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                       y  closest_y
                                -------------------
                                    1990       1991
                                    2000       2002
                                    2010       2015
                                    2020       2030
                                    2025       2030
                                    2040          .
&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 May 2018 03:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-sql-how-to-join-to-nearest-year/m-p/460227#M116965</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-05-05T03:32:18Z</dc:date>
    </item>
  </channel>
</rss>

