<?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: How to chose closer date ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847384#M335022</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname x v9 'c:\temp';

proc sql;
create table temp as
select a.*,b.date2,b.date2-a.date as diff
 from x.data1 as a left join x.data2 as b
  on a.no_k=b.no_k 
   order by a.no_k,a.date,diff
 ;
 quit;
 data temp2;
  set temp;
  by no_k date;
  if first.date then vname='Date2_closer_min';
  if last.date  then vname='Date2_closer_max';
if first.date or last.date;
run;
proc transpose data=temp2 out=want;
by no_k date;
var date2;
id vname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 02 Dec 2022 03:27:01 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-12-02T03:27:01Z</dc:date>
    <item>
      <title>How to chose closer date ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847373#M335015</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have 2 datasets, I would like to do a proc sql left join between data 1 et data 2 to display the most closest date for DATE 1 column from DATE 2 column. Do you know, please,&amp;nbsp; some sas function for closer date ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added the data.&lt;/P&gt;
&lt;P&gt;Thank you !&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASdevAnneMarie_0-1669933896733.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77932i2443FFD1329BAEC3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASdevAnneMarie_0-1669933896733.png" alt="SASdevAnneMarie_0-1669933896733.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASdevAnneMarie_1-1669933965456.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77933i54BEB5307AB68314/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASdevAnneMarie_1-1669933965456.png" alt="SASdevAnneMarie_1-1669933965456.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASdevAnneMarie_2-1669933999596.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77934i7F3F940CB40FB2ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASdevAnneMarie_2-1669933999596.png" alt="SASdevAnneMarie_2-1669933999596.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Dec 2022 22:37:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847373#M335015</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2022-12-01T22:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to chose closer date ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847384#M335022</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname x v9 'c:\temp';

proc sql;
create table temp as
select a.*,b.date2,b.date2-a.date as diff
 from x.data1 as a left join x.data2 as b
  on a.no_k=b.no_k 
   order by a.no_k,a.date,diff
 ;
 quit;
 data temp2;
  set temp;
  by no_k date;
  if first.date then vname='Date2_closer_min';
  if last.date  then vname='Date2_closer_max';
if first.date or last.date;
run;
proc transpose data=temp2 out=want;
by no_k date;
var date2;
id vname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Dec 2022 03:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847384#M335022</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-12-02T03:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to chose closer date ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847427#M335034</link>
      <description>&lt;P&gt;Thank you Ksharp,&lt;BR /&gt;&lt;BR /&gt;The code is OK, but I need the closest day. &lt;BR /&gt;Do you know, please, how to add in sql condition that I need hold only 2 rows : when diff is min for positive values of diff and when diff is min of negative values of diff ? &lt;BR /&gt;Thank you very much !&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 10:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847427#M335034</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2022-12-02T10:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to chose closer date ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847430#M335036</link>
      <description>&lt;P&gt;What do you except as result actually?&lt;/P&gt;
&lt;P&gt;Have a look at the function intck, but you can just use subtraction (along with abs function) to get the difference of two dates.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 11:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847430#M335036</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-12-02T11:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to chose closer date ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847433#M335037</link>
      <description>&lt;P&gt;I don't know how to select, in proc sql, the min for positive values of diff (to have the 25/11/2022) and the max for negatives diff (to have the 11/11/2022).&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASdevAnneMarie_0-1669983750339.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77958iC9EA6C58E9CC0EA5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASdevAnneMarie_0-1669983750339.png" alt="SASdevAnneMarie_0-1669983750339.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thank you for your help !&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 12:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847433#M335037</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2022-12-02T12:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to chose closer date ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847441#M335040</link>
      <description>&lt;P&gt;Sorry, i don't get it. In the data you have posted date1 is always smaller than date2.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 12:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847441#M335040</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-12-02T12:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to chose closer date ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847534#M335079</link>
      <description>&lt;P&gt;You need ABS() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname x v9 'c:\temp';

proc sql;
create table want as
select a.*,b.date2,b.date2-a.date as diff
 from x.data1 as a left join x.data2 as b
  on a.no_k=b.no_k 
   where calculated diff&amp;gt;=0
    group by a.no_k,a.date
     having abs(calculated diff)=min(abs(calculated diff))
union 
select a.*,b.date2,b.date2-a.date as diff
 from x.data1 as a left join x.data2 as b
  on a.no_k=b.no_k 
   where calculated diff&amp;lt;0
    group by a.no_k,a.date
     having abs(calculated diff)=min(abs(calculated diff))
 ;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 03 Dec 2022 03:00:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-chose-closer-date/m-p/847534#M335079</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-12-03T03:00:13Z</dc:date>
    </item>
  </channel>
</rss>

