<?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: Select maximum date among the subject in Advanced Programming</title>
    <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828068#M196</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject date :mmddyy10. dummy;
format date mmddyy10.;
datalines;
1 3/8/2022  0 
2 3/9/2022  0 
2 3/10/2022 1 
2 3/11/2022 1 
3 3/12/2022 0 
;

proc sql;
   create table want as
   select subject
        , max(date) as date format = mmddyy10.
        , dummy
   from have
   group by subject
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;subject  date        dummy
1        03/08/2022  0 
2        03/11/2022  1 
2        03/11/2022  0 
2        03/11/2022  1 
3        03/12/2022  0 
&lt;/PRE&gt;</description>
    <pubDate>Wed, 10 Aug 2022 12:03:24 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2022-08-10T12:03:24Z</dc:date>
    <item>
      <title>Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828065#M194</link>
      <description>&lt;P&gt;I have data like table-1 and I want it like table-2&lt;/P&gt;
&lt;P&gt;I want maximum date for all the records among the one subject.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Table-1&lt;/P&gt;
&lt;TABLE tabindex="0" width="197" data-is-focusable="true"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;&lt;SPAN&gt;subject&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px"&gt;&lt;SPAN&gt;date&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px" align="right"&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;SPAN&gt;3/8/2022&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;EM&gt;&lt;STRONG&gt;3/9/2022&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;EM&gt;&lt;STRONG&gt;3/10/2022&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;EM&gt;&lt;STRONG&gt;3/11/2022&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;SPAN&gt;3/12/2022&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;Table-2&lt;/P&gt;
&lt;TABLE tabindex="0" width="197" data-is-focusable="true"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;&lt;SPAN&gt;subject&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px"&gt;&lt;SPAN&gt;date&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px" align="right"&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;SPAN&gt;3/8/2022&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;&lt;EM&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;EM&gt;&lt;STRONG&gt;3/11/2022&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;&lt;EM&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;EM&gt;&lt;STRONG&gt;3/11/2022&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;&lt;EM&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;EM&gt;&lt;STRONG&gt;3/11/2022&lt;/STRONG&gt;&lt;/EM&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="65.7604px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="87.5938px" height="30px" align="right"&gt;&lt;SPAN&gt;3/12/2022&lt;/SPAN&gt;&lt;/TD&gt;
&lt;TD width="42.6458px" height="30px" align="right"&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 10 Aug 2022 11:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828065#M194</guid>
      <dc:creator>KalaBhairava</dc:creator>
      <dc:date>2022-08-10T11:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828068#M196</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject date :mmddyy10. dummy;
format date mmddyy10.;
datalines;
1 3/8/2022  0 
2 3/9/2022  0 
2 3/10/2022 1 
2 3/11/2022 1 
3 3/12/2022 0 
;

proc sql;
   create table want as
   select subject
        , max(date) as date format = mmddyy10.
        , dummy
   from have
   group by subject
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;subject  date        dummy
1        03/08/2022  0 
2        03/11/2022  1 
2        03/11/2022  0 
2        03/11/2022  1 
3        03/12/2022  0 
&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Aug 2022 12:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828068#M196</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-10T12:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828070#M197</link>
      <description>&lt;P&gt;Hi KalaBhairava,&lt;/P&gt;&lt;P&gt;Here's a quick solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data = table1 out=temp;
	by subject descending date;
run;

data table2 (drop = h_d);
	set temp;
	by subject;
	retain h_d;
	if first.subject then h_d = date;
	date = h_d;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;HTH,&lt;/P&gt;&lt;P&gt;Cheers.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 12:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828070#M197</guid>
      <dc:creator>Anne_A</dc:creator>
      <dc:date>2022-08-10T12:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828072#M198</link>
      <description>&lt;P&gt;Or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject date :mmddyy10. dummy;
format date mmddyy10.;
datalines;
1 3/8/2022  0 
2 3/9/2022  0 
2 3/10/2022 1 
2 3/11/2022 1 
3 3/12/2022 0 
;

data want(drop = d);
   do _N_ = 1 by 1 until (last.subject);
      set have;
      by subject;
      if date &amp;gt; d then d = date;
   end;
   do _N_ = 1 to _N_;
      set have;
      date = d;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Aug 2022 12:11:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828072#M198</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-10T12:11:23Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828095#M199</link>
      <description>&lt;P&gt;If the data are sorted by subject:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (in=firstpass) have (in=secondpass);
  by subject;
  retain maxdate;
  if first.subject then maxdate=date;
  else if firstpass then maxdate=max(date,maxdate);
  if secondpass;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if the data are not sorted, and sorting is too expensive then you should use&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;'s suggestion.&amp;nbsp; &amp;nbsp;(I've struck out my original content below - it was wrong).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;STRIKE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if _n_=1 then do;
    if 0 then set have (rename=(date=maxdate));
    declare hash h (dataset:'have (keep=subject date rename=(date=maxdate))',multidata:'Y',ordered:'D');
      h.definekey('subject');
      h.definedata('maxdate');
      h.definedone();
  end;
  h.find();
run;&lt;/CODE&gt;&lt;/STRIKE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRIKE&gt;&amp;nbsp;&lt;/STRIKE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 20:26:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828095#M199</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-12T20:26:08Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828141#M200</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;. I always like the elegance of your trademark "firstpass-secondpass" solutions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this technique could even help in the "not sorted" case to support the hash object approach.&amp;nbsp;&lt;SPAN&gt;Here's my attempt to combine the two:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=rc);
  set have (in=firstpass) have (in=secondpass);
  if _n_=1 then do;
    dcl hash h();
    h.definekey('subject');
    h.definedata('maxdate');
    h.definedone();
    maxdate=.;
  end;
  rc=h.find();
  if firstpass &amp;amp; date&amp;gt;maxdate then h.replace(key: subject, data: date);
  if secondpass;
  format maxdate mmddyy10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Aug 2022 18:24:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828141#M200</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-10T18:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828190#M201</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate your positive comment about the firstpass/secondpass paradigm.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if the data is sorted, there is likely an efficiency difference compared to your suggestion.&amp;nbsp; When you use it with a BY statement, as in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have (in=firstpass) have (in=secondpass);
    by subject;
    .....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the data are interleaved, meaning a given ID group is read twice before proceeding to the next ID group.&amp;nbsp; &amp;nbsp;It'll be more efficient because that BY group data is likely sitting in memory allocated as a disk buffer after the firstpass. So the secondpass likely reads from memory rather than over the disk channel from external storage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if you have to do it without the BY statement, as in:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have (in=firstpass) have (in=secondpass);
    .... other code ....&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;it reads the ENTIRE dataset for firstpass before rereading during secondpass.&amp;nbsp; You'll almost certainly be forced to implement a lot more disk activity.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, my solution for unsorted data using the hash object statement&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;declare hash h (dataset:'have'.....)&lt;/EM&gt;&lt;/STRONG&gt; would have the same disadvantage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In that case, your solution would likely be a bit better because your hash object would take a lot less memory, having only one "row" per SUBJECT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Aug 2022 23:13:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828190#M201</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-10T23:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select maximum date among the subject</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828220#M202</link>
      <description>&lt;P&gt;Thanks. Very good point about reading data from memory vs. disk. Yes, for sorted data I wouldn't have suggested this approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;&lt;FONT size="2"&gt;Actually, my main motivation for creating this solution was to "rescue" your hash object idea, which I think is unlikely to work in the original form because the "&lt;FONT face="courier new,courier"&gt;ordered:'D'&lt;/FONT&gt;" argument tag refers to the &lt;EM&gt;key&lt;/EM&gt; values only, i.e., won't sort the dates (or has this changed in a SAS release newer than 9.4M5?). Adding &lt;FONT face="courier new,courier"&gt;'maxdate'&lt;/FONT&gt;&amp;nbsp;to the key portion of &lt;FONT face="courier new,courier"&gt;h&lt;/FONT&gt; would hamper the intended lookup based only on &lt;FONT face="courier new,courier"&gt;subject&lt;/FONT&gt;.&lt;/FONT&gt;&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2022 07:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Select-maximum-date-among-the-subject/m-p/828220#M202</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-11T07:41:24Z</dc:date>
    </item>
  </channel>
</rss>

