<?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 by identifying the most recent date relative to the refernce date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321991#M271052</link>
    <description>Thanks so much for your guidance. I am super new to this. Do u mind sharing how to do the left join??&lt;BR /&gt;</description>
    <pubDate>Mon, 02 Jan 2017 18:44:06 GMT</pubDate>
    <dc:creator>aarony</dc:creator>
    <dc:date>2017-01-02T18:44:06Z</dc:date>
    <item>
      <title>merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321975#M271049</link>
      <description>&lt;P&gt;i have a dataset (dataset1).&lt;/P&gt;
&lt;P&gt;ticker date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;19991231 &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20011231 &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20031231 &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i have another dataset&amp;nbsp;&lt;SPAN&gt;(dataset2).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;ticker date&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20020101&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i want to find the most recent date from dataset 1 in reference to dataset 2. (ie., find in the dataset1 the closest recent date to the date in dataset2&amp;nbsp;to rrive at the following):&lt;/P&gt;
&lt;P&gt;ticker date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;20020101 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;could you provide me of your guidance? thx in advance.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jan 2017 17:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321975#M271049</guid>
      <dc:creator>aarony</dc:creator>
      <dc:date>2017-01-02T17:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321985#M271050</link>
      <description>&lt;P&gt;i am trying something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt; create table f32 as select &lt;BR /&gt; a.*, b.*&lt;BR /&gt; from m2 as a, t3 as b&lt;BR /&gt; where a.ticker=b.ticker and b.date-a.date=min(b.date-a.date);&lt;BR /&gt; quit; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but this does not work. any one has any insights?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jan 2017 18:12:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321985#M271050</guid>
      <dc:creator>aarony</dc:creator>
      <dc:date>2017-01-02T18:12:08Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321989#M271051</link>
      <description>&lt;P&gt;You do not have GROUP BY clause. &amp;nbsp;Also you want to use a HAVING clause to subset using the aggregate value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table f32 as
    select a.*
         , b.*
         , (b.date - a.date) as diff
    from m2 as a
       , t3 as b
    where a.ticker=b.ticker
      and a.date &amp;lt;= b.date 
    group by a.ticker
    having (calculated diff)=min(calculated diff)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might also what to use an LEFT JOIN instead of the implied inner join so that you can keep records that do not have prior dates to match to them.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jan 2017 18:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321989#M271051</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-01-02T18:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321991#M271052</link>
      <description>Thanks so much for your guidance. I am super new to this. Do u mind sharing how to do the left join??&lt;BR /&gt;</description>
      <pubDate>Mon, 02 Jan 2017 18:44:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321991#M271052</guid>
      <dc:creator>aarony</dc:creator>
      <dc:date>2017-01-02T18:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321997#M271053</link>
      <description>&lt;P&gt;Let's make some example data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
 input ticker $ date value ;
 informat date yymmdd10.;
 format date yymmdd10. ;
cards;
a 19991231 1
a 20011231 2
a 20031231 3
b 20151231 4
;

data master;
  input ticker $ date ;
  informat date yymmdd10.;
  format date yymmdd10.;
cards;
a 20020101
c 20160701
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's use LOOKUP to find the most recent VALUE for each record in the MASTER table. &amp;nbsp;Notice that the TICKER='c' there will be no match.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
    select master.*
         , lookup.value
         , lookup.date as value_date
    from master
    left join lookup
    on master.ticker=lookup.ticker
      and lookup.date &amp;lt;= master.date
    group by master.ticker
    having (lookup.date)=max(lookup.date)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Jan 2017 18:59:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/321997#M271053</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-01-02T18:59:11Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322020#M271054</link>
      <description>Dear Tom, thank you so much for your kind guidance.&lt;BR /&gt;For some reason, the code is not grabbing the value from most recent date. I am really puzzled why this is.&lt;BR /&gt;Could it be because of the date format? My date format is YYMMDDN8.&lt;BR /&gt;I get crazy numbers on my DIFF variable...i.e., 20112614&lt;BR /&gt;</description>
      <pubDate>Mon, 02 Jan 2017 22:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322020#M271054</guid>
      <dc:creator>aarony</dc:creator>
      <dc:date>2017-01-02T22:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322021#M271055</link>
      <description>&lt;P&gt;Make sure your variables are really dates. &amp;nbsp;They might be datetime values with format that makes them look like dates. Or since you are not printing any delimiters it is possible your date variables as just numbers that look to you like a date but that SAS is think is just a number.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jan 2017 23:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322021#M271055</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-01-02T23:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322022#M271056</link>
      <description>&lt;P&gt;thank you so somuch!!! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this was very very helpful!&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jan 2017 23:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322022#M271056</guid>
      <dc:creator>aarony</dc:creator>
      <dc:date>2017-01-02T23:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: merging by identifying the most recent date relative to the refernce date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322054#M271057</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup;
 input ticker $ date value ;
 informat date yymmdd10.;
 format date yymmdd10. ;
cards;
a 19991231 1
a 20011231 2
a 20031231 3
b 20151231 4
;

data master;
  input ticker $ date ;
  informat date yymmdd10.;
  format date yymmdd10.;
cards;
a 20020101
c 20160701
;
data want;
 set lookup(in=ina) master(in=inb);
 by ticker date;
 retain temp;
 if first.ticker then temp=.;
 if ina then temp=value;
 if inb then do;value=temp;output;end;
 drop temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 03 Jan 2017 03:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-by-identifying-the-most-recent-date-relative-to-the/m-p/322054#M271057</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-01-03T03:12:40Z</dc:date>
    </item>
  </channel>
</rss>

