<?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: Retrieving past 5 years record from a focal date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532486#M145914</link>
    <description>&lt;P&gt;haven't used yrdiff in a while.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp; is brilliant as usual&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;copied(plagiarized from his) for hash demo&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input date :yymmdd10.;
format date yymmdd10.;
cards;
2013-03-01
2014-07-30
2014-09-15
2015-02-22
2018-01-02
2018-03-02
2019-01-26
;

data want ;
 if _n_=1 then do;
 if 0 then set have(rename=(date=_date));
   declare hash H (dataset:'have(rename=(date=_date))',ordered: "A") ;
   h.definekey  ("_date") ;
   h.definedone () ;
   declare hiter hh('h');
   end;
set have;
years=0;
do while(hh.next()=0);
if 	0 &amp;lt; yrdif(_date,DATE,'age') &amp;lt;= 5 then years=sum(years,1);
end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 04 Feb 2019 03:25:20 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-02-04T03:25:20Z</dc:date>
    <item>
      <title>Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532478#M145907</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have observations with the 'date_created' column having dates as in YYYY-MM-DD format.&lt;/P&gt;&lt;P&gt;I would like to count the number of observations that are created within past 5 years from the focal observation, by adding 'numof5years' column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my data:&lt;/P&gt;&lt;P&gt;[data_created]&lt;/P&gt;&lt;P&gt;2013-03-01&lt;/P&gt;&lt;P&gt;2014-07-30&lt;/P&gt;&lt;P&gt;2014-09-15&lt;/P&gt;&lt;P&gt;2015-02-22&lt;/P&gt;&lt;P&gt;2018-01-02&lt;/P&gt;&lt;P&gt;2018-03-02&lt;/P&gt;&lt;P&gt;2019-01-26&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;expected output:&lt;/P&gt;&lt;P&gt;[data_created]&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; [numof5years]&lt;/P&gt;&lt;P&gt;2013-03-01&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2014-07-30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;2014-09-15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2015-02-22&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;2018-01-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2018-03-02&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2019-01-26&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 01:43:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532478#M145907</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-02-04T01:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532480#M145909</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
 input DATE yymmdd10.;
 cards;
2013-03-01
2014-07-30
2014-09-15
2015-02-22
2018-01-02
2018-03-02
2019-01-26
run;
proc sql;
  select a.DATE     format=date9.
        ,count(b.DATE)        
  from HAVE a
         left join
       HAVE b
        on 0 &amp;lt; yrdif(b.DATE,a.DATE) &amp;lt;= 5
  group by a.DATE
  order by a.DATE;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;&lt;A target="_blank" name="IDX"&gt;&lt;/A&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;DATE&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;&amp;nbsp;&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;01MAR2013&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;30JUL2014&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;15SEP2014&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;22FEB2015&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;02JAN2018&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;02MAR2018&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;26JAN2019&lt;/TD&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 02:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532480#M145909</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-02-04T02:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532481#M145910</link>
      <description>Hi Chris, actually i'm doing this in a DATA procedure.&lt;BR /&gt;will there be a comparable function as 'yrdif' of SQL?</description>
      <pubDate>Mon, 04 Feb 2019 02:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532481#M145910</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-02-04T02:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532482#M145911</link>
      <description>For a within table lookup like this, it's better to stick with SQL, it's much harder to do this type of join in a data step.</description>
      <pubDate>Mon, 04 Feb 2019 02:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532482#M145911</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-04T02:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532485#M145913</link>
      <description>&lt;DIV class="lia-quilt-row lia-quilt-row-forum-message-main"&gt;
&lt;DIV class="lia-quilt-column lia-quilt-column-20 lia-quilt-column-right lia-quilt-column-main-right"&gt;
&lt;DIV class="lia-quilt-column-alley lia-quilt-column-alley-right"&gt;
&lt;DIV id="messagebodydisplay_0" class="lia-message-body lia-component-body"&gt;
&lt;DIV class="lia-message-body-content"&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;&lt;/EM&gt;&lt;/SPAN&gt;&lt;EM&gt;actually i'm doing this in a DATA procedure. will there be a comparable function as 'yrdif' of SQL?&lt;/EM&gt;&lt;/DIV&gt;
&lt;DIV class="lia-message-body-content"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="lia-message-body-content"&gt;You can use that function in data step as well. Cartesian products are not easily obtained in data steps though.&lt;/DIV&gt;
&lt;DIV class="lia-message-body-content"&gt;The type of join guides the coding choice here.&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="lia-quilt-row lia-quilt-row-forum-message-footer"&gt;
&lt;DIV class="lia-quilt-column lia-quilt-column-04 lia-quilt-column-left lia-quilt-column-footer-left"&gt;
&lt;DIV class="lia-quilt-column-alley lia-quilt-column-alley-left"&gt;
&lt;DIV class="lia-message-notify lia-component-report-abuse"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Mon, 04 Feb 2019 02:41:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532485#M145913</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-02-04T02:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532486#M145914</link>
      <description>&lt;P&gt;haven't used yrdiff in a while.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp; is brilliant as usual&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;copied(plagiarized from his) for hash demo&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input date :yymmdd10.;
format date yymmdd10.;
cards;
2013-03-01
2014-07-30
2014-09-15
2015-02-22
2018-01-02
2018-03-02
2019-01-26
;

data want ;
 if _n_=1 then do;
 if 0 then set have(rename=(date=_date));
   declare hash H (dataset:'have(rename=(date=_date))',ordered: "A") ;
   h.definekey  ("_date") ;
   h.definedone () ;
   declare hiter hh('h');
   end;
set have;
years=0;
do while(hh.next()=0);
if 	0 &amp;lt; yrdif(_date,DATE,'age') &amp;lt;= 5 then years=sum(years,1);
end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 03:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532486#M145914</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-04T03:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532489#M145916</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;@ChrisNZ&amp;nbsp; is brilliant as usual&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;img id="smileyembarrassed" class="emoticon emoticon-smileyembarrassed" src="https://communities.sas.com/i/smilies/16x16_smiley-embarrassed.png" alt="Smiley Embarassed" title="Smiley Embarassed" /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 03:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532489#M145916</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-02-04T03:39:09Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532513#M145926</link>
      <description>&lt;P&gt;You can do it in a datastep like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
 input DATE yymmdd10.;
 format date yymmdd10.;
 cards;
2013-03-01
2014-07-30
2014-09-15
2015-02-22
2018-01-02
2018-03-02
2019-01-26
;run;

data want;
  set have;
  retain _P_ 1;
  do _P_=_P_ to _N_-1;
    set have(keep=date rename=(date=old_date)) point=_P_;
    if old_date&amp;gt;=intnx('year',date,-5,'S') then leave;
    end;
  numof5years=_N_-_P_;
  drop old_date;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have a BY variable in the real data, just ad a couple of lines:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by key;
  if first.key then
    _P_=_N_;
  retain _P_ 1;
  do _P_=_P_ to _N_-1;
    set have(keep=date rename=(date=old_date)) point=_P_;
    if old_date&amp;gt;=intnx('year',date,-5,'S') then leave;
    end;
  numof5years=_N_-_P_;
  drop old_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Feb 2019 07:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532513#M145926</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-02-04T07:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532531#M145931</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
 input DATE yymmdd10.;
 format DATE yymmdd10.;
 cards;
2013-03-01
2014-07-30
2014-09-15
2015-02-22
2018-01-02
2018-03-02
2019-01-26
run;
proc sql;
select *,(select count(*) from have 
where  intnx('year',a.date,-5,'s') &amp;lt; date &amp;lt; a.date) as want
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Feb 2019 11:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532531#M145931</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-02-04T11:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532570#M145941</link>
      <description>&lt;P&gt;Hi Nov, brilliant as usual!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have modified the range,&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 	0 &amp;lt;= yrdif(_date,DATE,'age') &amp;lt; 5&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;because i wanted to count the dates within the same year.&lt;/P&gt;&lt;P&gt;but by doing this, this makes a tiny problem... that some are counted twice..&lt;/P&gt;&lt;P&gt;when comparing with the hashtable, what if I want to cancel out counts coming from myself?&lt;/P&gt;</description>
      <pubDate>Mon, 04 Feb 2019 14:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532570#M145941</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-02-04T14:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532571#M145942</link>
      <description>wow adding a calculated row using sql... thnx a lot K</description>
      <pubDate>Mon, 04 Feb 2019 14:42:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532571#M145942</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-02-04T14:42:47Z</dc:date>
    </item>
    <item>
      <title>Re: Retrieving past 5 years record from a focal date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532653#M145972</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input date :yymmdd10.;
format date yymmdd10.;
cards;
2013-03-01
2014-07-30
2014-09-15
2015-02-22
2018-01-02
2018-03-02
2019-01-26
;

proc sql;
create table want as
select a.date,sum(0 &amp;lt; yrdif(b.date,a.DATE,'age') &amp;lt;= 5)  as count
from have a, have b
where b.date&amp;lt;=a.date
group by a.date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Feb 2019 18:02:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retrieving-past-5-years-record-from-a-focal-date/m-p/532653#M145972</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-04T18:02:04Z</dc:date>
    </item>
  </channel>
</rss>

