<?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: Maximum date for each months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/900058#M355720</link>
    <description>&lt;P&gt;Here a SAS SQL option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input ID DATE:date9.;
 format date date9.;
 datalines;
2 01JAN2020
2 02JAN2020
2 03JAN2020
2 22FEB2020
2 24FEB2020
3 01JAN2020
3 02JAN2020
3 22FEB2020
3 24FEB2020
4 01JAN2020
4 02JAN2020
4 22FEB2021
4 23FEB2021
4 25MAR2021
4 26MAR2021
4 27MAR2021
;

proc sql;
  create table want as
  select id, max(date) as date format=date9.
  from have
  group by id, intnx('month',date,0,'b')
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 Oct 2023 02:11:49 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-10-26T02:11:49Z</dc:date>
    <item>
      <title>Maximum date for each months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899904#M355653</link>
      <description>&lt;P&gt;I have a big dataset and which contain id's and daily values for it for a few years. I want to select for each id's the maximum day for each month for this few years.&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID DATE&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 01JAN202&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 02JAN2020&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 03JAN2020&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 22FEB2020&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 24FEB2020&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 01JAN2020&lt;/P&gt;&lt;P&gt;3 02JAN2020&lt;/P&gt;&lt;P&gt;3 22FEB2020&lt;/P&gt;&lt;P&gt;3 24FEB2020&lt;/P&gt;&lt;P&gt;4 01JAN2020&lt;/P&gt;&lt;P&gt;4 02JAN2020&lt;/P&gt;&lt;P&gt;4 22FEB2021&lt;/P&gt;&lt;P&gt;4 23FEB2021&lt;/P&gt;&lt;P&gt;4 25MAR2021&lt;/P&gt;&lt;P&gt;4 26MAR2021&lt;/P&gt;&lt;P&gt;4 27MAR2021&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the above sample I need to get the data as below:&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 02JAN2020&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 24FEB2020&lt;/P&gt;&lt;P&gt;3 02JAN2020&lt;/P&gt;&lt;P&gt;3 24FEB2020&lt;/P&gt;&lt;P&gt;4 02JAN2020&lt;/P&gt;&lt;P&gt;4 23FEB2021&lt;/P&gt;&lt;P&gt;4 27MAR2021&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 09:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899904#M355653</guid>
      <dc:creator>newuser2022</dc:creator>
      <dc:date>2023-10-25T09:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum date for each months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899905#M355654</link>
      <description>&lt;P&gt;Assuming the data are numeric and not character (are they? you didn't say) this is a job for PROC SUMMARY&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class id date;
    format date monyy7.;
    var date;
    output out=want max= ;
run;
proc datasets library=work nolist;
    modify want;
    format date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Oct 2023 09:49:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899905#M355654</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-25T09:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum date for each months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899912#M355655</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select id, date
  from have
  group by id, month(date)
  having date = max(date)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Oct 2023 10:00:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899912#M355655</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-25T10:00:14Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum date for each months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899921#M355658</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select id, date
  from have
  group by id, month(date)
  having date = max(date)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Doesn't this fail if you have dates in January (or any month) in two different years?&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 11:18:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899921#M355658</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-25T11:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum date for each months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899938#M355663</link>
      <description>&lt;P&gt;Correct. year(date) should be inserted in the GROUP BY. Or we should use put(date,yymmn6.).&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 12:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/899938#M355663</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-25T12:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum date for each months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/900046#M355716</link>
      <description>&lt;P&gt;Apparently your data is sorted by ID/DATE, and you want the latest date in each month.&lt;/P&gt;
&lt;P&gt;Then:&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 :date9. ;
  format date date9. ;
datalines;
2  01JAN2020
2  02JAN2020
2  03JAN2020
2  22FEB2020
2  24FEB2020
3  01JAN2020
3 02JAN2020
3 22FEB2020
3 24FEB2020
4 01JAN2020
4 02JAN2020
4 22FEB2021
4 23FEB2021
4 25MAR2021
4 26MAR2021
4 27MAR2021
run;
data want (drop=nxt_date);
  set have (keep=id);
  by id;
  merge have    have(firstobs=2 keep=date rename=(date=nxt_date));
  if last.id=1 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 self-merge in the merge statement provides a way to look ahead one observation (using the FIRSTOBS=2 option), and compare the current date to the following date (nxt_date).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, you included a 03jan2020 in your data for ID=1.&amp;nbsp; So shouldn't that be the expected value for your result?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 01:08:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/900046#M355716</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-26T01:08:53Z</dc:date>
    </item>
    <item>
      <title>Re: Maximum date for each months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/900058#M355720</link>
      <description>&lt;P&gt;Here a SAS SQL option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input ID DATE:date9.;
 format date date9.;
 datalines;
2 01JAN2020
2 02JAN2020
2 03JAN2020
2 22FEB2020
2 24FEB2020
3 01JAN2020
3 02JAN2020
3 22FEB2020
3 24FEB2020
4 01JAN2020
4 02JAN2020
4 22FEB2021
4 23FEB2021
4 25MAR2021
4 26MAR2021
4 27MAR2021
;

proc sql;
  create table want as
  select id, max(date) as date format=date9.
  from have
  group by id, intnx('month',date,0,'b')
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Oct 2023 02:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Maximum-date-for-each-months/m-p/900058#M355720</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-26T02:11:49Z</dc:date>
    </item>
  </channel>
</rss>

