<?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: Sort only part of a  _temporary_ Array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915899#M360842</link>
    <description>&lt;P&gt;Hi! The hash solution works nicely in SAS ODA. /Br Anders&lt;/P&gt;</description>
    <pubDate>Tue, 13 Feb 2024 18:26:19 GMT</pubDate>
    <dc:creator>AndersS</dc:creator>
    <dc:date>2024-02-13T18:26:19Z</dc:date>
    <item>
      <title>Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915314#M360652</link>
      <description>&lt;P&gt;Hi! In the Data-step we can use&amp;nbsp; CALL SORTN( of&amp;nbsp; &amp;nbsp;ARRN(*));&amp;nbsp; to sort all elements in the array ARRN in increasing order. It works fine for both ordinary arrays and _temporary_ arrays.&lt;BR /&gt;&lt;BR /&gt;If the array is not _temporary_ then we can specify the variable names,&amp;nbsp; like&amp;nbsp;CALL SORTN( of ARRN3-ARRN10); to sort only elements 3 to 10. (Note that it is&amp;nbsp;not possible to specify the array element names.&amp;nbsp;&amp;nbsp;CALL SORTN( of ARRN(3) -ARRN(10));&amp;nbsp; &amp;nbsp;gives ERROR!)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question: Is it possible to sort only PART of the _temporary_ array&amp;nbsp; in some way. This can be useful.&lt;BR /&gt;I have tried a number of different ways, but it did not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;_Temporary_ arrays are VERY much faster than ordinary arrays. (A way of speeding up ordinary arrays is NOT to include them in the output data set. i.e. they are not included in the file buffer).&lt;BR /&gt;Many thanks in advance!&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;/Br Anders&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2024 18:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915314#M360652</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-09T18:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915349#M360666</link>
      <description>&lt;P&gt;You cannot use a SAS-Variable-List to reference elements of a temporary array, because they are not variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can sort elements of a temporary array if you reference them properly.&amp;nbsp; array-name[subscript].&amp;nbsp; I don't know if my example is adequate but seems reasonable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;48         data _null_;
49            array a[5] _temporary_ (1 1 4 3 2);
50            do i = 1 to dim(a); put (a[i])(=); end;
51            put ' ';
52            call sortn(a[3],a[4],a[5]);
53            do i = 1 to dim(a); put (a[i])(=); end;
54            run;

a[1]=1
a[2]=1
a[3]=4
a[4]=3
a[5]=2

a[1]=1
a[2]=1
a[3]=2
a[4]=3
a[5]=4&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2024 22:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915349#M360666</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2024-02-09T22:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915376#M360679</link>
      <description>&lt;P&gt;Just code it on your own ,if you have too many members in _temporary_ array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
array a[10] _temporary_ (1 1 4 3 2   5 4 3 2 1);
/*sort the member of arrary from 6th to 10th*/
do i=6 to 10;
 do j=i+1 to 10;
  if a{i}&amp;gt;a{j} then do;temp=a{i};a{i}=a{j};a{j}=temp; end;
 end;
end;
drop i j temp;

/*print this array*/
do i=1 to 10;
 put a{i}= ;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;344  data _null_;
345  array a[10] _temporary_ (1 1 4 3 2   5 4 3 2 1);
346  /*sort the member of arrary from 6th to 10th*/
347  do i=6 to 10;
348   do j=i+1 to 10;
349    if a{i}&amp;gt;a{j} then do;temp=a{i};a{i}=a{j};a{j}=temp; end;
350   end;
351  end;
352  drop i j temp;
353
354  /*print this array*/
355  do i=1 to 10;
356   put a{i}= ;
357  end;
358  run;

a[1]=1
a[2]=1
a[3]=4
a[4]=3
a[5]=2
a[6]=1
a[7]=2
a[8]=3
a[9]=4
a[10]=5
NOTE: “DATA 语句”所用时间（总处理时间）:
      实际时间          0.03 秒
      CPU 时间          0.00 秒




&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Feb 2024 02:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915376#M360679</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-02-10T02:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915384#M360682</link>
      <description>&lt;P&gt;Hi! It works for a few elements. But I have several thousand. /Br Anders&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 09:42:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915384#M360682</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-10T09:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915385#M360683</link>
      <description>&lt;P&gt;Hi! This is a version of the classical "Bubble sort". Yes it works. /Br Anders&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 09:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915385#M360683</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-10T09:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915386#M360684</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10552"&gt;@AndersS&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10552"&gt;@AndersS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi! It works for a few elements. But I have several thousand. /Br Anders&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This should not be an obstacle. You can build the list for the argument of CALL SORTN&amp;nbsp;programmatically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create two lists of array elements */

data _null_;
length g1 g2 $80;
do i=37 to 45;
  g1=catx(',',g1,cat('a[',i,']'));
end;
call symputx('group1',g1);
do i=5 to 77 by 9;
  g2=catx(',',g2,cat('a[',i,']'));
end;
call symputx('group2',g2);
run;

/* Sort those parts of a temporary array */

data _null_;
array a[81] _temporary_;
do i=1 to dim(a);
  a[i]=82-i;
end;
call sortn(&amp;amp;group1);
call sortn(&amp;amp;group2);
do i=1 to dim(a);
  put a[i] 3. @;
  if ~mod(i,9) then put;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt; 81 80 79 78 &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt; 5&lt;/FONT&gt;&lt;/STRONG&gt; 76 75 74 73
 72 71 70 69 &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;14&lt;/FONT&gt;&lt;/STRONG&gt; 67 66 65 64
 63 62 61 60 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;23&lt;/STRONG&gt;&lt;/FONT&gt; 58 57 56 55
 54 53 52 51 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;32&lt;/STRONG&gt;&lt;/FONT&gt; 49 48 47 46
 &lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;37 38 39 40 &lt;FONT color="#800080"&gt;41&lt;/FONT&gt; 42 43 44 45&lt;/STRONG&gt;&lt;/FONT&gt;
 36 35 34 33 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;50&lt;/STRONG&gt;&lt;/FONT&gt; 31 30 29 28
 27 26 25 24 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;59&lt;/STRONG&gt;&lt;/FONT&gt; 22 21 20 19
 18 17 16 15 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;68&lt;/STRONG&gt;&lt;/FONT&gt; 13 12 11 10
  9  8  7  6 &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;77&lt;/STRONG&gt;&lt;/FONT&gt;  4  3  2  1&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Feb 2024 10:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915386#M360684</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-02-10T10:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915391#M360687</link>
      <description>&lt;P&gt;You already have a solution for your question, but just for fun one more:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* just to print arrays elements */
/* EDIT: */
/* just to print arrays elements */
%macro prArr(A,s=LBound(&amp;amp;A.),e=HBound(&amp;amp;A.),b=1,i=i);
put; do &amp;amp;i. = &amp;amp;s. to &amp;amp;e. by &amp;amp;b.; put &amp;amp;A.[i]=; end;
%mend prArr;

/* list part of array you want to sort */
%macro ArrElem(A,s,e,b=1);
%local i;
%do i = &amp;amp;s. %to &amp;amp;e. %by &amp;amp;b.; %if (&amp;amp;i. NE &amp;amp;s.) %then,; &amp;amp;A.[&amp;amp;i.] %end;
%mend ArrElem;



data _null_;
  array a[10] _temporary_ (5 4 3 2 1  5 4 3 2 1);

  %prArr(a)

  /* one part... */
  call sortn(%ArrElem(a,6,10));

  %prArr(a)
  
  /* [EDIT:] */
  /* or even two parts */
  call sortn(%ArrElem(a,1,3),%ArrElem(a,8,10));

  %prArr(a)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 12:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915391#M360687</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-10T12:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915393#M360688</link>
      <description>&lt;P&gt;One more, just for fun, extension, a bit of "syntactic sugar"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ArrElem(A,elements);
%local i n k o s e b;
%let n=%sysfunc(countw(&amp;amp;elements., %str( )));
%do k=1 %to &amp;amp;n.;
  %let o = %scan(&amp;amp;elements.,&amp;amp;k.,%str( ));
  %let s = %scan(&amp;amp;o.,1,:);
  %let e = %scan(&amp;amp;o.,2,:);
  %let b = %scan(&amp;amp;o.,3,:);
  %if &amp;amp;e.= %then %let e=&amp;amp;s.;
  %if &amp;amp;b.= %then %let b=1;
  %if (&amp;amp;k. NE 1) %then,;
  %do i = &amp;amp;s. %to &amp;amp;e. %by &amp;amp;b.; %if (&amp;amp;i. NE &amp;amp;s.) %then,; &amp;amp;A.[&amp;amp;i.] %end;
%end;
%mend ArrElem;

options mprint;
data _null_;
  array a[10] _temporary_ (5 4 3 2 1  5 4 3 2 1);

  %prArr(a)

  /* one part... */
  call sortn( %ArrElem(a,6:10) );

  %prArr(a)

  /* or even two parts */
  call sortn( %ArrElem(a,1:5:2 8:10) );

  %prArr(a)
run;


data _null_;
  array a[10] _temporary_ (50 5 40 4 30 3 20 2 10 1);

  %prArr(a)

  /* sort even and odd */
  call sortn( %ArrElem(a,1:9:2 2:10:2) );

  %prArr(a)
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 12:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915393#M360688</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-10T12:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915399#M360692</link>
      <description>&lt;P&gt;Hi! VERY interesting solution. I can not use it, but quite good!&amp;nbsp; /Br Anders&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 14:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915399#M360692</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-10T14:23:25Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915400#M360693</link>
      <description>&lt;P&gt;Hi! VERY interesting solution by Bart.&amp;nbsp; Yes it works. I have just tested it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I&amp;nbsp; dont have any use if ths solution just now.&lt;/P&gt;
&lt;P&gt;But I think that these discussions are REALLY GREAT!&lt;/P&gt;
&lt;P&gt;/Br Anders&lt;/P&gt;</description>
      <pubDate>Sat, 10 Feb 2024 14:32:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915400#M360693</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-10T14:32:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915669#M360784</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10552"&gt;@AndersS&lt;/a&gt;&amp;nbsp;, I see you found your answer.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I will chime in and refer you to the article&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi26/p096-26.pdf" target="_self"&gt;QuickSorting An Array&lt;/A&gt;&amp;nbsp;by Paul Dorfman (aka the&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The %Qsort macro takes an array and sorts it using the Quicksort algorithm. Also, the macro takes the optional parameters lb and hb, so you can easily sort elements 3 to 10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I recommend reading the entire article. Pure gold &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Br&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2024 20:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915669#M360784</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-02-12T20:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915674#M360786</link>
      <description>Hi! Many thanks!&lt;BR /&gt;&lt;BR /&gt;BUT, %QSORT does NOT work in SAS ODA.&lt;BR /&gt;&lt;BR /&gt;Solution: Find the code for %QSORT and include it.&lt;BR /&gt;&lt;BR /&gt;Perhaps later on I will do that.&lt;BR /&gt;&lt;BR /&gt;CALL SORTN is rather good. Good enough now for my needs.&lt;BR /&gt;/Br Anders&lt;BR /&gt;&lt;BR /&gt;Anders Sköllermo  fil.dr.,  Sandgränd 13,  178 40 Ekerö,  073-5077373,&lt;BR /&gt;&lt;ANDERS.SKOLLERMO&gt; anders.skollermo@one.se&lt;BR /&gt;&lt;/ANDERS.SKOLLERMO&gt;</description>
      <pubDate>Mon, 12 Feb 2024 20:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915674#M360786</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-12T20:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915704#M360789</link>
      <description>&lt;P&gt;100% agree, pure gold! Like all from the man.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2024 21:35:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915704#M360789</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-12T21:35:30Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915709#M360791</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;(Perhaps stupid question)&lt;/P&gt;
&lt;P&gt;Is the SAS code for the macro %QSORT avaiable, as SAS code than can be included directly in the Data-step?&lt;/P&gt;
&lt;P&gt;I am using SAS ODA .&lt;BR /&gt;I do not know&amp;nbsp;proc fcmp.&amp;nbsp;(Would be nice to learn, but I am writing a paper, and do not have the time).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does anyone have a good version of&amp;nbsp; %QSORT, that I can download?&lt;/P&gt;
&lt;P&gt;MANY THANKS in advance!&lt;/P&gt;
&lt;P&gt;/Br Anders&lt;/P&gt;
&lt;P&gt;(Age and IQ are 76+. Trying to understand quantile confidence interval estimates).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2024 21:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915709#M360791</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-12T21:47:07Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915735#M360796</link>
      <description>&lt;P&gt;Hi, Anders:&lt;/P&gt;
&lt;P&gt;You can copy the entire QSORT macro directly from the pdf paper Peter Clemmensen has linked, then use it as described in the (pretty extensive) comments in the header.&amp;nbsp;Another advantages of using qsort or another method not based on SORTN/C is that you can also sort descending and control what to do with duplicates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't like to use QSORT for any reason, you can use the hash object to do what you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                                                                                                                                                                                                                   
  retain lb 3 hb 8 order "a" ; * use d for descending ;                                                                                                                                                                                                       
  array arr [10] (2 10 444 333 111 888 777 666 4 5) ;                                                                                                                                                                                                          
  if _n_ = 1 then do ;                                                                                                                                                                                                                                          
    dcl hash h (ordered:order, multidata:"y") ;                                                                                                                                                                                                                 
    h.definekey ("k") ;                                                                                                                                                                                                                                         
    h.definedone () ;                                                                                                                                                                                                                                           
    dcl hiter hi ("h") ;                                                                                                                                                                                                                                        
  end ;                                                                                                                                                                                                                                                         
  do _n_ = lb to hb ;                                                                                                                                                                                                                                           
    k = arr[_n_] ;                                                                                                                                                                                                                                              
    h.add() ;                                                                                                                                                                                                                                                   
  end ;                                                                                                                                                                                                                                                         
  do _n_ = lb by 1 while (hi.next() = 0) ;                                                                                                                                                                                                                      
    arr[_n_] = k ;                                                                                                                                                                                                                                              
  end ;                                                                                                                                                                                                                                                        
  put arr(*) ;                                                                                                                                                                                                                                                  
run ;      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively, you can use CALL SORTN against an auxiliary array with the required range and then copy the sorted items back into the array in question, for example:&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;%let range = 3:8 ; &lt;BR /&gt;data _null_ ; &lt;BR /&gt;  array arr [10] (2 10 444 333 111 888 777 666 4 5) ;&lt;BR /&gt;  array aux [&amp;amp;range] _temporary_ ; &lt;BR /&gt;  do _n_ = lbound (aux) to hbound (aux) ; &lt;BR /&gt;    aux [_n_] = arr [_n_] ; &lt;BR /&gt;  end ; &lt;BR /&gt;  call sortn (of aux[*]) ; &lt;BR /&gt;  do _n_ = lbound (aux) to hbound (aux) ; &lt;BR /&gt;    arr [_n_] = aux [_n_] ; &lt;BR /&gt;  end ; &lt;BR /&gt;  put arr(*) ; &lt;BR /&gt;run ; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As far as KSharp's bubble sort is concerned, it's okay for a few items but will get painfully slow even for a thousand since its running time is O(N**2), while all of the above approaches can handle millions of array items in a second or so.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 01:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915735#M360796</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2024-02-13T01:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915736#M360797</link>
      <description>&lt;P&gt;Hey Peter,&lt;/P&gt;
&lt;P&gt;Thanks 1E6 for your generous plug.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 01:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915736#M360797</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2024-02-13T01:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915780#M360818</link>
      <description>&lt;P&gt;Hi Paul!&amp;nbsp; GREAT. I will use the code from the paper.&lt;/P&gt;
&lt;P&gt;The SAS Community and all these discussions are VERY useful. I have used SAS since 1981.&amp;nbsp;I am&amp;nbsp; writing another paper about quantiles, and try NOT to use very SAS specific solutions.&amp;nbsp;/BR Anders&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 09:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915780#M360818</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-13T09:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915795#M360824</link>
      <description>&lt;P&gt;Here is a FCMP based Quick Sort Routine just for your reference.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib = work.cmpar.lib;
   subroutine qsort(v[*], left, right); 
   outargs v; 
   if left &amp;gt;= right then return; 
   call swap(v, left, ceil((left + right) / 2));
   last = left;
   do i = left to right;
      if v[i] &amp;lt; v[left] then call swap(v, last, i);
   end;
   call swap(v, left, last);
   call qsort(v, left, last - 1);
   call qsort(v, last + 1, right);
   endsub;
   subroutine swap(v[*], i, j); 
     outargs v;
     temp = v[i];
     v[i] = v[j];
     v[j] = temp;
   endsub;
quit;

options cmplib = work.cmpar;
/* Sort values from 2 to end of the Array k[*] */
data _null_;
   array k[5] _temporary_ (100, 2, 70, 12, 50); 
   call qsort(k, 2, dim(k)); 
   SORTED = catq('d',' ', of k[*]);
   put SORTED =; /* SORTED=100 2 12 50 70 */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Feb 2024 11:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915795#M360824</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2024-02-13T11:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915802#M360827</link>
      <description>&lt;P&gt;Hi! Very nice!&amp;nbsp; &amp;nbsp;I will try it in SAS ODA. /Br Anders&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 12:46:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915802#M360827</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-13T12:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Sort only part of a  _temporary_ Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915899#M360842</link>
      <description>&lt;P&gt;Hi! The hash solution works nicely in SAS ODA. /Br Anders&lt;/P&gt;</description>
      <pubDate>Tue, 13 Feb 2024 18:26:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-only-part-of-a-temporary-Array/m-p/915899#M360842</guid>
      <dc:creator>AndersS</dc:creator>
      <dc:date>2024-02-13T18:26:19Z</dc:date>
    </item>
  </channel>
</rss>

