<?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: How to use hash to find the minimum value and minimum date in groups? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675390#M203492</link>
    <description>Amazing!Thanks a lot! &lt;BR /&gt;The process is a little difficult for me to understand.&lt;BR /&gt;I‘m not good at using hiter and I can't understand "_N_ = hi.next();_N_ = hi.prev();“ in your code.Can you give me a hint？</description>
    <pubDate>Sat, 08 Aug 2020 14:13:00 GMT</pubDate>
    <dc:creator>Lee_wan</dc:creator>
    <dc:date>2020-08-08T14:13:00Z</dc:date>
    <item>
      <title>How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675357#M203464</link>
      <description>&lt;P&gt;Dear all，&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I have：&lt;/P&gt;
&lt;PRE&gt;data have;
input subject $ seq aval date $10. @@;
datalines;
A 1 3 2020-01-01
A 2 4 2020-01-04
A 3 3 2020-03-01
A 4 5 2020-04-01
B 1 4 2020-04-05
B 2 1 2020-03-03
B 3 2 2020-01-03
B 4 1 2020-02-03
C 2 4 2020-02-04
C 1 4 2020-02-03
;&lt;/PRE&gt;
&lt;P&gt;And what I want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lee_wan_0-1596863522432.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/48069i49BA02640F606089/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Lee_wan_0-1596863522432.png" alt="Lee_wan_0-1596863522432.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;And my code please see as after:&lt;/P&gt;
&lt;PRE&gt;data a;
  if _N_=1 then do;
    dcl hash h();
	h.definekey('subject','seq');
	h.definedata('min');
	h.definedone();
	call missing(min);
  end;

  do _N_=1 by 1 until(last.subject);
    set have;
	by subject;
	if first.subject or .&amp;lt;aval&amp;lt;min then h.replace();
	min=min(min,aval);
  end;

  do until(last.subject);
    set have;
	by subject;
    if h.check()=0 and aval=min then output;;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Running results are not what I want，and how to&amp;nbsp;modify the code in order to put the smallest date record into hash and output in the second DOW?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 05:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675357#M203464</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-08T05:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675362#M203468</link>
      <description>&lt;P&gt;You only need one DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (
  keep=subject _seq _aval _date
  rename=(_seq=seq _aval=aval _date=date)
);
_aval = 1e200;
do until (last.subject);
  set have;
  by subject;
  if aval &amp;lt; _aval
  then do;
    _aval = aval;
    _seq = seq;
    _date = date;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 06:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675362#M203468</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-08T06:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675364#M203470</link>
      <description>&lt;P&gt;Thanks for your reply, but the result is not I want. The date is not right.&amp;nbsp;I want to take the earliest date with the lowest aval at the same time.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 06:29:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675364#M203470</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-08T06:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675368#M203474</link>
      <description>&lt;P&gt;Slightly expand the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (
  keep=subject _seq _aval _date
  rename=(_seq=seq _aval=aval _date=date)
);
_aval = 1e200;
do until (last.subject);
  set have;
  by subject;
  if aval &amp;lt; _aval
  then do;
    _aval = aval;
    _seq = seq;
    _date = date;
  end;
  else if aval = _aval and date &amp;lt; _date
  then do; 
    _date = date;
    _seq = seq;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Aug 2020 06:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675368#M203474</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-08T06:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675372#M203478</link>
      <description>&lt;P&gt;If you really want to use the Hash Object to find the min values by group, here is an approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject $ seq aval date :yymmdd10.;
format date yymmdd10.;
datalines;
A 1 3 2020-01-01
A 2 4 2020-01-04
A 3 3 2020-03-01
A 4 5 2020-04-01
B 1 4 2020-04-05
B 2 1 2020-03-03
B 3 2 2020-01-03
B 4 1 2020-02-03
C 2 4 2020-02-04
C 1 4 2020-02-03
;

data want;
   if _N_ = 1 then do;
      declare hash h (dataset : 'have(obs=0)', ordered : 'A', multidata : 'Y');
      h.definekey ('aval', 'date');
      h.definedata (all : 'Y');
      h.definedone();
      declare hiter hi ('h');
   end;
 
   do until (last.subject);
      set have;
      by subject;
      h.add();
   end;
 
   _N_ = hi.next();
   _N_ = hi.prev();
   h.clear(); 
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;subject seq aval date 
A       1   3    2020-01-01 
B       4   1    2020-02-03 
C       1   4    2020-02-03 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 09:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675372#M203478</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-08T09:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675378#M203481</link>
      <description>&lt;P&gt;Just for fun, a hash of hashes approach if you have unsorted data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject $ seq aval date :yymmdd10.;
format date yymmdd10.;
datalines;
A 1 3 2020-01-01
B 3 2 2020-01-03
A 2 4 2020-01-04
C 2 4 2020-02-04
A 3 3 2020-03-01
B 4 1 2020-02-03
A 4 5 2020-04-01
B 1 4 2020-04-05
C 1 4 2020-02-03
B 2 1 2020-03-03
;

data want;
   dcl hash HoH(ordered : 'A');
   HoH.definekey('subject');
   HoH.definedata('h', 'hi', 'subject');
   HoH.definedone();
   dcl hiter HoHiter('HoH');

   do until (lr);
      set have end=lr;

      if HoH.find() ne 0 then do;
         dcl hash h(dataset : 'have(obs=0)', multidata : 'Y', ordered : 'A');
         h.definekey('aval', 'date');
         h.definedata(all : 'Y');
         h.definedone();
         dcl hiter hi('h');
         HoH.add();
      end;

      h.add();
   end;

   do while(HoHiter.next() = 0);
      _N_ = hi.next();
      _N_ = hi.prev();
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;subject seq aval date 
A       1   3    2020-01-01 
B       4   1    2020-02-03 
C       1   4    2020-02-03 &lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Aug 2020 12:02:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675378#M203481</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-08T12:02:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675387#M203489</link>
      <description>&lt;P&gt;Do you really have to use Hash Table ? Why not PROC SORT ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subject $ seq aval date $10. @@;
datalines;
A 1 3 2020-01-01
A 2 4 2020-01-04
A 3 3 2020-03-01
A 4 5 2020-04-01
B 1 4 2020-04-05
B 2 1 2020-03-03
B 3 2 2020-01-03
B 4 1 2020-02-03
C 2 4 2020-02-04
C 1 4 2020-02-03
;
data a;
    dcl hash h(dataset:'have(obs=0)',ordered:'a');
	dcl hiter hi('h');
	h.definekey('aval','date');
	h.definedata(all:'y');
	h.definedone();
  do until(last.subject);
    set have;
	by subject;
	h.ref();
  end;
  hi.first(); output;
  h.delete(); hi.delete();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Aug 2020 13:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675387#M203489</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-08-08T13:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675389#M203491</link>
      <description>&lt;PRE&gt;data have;
input subject $ seq aval date $10. @@;
datalines;
A 1 3 2020-01-01
A 2 4 2020-01-04
A 3 3 2020-03-01
A 4 5 2020-04-01
B 1 4 2020-04-05
B 2 1 2020-03-03
B 3 2 2020-01-03
B 4 1 2020-02-03
C 2 4 2020-02-04
C 1 4 2020-02-03
;
data a;
  if _N_=1 then do;
    dcl hash h(dataset:'have(obs=0)',ordered:'d');
	dcl hiter hi('h');
	h.definekey('aval','date');
	h.definedata(all:'y');
	h.definedone();
  end;
  do until(last.subject);
    set have;
	by subject;
	h.ref();
  end;
  hi.last(); output;
  rc=hi.next();h.clear();
  drop rc;
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 08 Aug 2020 13:53:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675389#M203491</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-08-08T13:53:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675390#M203492</link>
      <description>Amazing!Thanks a lot! &lt;BR /&gt;The process is a little difficult for me to understand.&lt;BR /&gt;I‘m not good at using hiter and I can't understand "_N_ = hi.next();_N_ = hi.prev();“ in your code.Can you give me a hint？</description>
      <pubDate>Sat, 08 Aug 2020 14:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675390#M203492</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-08T14:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to use hash to find the minimum value and minimum date in groups?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675411#M203506</link>
      <description>Can I use rc=h.first() to replace _N_ = hi.next();?&lt;BR /&gt;And why we can't delete the code for "_N_ = hi.prev();"? what dose this code do?&lt;BR /&gt;Thanks.</description>
      <pubDate>Sat, 08 Aug 2020 16:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-hash-to-find-the-minimum-value-and-minimum-date-in/m-p/675411#M203506</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-08T16:58:25Z</dc:date>
    </item>
  </channel>
</rss>

