<?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 Using hash  to select last row by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-hash-to-select-last-row-by-group/m-p/893073#M352781</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Here is Hash code to select Last Row by group.&lt;/P&gt;
&lt;P&gt;I want to ask-&lt;/P&gt;
&lt;P&gt;Lets say there there is another data set called List_tbl and I want to select last row by group only for the groups in data set List_Tbl (So in this example I want to perform it only for groups 'A','C','D')&lt;/P&gt;
&lt;P&gt;What is the way to tell SAS to perform it only for the groups in list_tbl?&lt;/P&gt;
&lt;P&gt;Please note that in real life List_Tbl will have 200,000 groups and&amp;nbsp; have table will have 5 millions groups.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input ID $ date : date9. var;
format date date9.;
cards;
A 01JAN2023 10
B 05JAN2023 5
B 06JAN2023 6
C 15JAN2023 7
C 16JAN2023 8
C 19JAN2023 9
C 20JAN2023 10
D 24JAN2023 2
;
Run;

Data List_tbl;
input ID $;
cards;
A
C
D
;
Run;

data _null_;
dcl hash H (dataset:'have',ordered: "a",duplicate:'r') ;
h.definekey  ("id") ;
h.definedata ("id","date","var") ;/***Var to show in wanted data set**/
h.definedone () ;
h.output(dataset:'want');
stop;
retain _n_   _iorc_ ;
if 0 then do;
set have;
output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 07 Sep 2023 05:35:26 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2023-09-07T05:35:26Z</dc:date>
    <item>
      <title>Using hash  to select last row by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-hash-to-select-last-row-by-group/m-p/893073#M352781</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Here is Hash code to select Last Row by group.&lt;/P&gt;
&lt;P&gt;I want to ask-&lt;/P&gt;
&lt;P&gt;Lets say there there is another data set called List_tbl and I want to select last row by group only for the groups in data set List_Tbl (So in this example I want to perform it only for groups 'A','C','D')&lt;/P&gt;
&lt;P&gt;What is the way to tell SAS to perform it only for the groups in list_tbl?&lt;/P&gt;
&lt;P&gt;Please note that in real life List_Tbl will have 200,000 groups and&amp;nbsp; have table will have 5 millions groups.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input ID $ date : date9. var;
format date date9.;
cards;
A 01JAN2023 10
B 05JAN2023 5
B 06JAN2023 6
C 15JAN2023 7
C 16JAN2023 8
C 19JAN2023 9
C 20JAN2023 10
D 24JAN2023 2
;
Run;

Data List_tbl;
input ID $;
cards;
A
C
D
;
Run;

data _null_;
dcl hash H (dataset:'have',ordered: "a",duplicate:'r') ;
h.definekey  ("id") ;
h.definedata ("id","date","var") ;/***Var to show in wanted data set**/
h.definedone () ;
h.output(dataset:'want');
stop;
retain _n_   _iorc_ ;
if 0 then do;
set have;
output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Sep 2023 05:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-hash-to-select-last-row-by-group/m-p/893073#M352781</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-09-07T05:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using hash  to select last row by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-hash-to-select-last-row-by-group/m-p/893074#M352782</link>
      <description>&lt;P&gt;How about that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ date : date9. var;
format date date9.;
cards;
A 01JAN2023 10
B 05JAN2023 5
B 06JAN2023 6
C 15JAN2023 7
C 16JAN2023 8
C 19JAN2023 9
C 20JAN2023 10
D 24JAN2023 2
;
Run;

Data List_tbl;
input ID $;
cards;
A
C
D
;
Run;


/* sort have */
proc sort data = have;
  by ID date;
Run;

data WANT;

  /* use List_tbl as a dictionary */
  dcl hash H (dataset:'List_tbl');
  h.definekey  ("id") ;
  h.definedone () ;


  do until(END);
    set have end=END;
    by ID;
    /* check if it is the first row and is in the dictionary */
    if first.ID AND H.find()=0 then output;
  end;

  stop;
run;

proc print data = WANT;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2023 05:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-hash-to-select-last-row-by-group/m-p/893074#M352782</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-07T05:49:58Z</dc:date>
    </item>
  </channel>
</rss>

