<?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: Max date for id in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444418#M28730</link>
    <description>&lt;P&gt;Your data are already apparently sorted by ID and DATE.&amp;nbsp; You need a way to (1) determine if the record-in-hand is the last record for an id, or (2) the date of the next record is not the same month as current date:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id date :mmddyy10. value;
  format date date9.;
datalines;
1      1/20/2018            45
1      1/31/2018           100
1      2/20/2018            20
1      2/25/2018            87
2      1/17/2018            36
2      1/27/2018             45
2      2/02/2018             54
2      2/28/2018             68
run;

data want (drop=nxt_date);
  set have nobs=nrecs;
  by id;
  if _n_&amp;lt;nrecs then set have (firstobs=2 keep=date rename=(date=nxt_date));
  if last.id or intck('month',date,nxt_date)&amp;gt;0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "set have (firstobs=2) provides a way to look ahead by one observation, to compare nxt_date to date.&amp;nbsp;&amp;nbsp; It is the result clause of an "if _n_&amp;lt;nrecs" statement because without the IF test it would attempt to prematurely read beyond the end of data set HAVE, and the last obs of HAVE would not be output.&lt;/P&gt;</description>
    <pubDate>Sat, 10 Mar 2018 17:16:51 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2018-03-10T17:16:51Z</dc:date>
    <item>
      <title>Max date for id</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444303#M28718</link>
      <description>&lt;P&gt;I have data where i have ID and date and value&amp;nbsp; and need to find max date for each month for ID&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/20/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 45&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/31/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/20/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/25/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 87&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/17/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 36&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/27/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/02/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;54&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/28/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;68&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;what i want my data to look like&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/31/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/25/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 87&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 1/27/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2/28/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;68&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 23:11:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444303#M28718</guid>
      <dc:creator>hk2013</dc:creator>
      <dc:date>2018-03-09T23:11:04Z</dc:date>
    </item>
    <item>
      <title>Re: Max date for id</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444306#M28719</link>
      <description>&lt;P&gt;Here is one way:&lt;/P&gt;
&lt;PRE&gt;  input ID Date : mmddyy10. value;
  format Date date9.;
  cards;
1      1/20/2018            45
1      1/31/2018           100
1      2/20/2018            20
1      2/25/2018            87
2      1/17/2018            36
2      1/27/2018             45
2      2/02/2018             54
2      2/28/2018             68
;

data need;
  set have;
  month=month(date);
  day=day(date);
  year=year(date);
run;

proc sort data=need;
  by id year month descending day;
run;

proc sort data=need out=want nodupkey;
  by id year month;
run;

&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Mar 2018 23:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444306#M28719</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-03-09T23:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: Max date for id</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444308#M28720</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input ID Date : mmddyy10. value;
  format Date date9.;
  cards;
1      1/20/2018            45
1      1/31/2018           100
1      2/20/2018            20
1      2/25/2018            87
2      1/17/2018            36
2      1/27/2018             45
2      2/02/2018             54
2      2/28/2018             68
;


proc sql;
create table want as
select a.*
from have a, (select id,max(date) as max_date from have group by id, month(date)) b
where a.id =b.id  and a.date=b.max_date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Mar 2018 23:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444308#M28720</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-09T23:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Max date for id</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444314#M28721</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input ID Date : mmddyy10. value;
  format Date date9.;
  cards;
1      1/20/2018            45
1      1/31/2018           100
1      2/20/2018            20
1      2/25/2018            87
2      1/17/2018            36
2      1/27/2018             45
2      2/02/2018             54
2      2/28/2018             68
;

proc sql;
create table want as
select id, date,  value
from have
group by id, month(date)
having max(date)=date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Mar 2018 23:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444314#M28721</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-09T23:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: Max date for id</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444418#M28730</link>
      <description>&lt;P&gt;Your data are already apparently sorted by ID and DATE.&amp;nbsp; You need a way to (1) determine if the record-in-hand is the last record for an id, or (2) the date of the next record is not the same month as current date:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id date :mmddyy10. value;
  format date date9.;
datalines;
1      1/20/2018            45
1      1/31/2018           100
1      2/20/2018            20
1      2/25/2018            87
2      1/17/2018            36
2      1/27/2018             45
2      2/02/2018             54
2      2/28/2018             68
run;

data want (drop=nxt_date);
  set have nobs=nrecs;
  by id;
  if _n_&amp;lt;nrecs then set have (firstobs=2 keep=date rename=(date=nxt_date));
  if last.id or intck('month',date,nxt_date)&amp;gt;0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "set have (firstobs=2) provides a way to look ahead by one observation, to compare nxt_date to date.&amp;nbsp;&amp;nbsp; It is the result clause of an "if _n_&amp;lt;nrecs" statement because without the IF test it would attempt to prematurely read beyond the end of data set HAVE, and the last obs of HAVE would not be output.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Mar 2018 17:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444418#M28730</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-10T17:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: Max date for id</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444425#M28731</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input ID Date : mmddyy10. value;
  format Date date9.;
  cards;
1      1/20/2018            45
1      1/31/2018           100
1      2/20/2018            20
1      2/25/2018            87
2      1/17/2018            36
2      1/27/2018             45
2      2/02/2018             54
2      2/28/2018             68
;


data _null_;
if _n_=1 then do;
if 0 then set have;
dcl hash H (ordered:'y') ;
   h.definekey  ("id","month") ;
   h.definedata ("id","date","value") ;
   h.definedone () ;
end;
set have end=last;
month=month(date);
h.replace();
if last then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Mar 2018 18:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Max-date-for-id/m-p/444425#M28731</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-10T18:28:52Z</dc:date>
    </item>
  </channel>
</rss>

