<?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: Joining two datasets on date that's true as of the date in the second dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232957#M42480</link>
    <description>&lt;P&gt;It's probably easier to reorder the records in DATASET2.&amp;nbsp; That way, you can take the first one that meets the conditions for comparing the dates:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=dataset2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by id descending date2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge dataset1 dataset2;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;if date1 &amp;gt;= date2 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; date1 = '01jan1800'd;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This assumes you will never have any dates that fall before January 1, 1800 (and assumes that I can still think straight after pondering this problem).&amp;nbsp; One of the keys is that DATE1 will be retained, and not re-read from DATASET1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
    <pubDate>Tue, 03 Nov 2015 20:11:49 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2015-11-03T20:11:49Z</dc:date>
    <item>
      <title>Joining two datasets on date that's true as of the date in the second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232949#M42472</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two datasets: Dataset 1 is at the person level and each person has a corresponding date. There are also a number of person-level categorical variables that I'd like to keep as is if possible. In Dataset 2 each person occurs multiple times and has multiple different dates.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset1:&lt;/P&gt;&lt;P&gt;ID1 &amp;nbsp; &amp;nbsp; Date1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var1 &amp;nbsp; &amp;nbsp; var2....etc.&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;10/31/2014 &amp;nbsp; &amp;nbsp; abc &amp;nbsp; &amp;nbsp; &amp;nbsp;def&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;9/7/2013 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;xyz &amp;nbsp; &amp;nbsp; &amp;nbsp; abc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset2:&lt;/P&gt;&lt;P&gt;ID2 &amp;nbsp; &amp;nbsp; &amp;nbsp;Date2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9/7/2012&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2/4/2013&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 10/1/2014&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9/7/2012&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; 10/2/2014&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to merge the datasets such that from Dataset 2, I only bring in one record for each person--the record that has the Date2&amp;nbsp;value that remains true as of that person's Date1 in Dataset1. So for example, ID=1 has a Date1 value of 10/31/2014. I would want the latest Date2 value for ID=1 that occurs before Date1--in this case, 10/1/2014.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2015 19:27:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232949#M42472</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2015-11-03T19:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two datasets on date that's true as of the date in the second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232953#M42476</link>
      <description>&lt;P&gt;Hi mate,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need simply join these tables where the dataset2 &amp;nbsp;remains only the last dates of each id you can simply do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=dataset1(rename=id1=id date1=date);
  by id date;
run;

proc sort data=dataset2(rename=id2=id date2=date);
  by id date;
run;

data dt1;&lt;BR /&gt;   set dataset1;&lt;BR /&gt;   by id date;&lt;BR /&gt;   if last.date then flg = 1;&lt;BR /&gt;   else flg = 0;&lt;BR /&gt;run;&lt;BR /&gt;data dt2;&lt;BR /&gt; set dataset2;&lt;BR /&gt; by id date;&lt;BR /&gt; if last.date then flg = 1;&lt;BR /&gt; else flg = 0;&lt;BR /&gt;run&lt;BR /&gt;&lt;BR /&gt;data merge;&lt;BR /&gt;   merge dt1(in=a)&lt;BR /&gt;         dt2(in=b);&lt;BR /&gt;   by id;&lt;BR /&gt;   if a;&lt;BR /&gt;   where flg = 1;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Maybe this helps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2015 19:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232953#M42476</guid>
      <dc:creator>DartRodrigo</dc:creator>
      <dc:date>2015-11-03T19:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two datasets on date that's true as of the date in the second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232955#M42478</link>
      <description>&lt;P&gt;Simple proc sql will do as well but you need to list all variables from dataset1 in group by statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
	create table ds3 as
	select a.*, max(b.date2) as date3 format=mmddyy10.
	from ds1 as a left join ds2 as b
	on a.id1=b.id2 and b.date2&amp;lt;=a.date1
	group by a.id1, a.date1, a.var1, a.var2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Nov 2015 20:04:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232955#M42478</guid>
      <dc:creator>ndp</dc:creator>
      <dc:date>2015-11-03T20:04:12Z</dc:date>
    </item>
    <item>
      <title>Re: Joining two datasets on date that's true as of the date in the second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232957#M42480</link>
      <description>&lt;P&gt;It's probably easier to reorder the records in DATASET2.&amp;nbsp; That way, you can take the first one that meets the conditions for comparing the dates:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=dataset2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by id descending date2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge dataset1 dataset2;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;if date1 &amp;gt;= date2 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; date1 = '01jan1800'd;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This assumes you will never have any dates that fall before January 1, 1800 (and assumes that I can still think straight after pondering this problem).&amp;nbsp; One of the keys is that DATE1 will be retained, and not re-read from DATASET1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Nov 2015 20:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Joining-two-datasets-on-date-that-s-true-as-of-the-date-in-the/m-p/232957#M42480</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-11-03T20:11:49Z</dc:date>
    </item>
  </channel>
</rss>

