<?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: SAS-Create Unique Encounters based on Minimal Difference in Dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287286#M269932</link>
    <description>&lt;P&gt;Some of the matching rules aren't 100% clear, but I'll go with a reasonable approach:&amp;nbsp; create one record for each existing t2 record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After both data sets are sorted by ID and TIME1/TIME2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;set t1 (in=in1 rename=(time1=time2)) &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t2 (in=in2);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;by id time2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if first.id then time1=.;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;retain time1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if in1 then time1=time2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if in2 then output;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested, but should be OK.&amp;nbsp; It takes each t2 record and matches it with the most recent t1 record.&amp;nbsp; The same t1 record could potentially match multiple t2 records.&amp;nbsp; And there might not be a match if the t2 record is earliest.&amp;nbsp; In any case, there will be one record in the output for each original t2 record.&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jul 2016 17:47:49 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-07-26T17:47:49Z</dc:date>
    <item>
      <title>SAS-Create Unique Encounters based on Minimal Difference in Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287261#M269929</link>
      <description>&lt;P&gt;I am trying to select unique encounters based on minimal days between encounters. I would like to choose&amp;nbsp;the closest date from table 1 to its respective closest date in table 2 (if exists) to make a bookend date range, if you will. &amp;nbsp;There may me more ID-Dates in table 1 than table 2 and vice versa. For for instance my data from t1 would be something like:&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp;time1&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp; 01/09/2015&lt;BR /&gt; A &amp;nbsp; &amp;nbsp; 02/16/2015&lt;BR /&gt; B &amp;nbsp; &amp;nbsp; 03/03/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 04/01/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 01/20/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 03/15/2015&lt;/P&gt;
&lt;P&gt;t2 similarly:&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; time2&lt;BR /&gt; A &amp;nbsp; &amp;nbsp; 01/29/2015&lt;BR /&gt; A &amp;nbsp; &amp;nbsp; 02/19/2015&lt;BR /&gt; B &amp;nbsp; &amp;nbsp; 03/06/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 01/27/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 03/18/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 04/04/2015&lt;/P&gt;
&lt;P&gt;What I don't want when joining on ID is a combination of all unique dates, rather combinations where the days between are minimum eg.&lt;/P&gt;
&lt;P&gt;desired_output:&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp;date_time1 &amp;nbsp; &amp;nbsp;date_time2&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp; 01/09/2015 &amp;nbsp; &amp;nbsp; 01/29/2015&lt;BR /&gt; A &amp;nbsp; &amp;nbsp; 02/16/2015 &amp;nbsp; &amp;nbsp; 02/19/2015&lt;BR /&gt; B &amp;nbsp; &amp;nbsp; 03/03/2015 &amp;nbsp; &amp;nbsp; 03/06/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 01/20/2015 &amp;nbsp; &amp;nbsp; 01/27/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 03/15/2015 &amp;nbsp; &amp;nbsp; 03/18/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 04/01/2015 &amp;nbsp; &amp;nbsp; 04/04/2015&lt;/P&gt;
&lt;P&gt;I am not sure how to do this, I have tried selecting the max and min but that only gives something like, which is not what I want:&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp;date_time1 &amp;nbsp; &amp;nbsp;date_time2&lt;BR /&gt; A &amp;nbsp; &amp;nbsp; 01/09/2015 &amp;nbsp; &amp;nbsp; 02/19/2015&lt;/P&gt;
&lt;P&gt;&amp;nbsp;B &amp;nbsp; &amp;nbsp;03/03/2015 &amp;nbsp; &amp;nbsp; 03/06/2015&lt;BR /&gt; C &amp;nbsp; &amp;nbsp; 01/20/2015 &amp;nbsp; &amp;nbsp; 04/04/2015&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas/suggestions for how to do this would be greatly appreciated! Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2016 13:30:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287261#M269929</guid>
      <dc:creator>bradbelf262</dc:creator>
      <dc:date>2016-07-27T13:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS-Create Unique Encounters based on Minimal Difference in Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287278#M269930</link>
      <description>&lt;P&gt;Just to get this question out of the way first ... are your variables actually dates or date-times?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is more than one way to combine data sets.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Take the earliest t1 record, match it with the earliest t2 record, etc.&lt;/LI&gt;
&lt;LI&gt;Re-use some records.&amp;nbsp; For each t2 record, take the closest t1 record that is earlier.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Is it possible that t1 would contain 3 records for an ID, while t2 would contain only 2 records for the same ID?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will take just a little more definition to the problem.&amp;nbsp; The programming won't be simple, but won't be terribly difficult either.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 17:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287278#M269930</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-26T17:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: SAS-Create Unique Encounters based on Minimal Difference in Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287282#M269931</link>
      <description>&lt;P&gt;Thank you for the response. &amp;nbsp;The variables are actual dates (mmddyy10. format to be exact). &amp;nbsp;It is possible that t1 would have more records than t2, also possible (although much more rare) that there would more dates per ID in t2 than t1. &amp;nbsp;It also is possible that IDs in t1 have no matching ID in t2 or there are dates-id's in t2 with no match in t1. &amp;nbsp;I hope this helps any future viewers of this and if I am unclear please let me know.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 17:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287282#M269931</guid>
      <dc:creator>bradbelf262</dc:creator>
      <dc:date>2016-07-26T17:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: SAS-Create Unique Encounters based on Minimal Difference in Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287286#M269932</link>
      <description>&lt;P&gt;Some of the matching rules aren't 100% clear, but I'll go with a reasonable approach:&amp;nbsp; create one record for each existing t2 record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After both data sets are sorted by ID and TIME1/TIME2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;set t1 (in=in1 rename=(time1=time2)) &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t2 (in=in2);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;by id time2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if first.id then time1=.;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;retain time1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if in1 then time1=time2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;if in2 then output;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested, but should be OK.&amp;nbsp; It takes each t2 record and matches it with the most recent t1 record.&amp;nbsp; The same t1 record could potentially match multiple t2 records.&amp;nbsp; And there might not be a match if the t2 record is earliest.&amp;nbsp; In any case, there will be one record in the output for each original t2 record.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2016 17:47:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287286#M269932</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-26T17:47:49Z</dc:date>
    </item>
    <item>
      <title>Re: SAS-Create Unique Encounters based on Minimal Difference in Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287325#M269933</link>
      <description>&lt;P&gt;Assuming you want to minimize the absolute time difference between time1 and time2 :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table t3 as
select t1.ID, time1 format=yymmdd10., time2 format=yymmdd10.
from t1 inner join t2 on t1.ID=t2.ID
group by t1.time1
having abs(t2.time2-t1.time1) = min(abs(t2.time2-t1.time1));
select * from t3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jul 2016 19:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287325#M269933</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-07-26T19:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS-Create Unique Encounters based on Minimal Difference in Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287394#M269934</link>
      <description>&lt;PRE&gt;

data t1;
input ID  $ time1 : mmddyy10.;
format time1  mmddyy10.;
cards;
A     01/09/2015
A     02/16/2015
;
run;
data t2;
input ID  $ time2 : mmddyy10.;
format time2 mmddyy10.;
cards;
A      01/29/2015
A      02/19/2015
;
run;
proc sql;
select b.*,a.time1
 from t1 as a right join t2 as b
  on a.id=b.id and b.time2 &amp;gt; a.time1
   group by b.id,b.time2
    having b.time2-a.time1=min(b.time2-a.time1);
quit;


&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jul 2016 02:40:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Create-Unique-Encounters-based-on-Minimal-Difference-in/m-p/287394#M269934</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-27T02:40:52Z</dc:date>
    </item>
  </channel>
</rss>

