<?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: Merge dataset with dates, but only keep most recent date from 2nd dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452362#M283897</link>
    <description>&lt;P&gt;SQL is by far the best approach i think however hash is fun&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data current;
infile cards;
input id visit_date;
attrib visit_date length=4 informat=date9. format=ddmmyy10.;
cards;
1 01nov14 
1 11nov14
2 22nov15 
2 24nov15
3 01nov16 
4 01nov16
;
run;

data previous;
infile cards;
input id last_date;
attrib last_date length=4 informat=date9. format=ddmmyy10.;
cards;
1 01jan14 
1 11feb14
1 02jan15
1 01nov14
2 01nov15 
2 22nov15
2 23nov15
3 01nov16 
4 12nov16
;
run;
data want;
if _n_=1 then do;
if 0 then do; set current;set previous;end;
  declare hash h(dataset: 'previous',multidata:'y');
  h.definekey('id');
  h.definedata('last_date');
  h.definedone();
 end;
 set current;
 rc=h.find();
 do while(rc = 0);
  if visit_date&amp;gt;last_date then  _t=max( _t, last_date);
  rc = h.find_next();
 end;
 last_date=_t;
 drop rc _:;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 08 Apr 2018 21:03:48 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-04-08T21:03:48Z</dc:date>
    <item>
      <title>Merge dataset with dates, but only keep most recent date from 2nd dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452341#M283894</link>
      <description>&lt;P&gt;I have two datasets, &lt;STRONG&gt;current&lt;/STRONG&gt; -- which is my main file containing the visit_dates I want to match on, and &lt;STRONG&gt;previous&lt;/STRONG&gt; which contains other visit_dates (not necessarily always prior to the &lt;STRONG&gt;current&lt;/STRONG&gt; dataset).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create a dataset that only keeps the most recent &lt;STRONG&gt;last_date&lt;/STRONG&gt; prior to the&amp;nbsp;&lt;STRONG&gt;visit_date&lt;/STRONG&gt; in the&amp;nbsp;&lt;STRONG&gt;current &lt;/STRONG&gt;dataset. If there are no prior visits, then last_date should be empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the best way to approach this problem?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data current;
infile cards;
input id visit_date;
attrib visit_date length=4 informat=date9. format=ddmmyy10.;
cards;
1 01nov14 
1 11nov14
2 22nov15 
2 24nov15
3 01nov16 
4 01nov16
;
run;

data previous;
infile cards;
input id last_date;
attrib last_date length=4 informat=date9. format=ddmmyy10.;
cards;
1 01jan14 
1 11feb14
1 02jan15
1 01nov14
2 01nov15 
2 22nov15
2 23nov15
3 01nov16 
4 12nov16
;
run;

data want;
infile cards;
input id visit_date last_date;
attrib visit_date last_date length=4 informat=date9. format=ddmmyy10.;
cards;
1 01nov14 11feb14
1 11nov14 01nov14
2 22nov15 01nov15
2 24nov15 23nov15
3 01nov16 .
4 01nov16 .
;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 17:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452341#M283894</guid>
      <dc:creator>Fettah</dc:creator>
      <dc:date>2018-04-08T17:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merge dataset with dates, but only keep most recent date from 2nd dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452347#M283895</link>
      <description>&lt;P&gt;Hi Fettah&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's one way to do it: first I did a Cartesian Join (work.step1) via PROC SQL, then use the SAS data step by group processing to get the records you want. The results dataset has a new variable result_date which is the variable you want (just use a drop and rename, to drop last_date and rename result_date to last_date), I left that out to make it clearer to see how it is working.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Andy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;create table work.step1 as&lt;BR /&gt;select&lt;BR /&gt;current.*,&lt;BR /&gt;previous.*&lt;BR /&gt;from&lt;BR /&gt;work.current as current,&lt;BR /&gt;work.previous as previous&lt;BR /&gt;where&lt;BR /&gt;previous.id=current.id&lt;BR /&gt;order by&lt;BR /&gt;current.id,&lt;BR /&gt;current.visit_date,&lt;BR /&gt;previous.last_date&lt;BR /&gt;;&lt;BR /&gt;quit ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data results ;&lt;BR /&gt;retain result_date ;&lt;BR /&gt;attrib result_date length=4 informat=date9. format=ddmmyy10.;&lt;BR /&gt;set work.step1 ;&lt;BR /&gt;by id visit_date ;&lt;BR /&gt;if first.visit_date then&lt;BR /&gt;result_date=. ;&lt;BR /&gt;if visit_date &amp;gt; last_date then&lt;BR /&gt;result_date=last_date ;&lt;BR /&gt;if last.visit_date then&lt;BR /&gt;output ;&lt;BR /&gt;run ;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Apr 2018 17:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452347#M283895</guid>
      <dc:creator>AndyMayes</dc:creator>
      <dc:date>2018-04-08T17:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: Merge dataset with dates, but only keep most recent date from 2nd dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452349#M283896</link>
      <description>&lt;P&gt;something like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select  a.id, visit_date format=date7., max(last_date) as last_date format=date7.
from current a
left join
previous b
on a.id =b.id
and visit_date &amp;gt; last_date
group by 1,2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Apr 2018 18:31:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452349#M283896</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-04-08T18:31:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merge dataset with dates, but only keep most recent date from 2nd dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452362#M283897</link>
      <description>&lt;P&gt;SQL is by far the best approach i think however hash is fun&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data current;
infile cards;
input id visit_date;
attrib visit_date length=4 informat=date9. format=ddmmyy10.;
cards;
1 01nov14 
1 11nov14
2 22nov15 
2 24nov15
3 01nov16 
4 01nov16
;
run;

data previous;
infile cards;
input id last_date;
attrib last_date length=4 informat=date9. format=ddmmyy10.;
cards;
1 01jan14 
1 11feb14
1 02jan15
1 01nov14
2 01nov15 
2 22nov15
2 23nov15
3 01nov16 
4 12nov16
;
run;
data want;
if _n_=1 then do;
if 0 then do; set current;set previous;end;
  declare hash h(dataset: 'previous',multidata:'y');
  h.definekey('id');
  h.definedata('last_date');
  h.definedone();
 end;
 set current;
 rc=h.find();
 do while(rc = 0);
  if visit_date&amp;gt;last_date then  _t=max( _t, last_date);
  rc = h.find_next();
 end;
 last_date=_t;
 drop rc _:;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Apr 2018 21:03:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-dataset-with-dates-but-only-keep-most-recent-date-from-2nd/m-p/452362#M283897</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-08T21:03:48Z</dc:date>
    </item>
  </channel>
</rss>

