<?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: Closest date to another and the difference in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Closest-date-to-another-and-the-difference/m-p/387358#M92880</link>
    <description>&lt;P&gt;Sounds like you need a catersian join, i.e. merge all the expiry dates to each transaction date (only pseudocode as not typing in that test data nor guessing structure):&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=x  (keep=id expiry) nodupkey;
  by id expiry;
run;

proc sql;
  create table inter as 
  select A.ID,
         A.TRAN,
         B.EXPIRY,
         B.EXPIRY - A.TRAN as DIFF
   from  HAVE A
   left join X B
   on    A.ID=B.ID
   order by ID,
            TRAN,
            DIFF;
quit;

data inter;
  set inter;
  by id tran;
  if first.tran then output;
run;

data want;
  merge have inter (rename=(expiry=nearest));
  by id tran;
run;
&lt;/PRE&gt;</description>
    <pubDate>Fri, 11 Aug 2017 14:44:02 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-08-11T14:44:02Z</dc:date>
    <item>
      <title>Closest date to another and the difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Closest-date-to-another-and-the-difference/m-p/387341#M92873</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that looks like the below...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Tran Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Enrol Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Expiry Date&lt;/P&gt;&lt;P&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04JAN2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31DEC2015&amp;nbsp;&lt;BR /&gt;12345 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03DEC2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01DEC2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31NOV2017&amp;nbsp;&lt;/P&gt;&lt;P&gt;12345&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04APR2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01APR2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31MAR2018&amp;nbsp;&lt;BR /&gt;12345&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03JAN2018 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN2018 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31DEC2018&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, I have multiple lines of record for each ID for every time they renew a membership. I'm trying to work out the difference bewteen the transaction date and the closest expiry date. This will then tell me whether the renew before or after the expiry date and how many days difference it is.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2017 14:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Closest-date-to-another-and-the-difference/m-p/387341#M92873</guid>
      <dc:creator>CamRutherford</dc:creator>
      <dc:date>2017-08-11T14:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Closest date to another and the difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Closest-date-to-another-and-the-difference/m-p/387358#M92880</link>
      <description>&lt;P&gt;Sounds like you need a catersian join, i.e. merge all the expiry dates to each transaction date (only pseudocode as not typing in that test data nor guessing structure):&lt;/P&gt;
&lt;PRE&gt;proc sort data=have out=x  (keep=id expiry) nodupkey;
  by id expiry;
run;

proc sql;
  create table inter as 
  select A.ID,
         A.TRAN,
         B.EXPIRY,
         B.EXPIRY - A.TRAN as DIFF
   from  HAVE A
   left join X B
   on    A.ID=B.ID
   order by ID,
            TRAN,
            DIFF;
quit;

data inter;
  set inter;
  by id tran;
  if first.tran then output;
run;

data want;
  merge have inter (rename=(expiry=nearest));
  by id tran;
run;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Aug 2017 14:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Closest-date-to-another-and-the-difference/m-p/387358#M92880</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-08-11T14:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: Closest date to another and the difference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Closest-date-to-another-and-the-difference/m-p/387480#M92910</link>
      <description>&lt;P&gt;From your description, three cases are possible:&lt;/P&gt;
&lt;P&gt;1- Tran before Enrol&lt;/P&gt;
&lt;P&gt;2- Tran during Enrol&lt;/P&gt;
&lt;P&gt;3- Tran&amp;nbsp;after Expiry&lt;/P&gt;
&lt;P&gt;This should handle all:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines missover;
input ID (TranDate EnrolDate ExpiryDate) (:date9.);
format TranDate EnrolDate ExpiryDate yymmdd10.;
datalines;
12345         30DEC2014              01JAN2015          31DEC2015 Tran before first Enrol
12345         04JAN2015              01JAN2015          31DEC2015 
12345         03DEC2016              01DEC2016        30NOV2017 
12345         04FEB2017              01APR2017         31MAR2018 
12345         03JAN2018              01JAN2018          31DEC2018 
12345         03JAN2019              01JAN2018          31DEC2018 Tran when expired
;

proc sql;
select unique
    a.ID,
    a.TranDate,
    b.EnrolDate,
    b.expiryDate,
    intck('DAY', a.TranDate, b.Expirydate) as DaysLeftBeforeExpiry
from 
    have as a left join
    have as b on a.ID=b.ID and b.enrolDate &amp;lt;= a.tranDate
group by a.ID, a.TranDate
having intck('DAY', a.TranDate, b.Expirydate) = max(intck('DAY', a.TranDate, b.Expirydate));
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Aug 2017 19:44:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Closest-date-to-another-and-the-difference/m-p/387480#M92910</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-08-11T19:44:14Z</dc:date>
    </item>
  </channel>
</rss>

