<?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: Merging data with dates between two records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335170#M75820</link>
    <description>&lt;P&gt;This will work, but you may need to tweak it a bit, depending on how what you mean&amp;nbsp;&lt;EM&gt;between&lt;/EM&gt; to signify - is it inclusive at both ends or not? Also if you are going to have multiple entries per&amp;nbsp;&lt;EM&gt;app_num&lt;/EM&gt; in your error table, it becomes more complicated to solve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
infile cards;
attrib app_num length=8;
attrib app_date informat=date7. format=date9.;
input app_num
      app_date;
cards;
1 01jan16
1 08jan16
1 13jan16
;
run;

data table2;
infile cards;
attrib app_num length=8;
attrib error_date informat=date7. format=date9.;
attrib error_code length=$ 3;
input app_num
      error_date
      error_code;
cards;
1 09jan16 xxx
;
run;

data want;
merge table1(in=in_mast)
      table2(in=in_error);
by app_num;
if in_mast;
attrib save_app_date format=date9.;
attrib save_error_code length=$ 3;
retain save_app_date save_error_date save_error_code;
if first.app_num 
   then do;
        save_error_date = error_date;
        save_error_code = error_code;
        call missing(error_date, error_code); /* This is why the error values are being saved */
        end;
   else if save_app_date &amp;lt; save_error_date &amp;lt; app_date then do;
           error_date = save_error_date;
           error_code = save_error_code;
           end;
save_app_date = app_date;
drop save:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Feb 2017 01:01:17 GMT</pubDate>
    <dc:creator>LaurieF</dc:creator>
    <dc:date>2017-02-23T01:01:17Z</dc:date>
    <item>
      <title>Merging data with dates between two records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335164#M75816</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a created a data set which includes a a date stamp app_date for every application processed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to join data from a different table&amp;nbsp;which also includes a date error_date, but I unly want to join the data if the error date is before the app_date but after any prior app_date's.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ive been racking my brain for a couple of hours and I cannot figure out the logic. Here is an example of what Im looking for:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table1:&amp;nbsp;&lt;/P&gt;&lt;P&gt;App_num &amp;nbsp; &amp;nbsp; &amp;nbsp;App_Date&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01jan16&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; 08jan16&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13jan16&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table2:&lt;/P&gt;&lt;P&gt;App_num &amp;nbsp; &amp;nbsp; Error_date &amp;nbsp; &amp;nbsp; Error_Code&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 09jan16 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xxx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;P&gt;App_num &amp;nbsp; &amp;nbsp; &amp;nbsp;App_Date&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; Error_date &amp;nbsp; &amp;nbsp; Error_Code&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01jan16&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; 08jan16&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13jan16 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 09jan16 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xxx&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas to help?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nukie&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 00:30:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335164#M75816</guid>
      <dc:creator>WestChan</dc:creator>
      <dc:date>2017-02-23T00:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data with dates between two records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335170#M75820</link>
      <description>&lt;P&gt;This will work, but you may need to tweak it a bit, depending on how what you mean&amp;nbsp;&lt;EM&gt;between&lt;/EM&gt; to signify - is it inclusive at both ends or not? Also if you are going to have multiple entries per&amp;nbsp;&lt;EM&gt;app_num&lt;/EM&gt; in your error table, it becomes more complicated to solve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
infile cards;
attrib app_num length=8;
attrib app_date informat=date7. format=date9.;
input app_num
      app_date;
cards;
1 01jan16
1 08jan16
1 13jan16
;
run;

data table2;
infile cards;
attrib app_num length=8;
attrib error_date informat=date7. format=date9.;
attrib error_code length=$ 3;
input app_num
      error_date
      error_code;
cards;
1 09jan16 xxx
;
run;

data want;
merge table1(in=in_mast)
      table2(in=in_error);
by app_num;
if in_mast;
attrib save_app_date format=date9.;
attrib save_error_code length=$ 3;
retain save_app_date save_error_date save_error_code;
if first.app_num 
   then do;
        save_error_date = error_date;
        save_error_code = error_code;
        call missing(error_date, error_code); /* This is why the error values are being saved */
        end;
   else if save_app_date &amp;lt; save_error_date &amp;lt; app_date then do;
           error_date = save_error_date;
           error_code = save_error_code;
           end;
save_app_date = app_date;
drop save:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Feb 2017 01:01:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335170#M75820</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-23T01:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data with dates between two records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335171#M75821</link>
      <description>&lt;P&gt;Sorry - ignore that solution. It's rubbish. As I was walking&amp;nbsp;@schnauzerrosie I rewrote it in my head. By turning table1 into a kind-of &amp;nbsp;slowing-changing dimension, you can use SQL to create the output you want, with far fewer data contingencies. The only problem here would be if there were more than one error record within a date range.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This also, if your tables are really large, allows you to put integrity constraints on the tables, to make sure you're coping with duplicates. Note that I've created a low date for&amp;nbsp;&lt;EM&gt;app_date_previous&lt;/EM&gt; for the first instance per&amp;nbsp;&lt;EM&gt;app_num&lt;/EM&gt; - integrity constraints hate null values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
infile cards;
attrib app_num length=8;
attrib app_date length=4 informat=date7. format=date9.;
input app_num
      app_date;
cards;
1 01jan16
1 08jan16
1 13jan16
;
run;

data table2;
infile cards;
attrib app_num length=8;
attrib error_date length=4 informat=date7. format=date9.;
attrib error_code length=$ 3;
input app_num
      error_date
      error_code;
cards;
1 09jan16 xxx
;
run;

data table1_scd;
set table1;
retain app_date_previous;
attrib app_date_previous length=4 format=date9.;
by app_num;
if first.app_num then
   app_date_previous = '1jan1900'd;
output;
app_date_previous = app_date;
run;

proc sql;
create table want as
   select table1.app_num,
          table1.app_date,
          table2.error_date,
          table2.error_code
     from table1_scd as table1
     left join table2
       on table1.app_num = table2.app_num
      and table2.error_date between table1.app_date_previous and table1.app_date
    order by table1.app_num,
             table1.app_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Feb 2017 02:07:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335171#M75821</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-23T02:07:26Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data with dates between two records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335179#M75822</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
infile cards;
attrib app_num length=8;
attrib app_date informat=date7. format=date9.;
input app_num
      app_date;
cards;
1 01jan16
1 08jan16
1 13jan16
;
run;

data table2;
infile cards;
attrib app_num length=8;
attrib error_date informat=date7. format=date9.;
attrib error_code length=$ 3;
input app_num
      error_date
      error_code;
cards;
1 09jan16 xxx
;
run;
data want;
 set table1(in=ina) table2(in=inb rename=(error_date=app_date error_code=code));
 by app_num app_date;
 lag_date=lag(app_date);
 lag_code=lag(code);
 if lag(inb)=1 then do;error_date=lag_date;error_code=lag_code;end;
 if ina;
 format error_date date9.;
 drop lag_: code;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Feb 2017 02:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335179#M75822</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-02-23T02:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data with dates between two records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335185#M75827</link>
      <description>&lt;P&gt;The PDV (program data vector) is your friend.&amp;nbsp; In particular, you can take&amp;nbsp; advantage of the fact that variables from SET statements are automatically retained until the next SET that retrieves the same vars is encountered.&amp;nbsp; You want to "retain" vars from table 2 for the&amp;nbsp; subsequent table1 observation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=date);
  set table1 (in=in1 keep=app_date  rename=(app_date=date))
      table2 (in=in2 keep=error_date rename=(error_date=date));
  by date;

  if in1=1 then set table1;
  if in2=1 then set table2;
  if in1;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first SET statement, keeping only the BY variable, is just a means of controlling the sequence of data retrieved by the 2nd and 3rd SET statements.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted addition: Just realized I didn't demonstrate how to incorporate this for by groups (e.g. APP_NUM):&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=date);
  set table1 (in=in1 keep=app_num app_date   rename=(app_date=date))
      table2 (in=in2 keep=app_num error_date rename=(error_date=date));
  by app_num date;

  if in1=1 then set table1;
  if in2=1 then set table2;
  if in1 then output;
  if last.app_num then call missing(of _all_);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This allows the table2 data to persist for ALL subseqent table 1 observations, until either a new table2 record or end-of-by-group.&amp;nbsp; If you don't want table2 to persist beyond a single following table1 record, then change&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if last.app_num then call missing(of _all_);&lt;BR /&gt;to&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if in1 then call missing(of _all_);&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Thu, 23 Feb 2017 03:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335185#M75827</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-23T03:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data with dates between two records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335199#M75836</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17429"&gt;@LaurieF&lt;/a&gt; Perfect! This was the code was looking for.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 04:22:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-with-dates-between-two-records/m-p/335199#M75836</guid>
      <dc:creator>WestChan</dc:creator>
      <dc:date>2017-02-23T04:22:28Z</dc:date>
    </item>
  </channel>
</rss>

