<?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 Capture 1st value from second table based on closest date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Capture-1st-value-from-second-table-based-on-closest-date/m-p/271036#M269463</link>
    <description>&lt;P&gt;Hi-&lt;BR /&gt;I'm trying to query a second table (b) using account ID from first table (a). I want to only capture first observation from table b where start date from table (b) should correspond either to the same date from table (a), or be the first closest date after (as the data from "second" closest date no longer valid).&lt;BR /&gt;Example tables:&lt;BR /&gt;&lt;BR /&gt;Table a&lt;BR /&gt;&lt;BR /&gt;Account_ID | Shopping Date&lt;BR /&gt;___________________________&lt;BR /&gt;&amp;nbsp; 00001&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; | 03/20/2016&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Table b&lt;BR /&gt;&lt;BR /&gt;Key |Account_ID| Start Date | End Date | Score&lt;BR /&gt;____|____________________________________________&lt;BR /&gt;26 &amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 2/18/2016&amp;nbsp; | 3/19/2016 | 709&lt;BR /&gt;____|___________________________________________&lt;BR /&gt;27 &amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 3/19/2016&amp;nbsp; | 3/18/2016 | 715&lt;BR /&gt;____|___________________________________________&lt;BR /&gt;28&amp;nbsp;&amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 4/12/2016&amp;nbsp; | 5/11/2016 | 718&lt;BR /&gt;________________________________________________&lt;BR /&gt;29 &amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 5/12/2016&amp;nbsp; | 12/31/9999| 702&lt;BR /&gt;&lt;BR /&gt;So the result queried from table (b) should be key 27 with "score" of 715.&lt;BR /&gt;&lt;BR /&gt;I would appreciate any suggested code, including brief explanation of the functions involved. Thanks in advance&lt;/P&gt;</description>
    <pubDate>Tue, 17 May 2016 17:22:19 GMT</pubDate>
    <dc:creator>brulard</dc:creator>
    <dc:date>2016-05-17T17:22:19Z</dc:date>
    <item>
      <title>Capture 1st value from second table based on closest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-1st-value-from-second-table-based-on-closest-date/m-p/271036#M269463</link>
      <description>&lt;P&gt;Hi-&lt;BR /&gt;I'm trying to query a second table (b) using account ID from first table (a). I want to only capture first observation from table b where start date from table (b) should correspond either to the same date from table (a), or be the first closest date after (as the data from "second" closest date no longer valid).&lt;BR /&gt;Example tables:&lt;BR /&gt;&lt;BR /&gt;Table a&lt;BR /&gt;&lt;BR /&gt;Account_ID | Shopping Date&lt;BR /&gt;___________________________&lt;BR /&gt;&amp;nbsp; 00001&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; | 03/20/2016&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Table b&lt;BR /&gt;&lt;BR /&gt;Key |Account_ID| Start Date | End Date | Score&lt;BR /&gt;____|____________________________________________&lt;BR /&gt;26 &amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 2/18/2016&amp;nbsp; | 3/19/2016 | 709&lt;BR /&gt;____|___________________________________________&lt;BR /&gt;27 &amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 3/19/2016&amp;nbsp; | 3/18/2016 | 715&lt;BR /&gt;____|___________________________________________&lt;BR /&gt;28&amp;nbsp;&amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 4/12/2016&amp;nbsp; | 5/11/2016 | 718&lt;BR /&gt;________________________________________________&lt;BR /&gt;29 &amp;nbsp;&amp;nbsp; | 000001&amp;nbsp;&amp;nbsp; | 5/12/2016&amp;nbsp; | 12/31/9999| 702&lt;BR /&gt;&lt;BR /&gt;So the result queried from table (b) should be key 27 with "score" of 715.&lt;BR /&gt;&lt;BR /&gt;I would appreciate any suggested code, including brief explanation of the functions involved. Thanks in advance&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2016 17:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-1st-value-from-second-table-based-on-closest-date/m-p/271036#M269463</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2016-05-17T17:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Capture 1st value from second table based on closest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-1st-value-from-second-table-based-on-closest-date/m-p/271055#M269464</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dataset b: The end date for Key=27 is before the start date. I changed that.&lt;/P&gt;
&lt;P&gt;I wrote a little program that also works when you have thousands of account_id's (instead of just 1).&lt;/P&gt;
&lt;P&gt;I'm not completely sure that I fully grasp your request, but the below code (or something close) should do the job:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take care:&lt;/P&gt;
&lt;P&gt;This code is quite greedy (especially when you would / could have multiple shopping dates for an account in table a --&amp;gt; Cartesian product due to repeats of by-values in table a AND table b) but it has the advantage of simplicity. Faster solutions are possible (using hash tables for example) but they would be more difficult to read and understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
Account_ID='00001'; Shopping_Date='03/20/2016'; output;
run;
data b;
Key=26; Account_ID='00001'; Start_Date='02/18/2016'; End_Date='03/19/2016'; Score=709; output;
Key=27; Account_ID='00001'; Start_Date='03/19/2016'; End_Date='04/11/2016'; Score=715; output;
Key=28; Account_ID='00001'; Start_Date='04/12/2016'; End_Date='05/11/2016'; Score=718; output;
Key=29; Account_ID='00001'; Start_Date='05/12/2016'; End_Date='12/31/9999'; Score=702; output;
run;
data a; set a;
Shopping_Date1=input(Shopping_Date,mmddyy10.);
format Shopping_Date1 date9.;
run;
data b; set b;
Start_Date1=input(Start_Date,mmddyy10.);
End_Date1  =input(End_Date,  mmddyy10.);
format Start_Date1 End_Date1 date9.;
run;
PROC SQL noprint;
 create table c(where=(Shopping_Date1 between Start_Date1 AND End_Date1)) as
 select   b.Key , b.Account_ID , b.Start_Date1 , b.End_Date1 , b.Score , a.Shopping_Date1
        , INTCK('DAY',Start_Date1,Shopping_Date1) as DaysDiff  
 from   work.a  a
      , work.b  b
 where a.Account_ID = b.Account_ID
 order by b.Account_ID , DaysDiff;  
QUIT;
data c; set c; by Account_ID DaysDiff; if first.Account_ID; run;
/* end of program */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2016 18:26:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-1st-value-from-second-table-based-on-closest-date/m-p/271055#M269464</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2016-05-17T18:26:29Z</dc:date>
    </item>
    <item>
      <title>Re: Capture 1st value from second table based on closest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Capture-1st-value-from-second-table-based-on-closest-date/m-p/271126#M269465</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Instead of putting the where clause on the dates as an output dataset option,&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;create table c(where=(Shopping_Date1 between Start_Date1 AND End_Date1))&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;it's better to make a composite where clause below the "from"&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; where     a.Account_ID = b.Account_ID
       AND Shopping_Date1 between Start_Date1 AND End_Date1
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To verify whether it makes a difference indeed, use some feedback options to acquire extra log-info:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS MSGLEVEL=I; 
PROC SQL _METHOD STIMER noprint;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2016 21:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Capture-1st-value-from-second-table-based-on-closest-date/m-p/271126#M269465</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2016-05-17T21:53:39Z</dc:date>
    </item>
  </channel>
</rss>

