<?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 Hash Sort Ascending and Descending in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813753#M321191</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do we have an equivalent hash code for :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=test;by id descending sub;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to see how we could define multiple keys ordered mixed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Mushy&lt;/P&gt;</description>
    <pubDate>Tue, 17 May 2022 11:11:08 GMT</pubDate>
    <dc:creator>Mushy</dc:creator>
    <dc:date>2022-05-17T11:11:08Z</dc:date>
    <item>
      <title>Hash Sort Ascending and Descending</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813753#M321191</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do we have an equivalent hash code for :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=test;by id descending sub;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to see how we could define multiple keys ordered mixed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Mushy&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2022 11:11:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813753#M321191</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2022-05-17T11:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Sort Ascending and Descending</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813763#M321199</link>
      <description>&lt;P&gt;No unfortunately it's not possible. For multiple keys, they will either be all sorted ascending - or descending when you use the Ordered : Argument Tag.&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2022 11:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813763#M321199</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-05-17T11:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Sort Ascending and Descending</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813841#M321247</link>
      <description>&lt;P&gt;There's no direct analog to "by x y descending z" in hash object storage.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can replicate it by use of hash-of-hashes.&amp;nbsp; Here's a made-up example that replicates BY MAKE DESCENDING WEIGHT for sashelp.cars:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.cars out=cars;
  by descending make;
run;

data want (drop=_:);
  if 0 then set cars;
  declare hash h ;
  declare hiter hi ;
  declare hash hoh (ordered:'A');
    hoh.definekey('make');
    hoh.definedata('make','h','hi');
    hoh.definedone();
  declare hiter hohi ('hoh');


  do until (end_of_cars);
    set cars end=end_of_cars;
    if hoh.find() ^=0 then do;
      h=_new_ hash(dataset:'cars (obs=0)',ordered:'D',multidata:'Y');
      h.definekey('weight');
      h.definedata(all:'Y');
      h.definedone();
      hi=_new_ hiter('h');
      hoh.add();
    end;
    h.add();
  end;

  do _i=hohi.first() by 0 until (hohi.next()^=0);
    do _j=hi.first() by 0 until (hi.next()^=0);
      output;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the location of the "OUTPUT" statement is where you might also do programming that depends on the reordered sequence.&amp;nbsp; For instance if you wanted change of weight from one obs to the next:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  call missing(of _all_);
  do _i=hohi.first() by 0 until (hohi.next()^=0);
    do _j=hi.first() by 0 until (hi.next()^=0);
      weight_change=dif(weight);
      if make^=lag(make) then weight_change=.;
      output;
    end;
  end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I preceded this with the CALL MISSING(of _ALL_), which wasn't essential in this particular example, but is a good idea for properly replicating reading the first record in a SET .... BY MAKE DESCENDING WEIGHT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This could be definitely by greatly simplified by a sas implementation of something like "ORDERED:(MAKE descending WEIGHT)" - there would be no need for coding a hash-of-hashes solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2022 15:22:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813841#M321247</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-17T15:22:56Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Sort Ascending and Descending</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813850#M321251</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393424"&gt;@Mushy&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively you can create one or more replacement keys whose sort order reverses the sort order of the corresponding original keys. For example, use &lt;FONT face="courier new,courier"&gt;-sub&lt;/FONT&gt;&amp;nbsp;for a numeric variable &lt;FONT face="courier new,courier"&gt;sub&lt;/FONT&gt;&amp;nbsp;or&lt;/P&gt;
&lt;PRE&gt;-input(put(sub,$hex6.),hex6.)&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;for a character variable &lt;FONT face="courier new,courier"&gt;sub&lt;/FONT&gt; of length 3. (For character variables of arbitrary lengths you could replace each character &lt;FONT face="courier new,courier"&gt;c&lt;/FONT&gt; by&amp;nbsp;&lt;FONT face="courier new,courier"&gt;byte(255-rank(c))&lt;/FONT&gt;.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example using &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461" target="_blank" rel="noopener"&gt;&lt;FONT color="#3366FF"&gt;mkeintz&lt;/FONT&gt;&lt;/A&gt;'s dataset CARS:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select quote(trim(name)) into :varlist separated by ','
from dictionary.columns
where libname='WORK' &amp;amp; memname='CARS';
quit;

data _null_;
if _n_=1 then do;
  dcl hash h(ordered:'a', multidata:'y');
  h.definekey('make','_w');
  h.definedata(&amp;amp;varlist);
  h.definedone();
end;
set cars end=last;
_w=-weight;
h.add();
if last then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 May 2022 17:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Sort-Ascending-and-Descending/m-p/813850#M321251</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-05-17T17:47:41Z</dc:date>
    </item>
  </channel>
</rss>

