<?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 SQL left join , how to how to use min or max in the key between in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-left-join-how-to-how-to-use-min-or-max-in-the-key-between/m-p/685220#M207789</link>
    <description>&lt;P&gt;Dear all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any idea about how to resolve the following case :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in table a i have the variable "Year" taking value from 2016 to 2022&amp;nbsp; and in table b Year variable takes value from value 2019 to&amp;nbsp; 2020.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in that case , i would like to apply the following resuls:&lt;/P&gt;&lt;P&gt;a.2016=b.2019 ( in table a i would like to take value corresponding to min of year in table b which is 2019 , same case from 2017 to 2019 )&lt;/P&gt;&lt;P&gt;a.2020=b.2020 ( 2020 value from table a exist on table b )&lt;/P&gt;&lt;P&gt;a.2021=b.2020 ( from 2020 to 2021 from table a takes max value from table b which is 2020 )&lt;/P&gt;&lt;P&gt;i've tried the following code , but it doesn't work as expected :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example that case correspond to the first case&amp;nbsp;a.2016=b.2019 ( in table a i would like to take value corresponding to min of year in table b which is 2019 , same case from 2017 to 2019 )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.*,b.var_want&lt;BR /&gt;from tableA as a&lt;BR /&gt;left join tableB as b&lt;BR /&gt;on a.YEAR=max(2019,min(b.YEAR,2020))&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it gives an empty value for var_want and doesn't take value of 2019 in table b when year=2016 in table a&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for your help&lt;/P&gt;</description>
    <pubDate>Sat, 19 Sep 2020 20:28:08 GMT</pubDate>
    <dc:creator>bebess</dc:creator>
    <dc:date>2020-09-19T20:28:08Z</dc:date>
    <item>
      <title>SQL left join , how to how to use min or max in the key between</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-left-join-how-to-how-to-use-min-or-max-in-the-key-between/m-p/685220#M207789</link>
      <description>&lt;P&gt;Dear all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;any idea about how to resolve the following case :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in table a i have the variable "Year" taking value from 2016 to 2022&amp;nbsp; and in table b Year variable takes value from value 2019 to&amp;nbsp; 2020.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in that case , i would like to apply the following resuls:&lt;/P&gt;&lt;P&gt;a.2016=b.2019 ( in table a i would like to take value corresponding to min of year in table b which is 2019 , same case from 2017 to 2019 )&lt;/P&gt;&lt;P&gt;a.2020=b.2020 ( 2020 value from table a exist on table b )&lt;/P&gt;&lt;P&gt;a.2021=b.2020 ( from 2020 to 2021 from table a takes max value from table b which is 2020 )&lt;/P&gt;&lt;P&gt;i've tried the following code , but it doesn't work as expected :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example that case correspond to the first case&amp;nbsp;a.2016=b.2019 ( in table a i would like to take value corresponding to min of year in table b which is 2019 , same case from 2017 to 2019 )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.*,b.var_want&lt;BR /&gt;from tableA as a&lt;BR /&gt;left join tableB as b&lt;BR /&gt;on a.YEAR=max(2019,min(b.YEAR,2020))&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it gives an empty value for var_want and doesn't take value of 2019 in table b when year=2016 in table a&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for your help&lt;/P&gt;</description>
      <pubDate>Sat, 19 Sep 2020 20:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-left-join-how-to-how-to-use-min-or-max-in-the-key-between/m-p/685220#M207789</guid>
      <dc:creator>bebess</dc:creator>
      <dc:date>2020-09-19T20:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: SQL left join , how to how to use min or max in the key between</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-left-join-how-to-how-to-use-min-or-max-in-the-key-between/m-p/685223#M207791</link>
      <description>&lt;P&gt;Should be the other way around:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select a.*, b.var_want&lt;BR /&gt;from tableA as a&lt;BR /&gt;left join tableB as b&lt;BR /&gt;on b.YEAR=max(2019, min(a.YEAR, 2020))&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Sep 2020 20:47:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-left-join-how-to-how-to-use-min-or-max-in-the-key-between/m-p/685223#M207791</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-09-19T20:47:24Z</dc:date>
    </item>
  </channel>
</rss>

