<?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 Row with the max Value per group-Find efficient program in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865326#M341728</link>
    <description>&lt;P&gt;Please note that I MUST use out statement in sort procedure because I am not able to sort the raw data set,&lt;/P&gt;
&lt;P&gt;Will it also add running time?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Mar 2023 00:45:54 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2023-03-21T00:45:54Z</dc:date>
    <item>
      <title>Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865305#M341712</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a very big data set&amp;nbsp; which contain&amp;nbsp;502,899,724 rows.&lt;/P&gt;
&lt;P&gt;In the data set there are 90 columns.&lt;/P&gt;
&lt;P&gt;Each customer ID (Field ID) appear in multiple rows.&lt;/P&gt;
&lt;P&gt;Only 3 fields are interesting for me- ID, date,status.&lt;/P&gt;
&lt;P&gt;The data set is not sorted.&lt;/P&gt;
&lt;P&gt;The task is to create a new data set with one row per customer ID.&lt;/P&gt;
&lt;P&gt;The criteria is to choose the row with the maximum date value.&lt;/P&gt;
&lt;P&gt;I know 4 ways to do this task but the problem is that when I run it in real life with the big data set then the process run long time and cannot finish.&lt;/P&gt;
&lt;P&gt;I would like to ask please If anyone can show me a clever way to deal with this problem.&lt;/P&gt;
&lt;P&gt;I will appreciate If you can show a code that solve this issue.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Please note that the raw data set doesn't have Index and is not sorted&amp;nbsp;&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I&amp;nbsp; also want to ask- from the 4 ways I showed here which way is most efficient with related to run time?&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. status;
format date date9.;
cards;
1 19MAR2023 1
1 17MAR2023 0
1 18MAR2023 0
1 15MAR2023 1
1 14MAR2023 0
1 12MAR2023 0
2 11MAR2023 0
2 13MAR2023 0
2 15MAR2023 0
2 14MAR2023 0
2 12MAR2023 0
3 18MAR2023 1
3 17MAR2023 0
;
Run;



proc sort data=have;
by ID date;
Run;
data want_Way1;
set want_Way3;
by ID;
IF last.ID;
Run;


proc sql;
create table want_Way2 as
select a.*
from have as a
where  date=(select max(date)  FROM have as b Where b.ID=a.ID)
;
quit;


proc sql;
create table want_Way3 as
select a.*
from have as a
inner join
(select ID,
        max(date) as date
		From have 
		group by ID) as b
on a.ID=b.ID  and a.date=b.date
;
quit;


proc sql;
create table want_Way4 as
select a.*
from have  as a
LEFT  Join have as b
on a.ID=b.ID and a.date&amp;lt;b.date
where b.ID is null
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;have index.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 21:29:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865305#M341712</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-03-20T21:29:36Z</dc:date>
    </item>
    <item>
      <title>Re: Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865315#M341722</link>
      <description>&lt;P&gt;Why do you have such a large dataset that is not sorted?&amp;nbsp; It will be pretty hard to use for any analysis.&lt;/P&gt;
&lt;P&gt;Why are you taking ALL of the variables if you only want 3 of them?&amp;nbsp; That makes any of those programs take much longer than you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest first trying PROC SUMMARY and see if that works.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway data=have(keep=id date status);
  class id; 
  output out=want(drop=_type_ _freq_) idgroup(max(date) out[1] (date status)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It might complain if there are too many levels of ID to fit into memory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so then try a divide and conquer approach using OBS= and FIRSTOBS= dataset options.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway data=have(keep=id date status firstobs=1 obs=1000000);
  class id; 
  output part1=want(drop=_type_ _freq_) idgroup(max(date) out[1] (date status)=);
run;
proc summary nway data=have(keep=id date status firstobs=1000001 obs=2000000);
  class id; 
  output part2=want(drop=_type_ _freq_) idgroup(max(date) out[1] (date status)=);
run;
...
data want;
  set part1-part503 ;
  by id date ;
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 22:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865315#M341722</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-20T22:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865316#M341723</link>
      <description>&lt;P&gt;If you want the maximum date per id with that many observations I wouldn't touch anything with SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You say "The task is to create a new data set with one row per customer ID." But do not describe anything about what is required for the third variable of interest status.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do not say anything about how many unique values of ID you have but I suspect that you may have more than the Class statement in Proc Summary would accept.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't say a bit about where the thing failed. You may be running out of work space with the Sort step as unless you use some options you need about 3 times as much disk space as the set occupies to process and may well exceed available or allotted work space. If your job fails at the the Proc Sort then TAGSORT may help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My approach would be :&lt;/P&gt;
&lt;PRE&gt;Proc sort data=have tagsort;
   by id;
run;

proc summary data=have ;
   by id;
   var date;
   output out=want (drop=_:) max= ;
run;&lt;/PRE&gt;
&lt;P&gt;If you want the STATUS variable value that is associated with the max date, not stated anywhere in your description then&lt;/P&gt;
&lt;PRE&gt;proc summary data=have ;
   by id;
   var date;
   output out=want (drop=_:) max=
        maxid(date(status))=status;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 22:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865316#M341723</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-20T22:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865325#M341727</link>
      <description>&lt;P&gt;The task is to find the most recent customer status so technically for&amp;nbsp; each customer ID need to select the row with maximum date.(Please note that it cannot happen that&amp;nbsp; for same customer ID have multiple rows with same date)&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 00:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865325#M341727</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-03-21T00:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865326#M341728</link>
      <description>&lt;P&gt;Please note that I MUST use out statement in sort procedure because I am not able to sort the raw data set,&lt;/P&gt;
&lt;P&gt;Will it also add running time?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 00:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865326#M341728</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-03-21T00:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865333#M341733</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Please note that I MUST use out statement in sort procedure because I am not able to sort the raw data set,&lt;/P&gt;
&lt;P&gt;Will it also add running time?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Shouldn't, but if you don't need all the variables just keep the ones you want in the output set.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 01:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865333#M341733</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-21T01:39:09Z</dc:date>
    </item>
    <item>
      <title>Re: Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865342#M341738</link>
      <description>&lt;P&gt;I'd expect the proc sort/data step to perform best because it only requires one sort of the data. Make sure to only keep the variables of interest. ...and I assume that compression is set as a default else add it as a dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have(keep=id date status) out=inter(compress=binary);
  by id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;IF you've got sufficient memory to hold one row per ID in-memory then using a data step hash will likely outperform any other approach as it doesn't require any physical sort of the data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  if _n_=1 then
    do;
      if 0 then set have(keep=date rename=(date=_date));
      length _status 3;
      dcl hash h1();
      h1.defineKey('id');
      h1.defineData('id','_date','_status');
      h1.defineDone();
    end;

  set have end=_last;

  if h1.find() ne 0 then h1.add(key:id, data:id, data:date, data:status);
  else
    do;
      if _date&amp;lt;date then h1.replace(key:id, data:id, data:date, data:status);
    end;
  
  if _last then h1.output(dataset:'want(rename=(_date=date _status=status) compress=binary)');

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Mar 2023 02:37:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865342#M341738</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-03-21T02:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Select Row with the max Value per group-Find efficient program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865404#M341758</link>
      <description>&lt;P&gt;Same code with Patrick.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Since you only care about three variables,
hope you have memory big enough to run Hash Table.
*/
data have;
Input ID date : date9. status;
format date date9.;
cards;
1 19MAR2023 1
1 17MAR2023 0
1 18MAR2023 0
1 15MAR2023 1
1 14MAR2023 0
1 12MAR2023 0
2 11MAR2023 0
2 13MAR2023 0
2 15MAR2023 0
2 14MAR2023 0
2 12MAR2023 0
3 18MAR2023 1
3 17MAR2023 0
;
Run;

data _null_;
if _n_=1 then do;
 if 0 then set have;
 declare hash h(hashexp:20);
 h.definekey('id');
 h.definedata('id','date','status');
 h.definedone();
end;
set have end=last;
_date=date;_status=status;
if h.find()=0 then do;if _date&amp;gt;date then h.replace(key:id,data:id,data:_date,data:_status);end;
 else h.add();
if last then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Mar 2023 11:43:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-Row-with-the-max-Value-per-group-Find-efficient-program/m-p/865404#M341758</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-03-21T11:43:23Z</dc:date>
    </item>
  </channel>
</rss>

