<?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 one row by group where date=max(date) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890308#M351789</link>
    <description>So what solution do you suggest?</description>
    <pubDate>Mon, 21 Aug 2023 22:03:34 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2023-08-21T22:03:34Z</dc:date>
    <item>
      <title>select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889084#M351240</link>
      <description>&lt;P&gt;Hello friends,&lt;/P&gt;
&lt;P&gt;I have very big data set with many columns.&lt;/P&gt;
&lt;P&gt;Actually only 3 columns are important for me -id,date,LoanBalance&lt;/P&gt;
&lt;P&gt;The task is to create a new data set that contain for each customer ID only one row.&lt;/P&gt;
&lt;P&gt;The chosen row for each ID should be the row with max date.&lt;/P&gt;
&lt;P&gt;This code is not efficient and running very long time and even didn't finish.&lt;/P&gt;
&lt;P&gt;My question- What is most efficient way to do it?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*select one row per group based on max value*/
/*select one row per group based on max value*/
/*select one row per group based on max value*/
/*select one row per group based on max value*/
Data have;
input id date : date9. LoanBalance;
format date date9.;
cards;
1 01JUN2023 1000
1 02JUn2023 980
1 03JUN2023 950
1 04JUN2023 920
2 01JUN2023 3000
2 02JUn2023 2800
2 03JUN2023 0
;
Run;

Proc SQL;
Create table want as
select id,date,LoanBalance
from have
group by id
having date=max(date)
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Aug 2023 05:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889084#M351240</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-08-13T05:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889085#M351241</link>
      <description>&lt;PRE&gt;Proc sort data=have;
   by id date;
run;

data want;
   set have;
   by id;
   if last.id;
run;&lt;/PRE&gt;
&lt;P&gt;The summary functions in a HAVING clause do have a certain amount of overhead.&lt;/P&gt;
&lt;P&gt;The above code may take time to sort. May subset to reduce the number of variables to reduce disk space and read/write times if there very many more.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Aug 2023 06:43:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889085#M351241</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-13T06:43:32Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889088#M351244</link>
      <description>&lt;P&gt;To speed up the process, reduce data horizontally in the first step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort
  data=have (keep=id date loanbalance)
  out=intermediate
;
by id date;
run;

data want;
set intermediate;
by id;
if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Aug 2023 07:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889088#M351244</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-13T07:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889127#M351264</link>
      <description>&lt;P&gt;Since the data are already sorted by ID, you can pass through each ID twice: first pass to find the maximum value, the second pass to output the instance that match that maximum value.&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. LoanBalance;
  format date date9.;
cards;
1 01JUN2023 1000
1 02JUn2023 980
1 03JUN2023 950
1 04JUN2023 920
2 01JUN2023 3000
2 02JUn2023 2800
2 03JUN2023 0
run;

data want (drop=_:);
  set have (in=firstpass)  have (in=secondpass);
  by id;
  retain _max;
  if first.id then _max=loanbalance;
  else _max=max(_max,loanbalance);
  if secondpass and loanbalance=_max;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes there is only one instance of a maximum value.&amp;nbsp; If not, and you still want only one instance per id, then a rule is needed to choose among the tied observations.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Aug 2023 01:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/889127#M351264</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-14T01:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890146#M351726</link>
      <description>Thanks but in real data is not sorted, what solution do you offer?</description>
      <pubDate>Mon, 21 Aug 2023 05:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890146#M351726</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-08-21T05:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890147#M351727</link>
      <description>&lt;P&gt;II run your code and got error&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want (drop=_:);
set TeraData.VBM410_ACTIVE_LOAN_BAL (in=firstpass keep=Agreement_Account_Id Agreement_Summary_Date Fund_Actual_Payment_Count)
TeraData.VBM410_ACTIVE_LOAN_BAL (in=secondpass keep=Agreement_Account_Id Agreement_Summary_Date Fund_Actual_Payment_Count);
by Agreement_Account_Id;
retain _max;
if first.Agreement_Account_Id then _max=Fund_Actual_Payment_Count;
else _max=max(_max,Fund_Actual_Payment_Count);
if secondpass and Fund_Actual_Payment_Count=_max;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here error log&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1                                                          The SAS System                              08:15 Monday, August 21, 2023

1          ;*';*";*/;quit;run;
2          OPTIONS PAGENO=MIN;
3          %LET _CLIENTTASKLABEL='Program';
4          %LET _CLIENTPROCESSFLOWNAME='Process Flow';
5          %LET _CLIENTPROJECTPATH='';
6          %LET _CLIENTPROJECTPATHHOST='';
7          %LET _CLIENTPROJECTNAME='';
8          %LET _SASPROGRAMFILE='';
9          %LET _SASPROGRAMFILEHOST='';
10         
11         ODS _ALL_ CLOSE;
12         OPTIONS DEV=PNG;
13         GOPTIONS XPIXELS=0 YPIXELS=0;
14         FILENAME EGSR TEMP;
15         ODS tagsets.sasreport13(ID=EGSR) FILE=EGSR
16             STYLE=HTMLBlue
17             STYLESHEET=(URL="file:///C:/Program%20Files/SASHome/SASEnterpriseGuide/7.1/Styles/HTMLBlue.css")
18             NOGTITLE
19             NOGFOOTNOTE
20             GPATH=&amp;amp;sasworklocation
21             ENCODING=UTF8
22             options(rolap="on")
23         ;
NOTE: Writing TAGSETS.SASREPORT13(EGSR) Body file: EGSR
24         
25         GOPTIONS ACCESSIBLE;
26         data want (drop=_:);
27         set TeraData.VBM410_ACTIVE_LOAN_BAL (in=firstpass keep=Agreement_Account_Id Agreement_Summary_Date
27       ! Fund_Actual_Payment_Count)
28         TeraData.VBM410_ACTIVE_LOAN_BAL (in=secondpass keep=Agreement_Account_Id Agreement_Summary_Date
28       ! Fund_Actual_Payment_Count);
29         by Agreement_Account_Id;
30         retain _max;
31         if first.Agreement_Account_Id then _max=Fund_Actual_Payment_Count;
32         else _max=max(_max,Fund_Actual_Payment_Count);
33         if secondpass and Fund_Actual_Payment_Count=_max;
34         run;

NOTE: Compression was disabled for data set WORK.WANT because compression overhead would increase the size of the data set.
ERROR: Teradata row not delivered (trget): CLI2: REQEXHAUST(307): Request data exhausted. .
NOTE: The DATA step has been abnormally terminated.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 258289 observations read from the data set TERADATA.VBM410_ACTIVE_LOAN_BAL.
NOTE: There were 258285 observations read from the data set TERADATA.VBM410_ACTIVE_LOAN_BAL.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 123804 observations and 3 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           1:31.11
      user cpu time       0.41 seconds
      system cpu time     0.01 seconds
      memory              2027.40k
      OS Memory           26544.00k
      Timestamp           08/21/2023 08:18:32 AM
      Step Count                        4  Switch Count  47
      Page Faults                       0
      Page Reclaims                     560
      Page Swaps                        0
      Voluntary Context Switches        502
2                                                          The SAS System                              08:15 Monday, August 21, 2023

      Involuntary Context Switches      5
      Block Input Operations            0
      Block Output Operations           0
      

35         
36         GOPTIONS NOACCESSIBLE;
37         %LET _CLIENTTASKLABEL=;
38         %LET _CLIENTPROCESSFLOWNAME=;
39         %LET _CLIENTPROJECTPATH=;
40         %LET _CLIENTPROJECTPATHHOST=;
41         %LET _CLIENTPROJECTNAME=;
42         %LET _SASPROGRAMFILE=;
43         %LET _SASPROGRAMFILEHOST=;
44         
45         ;*';*";*/;quit;run;
46         ODS _ALL_ CLOSE;
47         
48         
49         QUIT; RUN;
50         
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2023 05:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890147#M351727</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-08-21T05:39:02Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890150#M351728</link>
      <description>&lt;P&gt;You seem to have run into a limit in Teradata.&lt;/P&gt;
&lt;PRE&gt;ERROR: Teradata row not delivered (trget): CLI2: REQEXHAUST(307): Request data exhausted.&lt;/PRE&gt;
&lt;P&gt;Check with your Teradata admins why this happens.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2023 06:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890150#M351728</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-21T06:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890154#M351730</link>
      <description>&lt;P&gt;Access to data in external databases is a factor in planning programming. You didn't mention that the data was on a Teradata server until getting an error.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2023 07:12:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890154#M351730</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-21T07:12:28Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890157#M351732</link>
      <description>&lt;P&gt;That your source table resides in a database is significant and something you should have told us from start.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a data step solution will likely pull all the data first to the SAS side before sub-setting it. And that's likely not efficient. I'm also not sure if Teradata will return the rows sorted only because you've got a BY &amp;lt;variable&amp;gt; in your data step.&lt;/P&gt;
&lt;P&gt;Googling the error you've got returned &lt;A href="https://support.teradata.com/knowledge?id=kb_article_view&amp;amp;sys_kb_id=f15bd0f71b973f00682ca8233a4bcbc2" target="_self"&gt;this Teradata knowledge base article&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would have expected that your initial Proc SQL would be right code to do things. Is that the exact code that causes the bad performance?&lt;/P&gt;
&lt;P&gt;Eventually add the following options prior to the SQL and share the SAS log with us that shows what actually gets executed on the database side.&lt;/P&gt;
&lt;P&gt;options sastrace=',,,d' sastraceloc=saslog nostsuffix;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2023 08:25:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890157#M351732</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-21T08:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890308#M351789</link>
      <description>So what solution do you suggest?</description>
      <pubDate>Mon, 21 Aug 2023 22:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890308#M351789</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-08-21T22:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890309#M351790</link>
      <description>May you please write more clearly what code should i run in order to help the experts suggest solution?</description>
      <pubDate>Mon, 21 Aug 2023 22:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890309#M351790</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-08-21T22:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890319#M351794</link>
      <description>&lt;P&gt;Ask your Teradata system admins for an efficient query .&lt;/P&gt;
&lt;P&gt;I doubt that SAS can pass your original query into Teradata as I don't think it will allow the use of aggregate functions in a HAVING clause the way that PROC SQL does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might be able to use the advanced SQL syntax of "windowing" functions to create the Teradata SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you already have a libref pointing to your TERADATA database you can use that in PROC SQL to send pass thru SQL into Teradata.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
connect to teradata;
create table want as
  select
  Agreement_Account_Id
, Agreement_Summary_Date
, Fund_Actual_Payment_Count
 from connection to teradata
(select 
  Agreement_Account_Id
, Agreement_Summary_Date
, Fund_Actual_Payment_Count
from SCHEMA_NAME.VBM410_ACTIVE_LOAN_BAL
where 1=row_number() partition by Agreement_Account_Id order by Fund_Actual_Payment_Count desc
  );
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 02:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890319#M351794</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-22T02:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: select one row by group where date=max(date)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890327#M351800</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;I feel my comments were clear. But Tom provided now actual SQL passthrough code that you could try.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Aug 2023 04:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-one-row-by-group-where-date-max-date/m-p/890327#M351800</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-22T04:40:03Z</dc:date>
    </item>
  </channel>
</rss>

