<?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: SAS Custom Sort in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597884#M172315</link>
    <description>&lt;P&gt;Fun fact: A numeric expression providing the desired sort order can be constructed from &lt;EM&gt;existing&lt;/EM&gt; informats and functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input c $;
cards;
A1Y
A1Z
A1A
B1Y
B1Z
B1A
ND
P
;

proc sql;
select c from have
order by mod(input(c,bits24.)+7,27);
quit; /* assuming ASCII encoding */&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 19 Oct 2019 10:21:32 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2019-10-19T10:21:32Z</dc:date>
    <item>
      <title>SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597767#M172266</link>
      <description>&lt;P&gt;I have the following treatments&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A1A&lt;/P&gt;
&lt;P&gt;A1Y&lt;/P&gt;
&lt;P&gt;A1Z&lt;/P&gt;
&lt;P&gt;B1A&lt;/P&gt;
&lt;P&gt;B1Y&lt;/P&gt;
&lt;P&gt;B1Z&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I need to display on the report in the following order based on dose level.&lt;/P&gt;
&lt;P&gt;A1Y&lt;/P&gt;
&lt;P&gt;A1Z&lt;/P&gt;
&lt;P&gt;A1A&lt;/P&gt;
&lt;P&gt;B1Y&lt;/P&gt;
&lt;P&gt;B1Z&lt;/P&gt;
&lt;P&gt;B1A&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I try to use PROC FORMAT, it only displays the numbers which I don't want. &lt;FONT color="#000080"&gt;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;Please help how I can custom sort to get the alphabetical dose levels and not mere numbers.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format;&lt;BR /&gt;value $cohort &lt;BR /&gt;'A1Y'=1&lt;BR /&gt;'A1Z'=2&lt;BR /&gt;'A1A'=3&lt;BR /&gt;'B1Y'=4&lt;BR /&gt;'B1Z'=5&lt;BR /&gt;'B1A'=6&lt;BR /&gt;'ND'=7&lt;BR /&gt;'P'=8;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 16:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597767#M172266</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-10-18T16:25:09Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597769#M172267</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;&amp;nbsp; You sound like a smart and creative person. Why not use your custom &lt;STRONG&gt;&lt;EM&gt;formatted-sort&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;with(as) an informat like below using your original idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var $;
cards;
A1A
A1Y
A1Z
B1A
B1Y
B1Z
;
proc format;
invalue cohort 
'A1Y'=1
'A1Z'=2
'A1A'=3
'B1Y'=4
'B1Z'=5
'B1A'=6
'ND'=7
'P'=8;
run;

data temp;
set have;
new_var=input(var, cohort.);
run;

proc sort data=temp out=want(drop=new_var);
by new_var;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 16:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597769#M172267</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-18T16:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597772#M172269</link>
      <description>&lt;P&gt;SQL is more flexible as you can use expressions to order without having to create a temp table like the Datastep and 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 var $;
cards;
A1A
A1Y
A1Z
B1A
B1Y
B1Z
;
proc format;
invalue cohort 
'A1Y'=1
'A1Z'=2
'A1A'=3
'B1Y'=4
'B1Z'=5
'B1A'=6
'ND'=7
'P'=8;
run;

proc sql;
create table want as
select *
from have
order by input(var, cohort.);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Oct 2019 16:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597772#M172269</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-18T16:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597782#M172274</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some PROC's support reporting tables ordered the same as the incoming data.&amp;nbsp; For instance PROC FREQ, PROC TABULATE, and PROC MEANS (and the DEFINE statement in PROC REPORT) honor the "ORDER=DATA" option.&amp;nbsp; So, without resorting (pun intended) to formats, you could create a data set view (as opposed to a data set file) with data from your original dataset processed in the desired order, which then can be submitted to the proc, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vtemp /view=vtemp;
  set sashelp.class (where=(sex='M'))  sashelp.class (where=(sex='F'));
run;

proc tabulate data=vtemp order=data; 
  class sex;
  var height weight;
  tables sex all='Total',
         (height weight)*(mean N);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here I've prevented the default behavior of PROC TABULATE to have the row with sex="F" precede the row with sex="M".&amp;nbsp; And remember, the data set view (VTEMP here) is merely a process, it does not write data to disk, so this is a fairly efficient approach (certainly faster than actually sorting the data)&amp;nbsp; The actual data is processed only when the view is named in a subsequent step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't even need to order the entire dataset.&amp;nbsp; You only need to order the first encounter with each value of interest.&amp;nbsp; I.e. in my example, you can just write one dummy record with sex="M" followed by a second dummy record with sex="F" preceding the original dataset of interest, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vtemp2 /view=vtemp2;
  if _n_=1 then do sex='M','F'; output; end;
  set sashelp.class;
  output;
run;
proc tabulate data=vtemp2 order=data; 
  class sex;
  var height weight;
  tables sex all='Total',
         (height weight)*(mean N);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The downside with the 2nd solution is that I've added 2 observations not in the original data set, which would generate erroneous results if all I wanted was a frequency table of SEX.&amp;nbsp; But PROC TABULATE doesn't care in this case, because I asked it to analyze height and weight (missing only in the added obs) classified by sex.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are ways to avoid this problem too, but I've gone far enough.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 17:33:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597782#M172274</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-18T17:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597785#M172276</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have the following treatments&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A1A&lt;/P&gt;
&lt;P&gt;A1Y&lt;/P&gt;
&lt;P&gt;A1Z&lt;/P&gt;
&lt;P&gt;B1A&lt;/P&gt;
&lt;P&gt;B1Y&lt;/P&gt;
&lt;P&gt;B1Z&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I need to display on the report in the following order based on dose level.&lt;/P&gt;
&lt;P&gt;A1Y&lt;/P&gt;
&lt;P&gt;A1Z&lt;/P&gt;
&lt;P&gt;A1A&lt;/P&gt;
&lt;P&gt;B1Y&lt;/P&gt;
&lt;P&gt;B1Z&lt;/P&gt;
&lt;P&gt;B1A&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What dose level? Are those codes dose levels? Your first statement says these are &lt;STRONG&gt;treatments&lt;/STRONG&gt;. So you are apparently missing some detail, i.e. the dose levels. Or be more precise in your terminology.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And how did you use the proc format in an attempt to create things in order? Format has nothing to do with sorting unless displaying in a procedure like Freq, Tabulate or Report and such. So show how you attempt to use the format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A limited number of procedures will use the Preloadfmt option and when available has some restrictions but this shows a table in the desired order:&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $cohort (notsorted)
'A1Y'='A1Y'
'A1Z'='A1Z'
'A1A'='A1A'
'B1Y'='B1Y'
'B1Z'='B1Z'
'B1A'='B1A'
'ND' ='ND' 
'P'  ='P'  
;
run;

data example;
  input value $;
datalines;
A1Z
A1A
B1Y
B1Z
B1A
ND 
P 
A1A
B1Y 
ND 
ND 
ND 
A1Y
A1Y
A1Y
A1Y
;


proc tabulate data=example;
   class value /preloadfmt order=data ;
   format value $cohort.;
   table value,
         n
         /printmiss
   ;
run;&lt;/PRE&gt;
&lt;P&gt;Please note: providing data example in a data step and posting code in a code box opened using the forum's {I} icon to preserve formatting, and code to generate output. Also the option on the format (notsorted) is needed to get the order to display that way with the Preloadfmt option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc Report will also allow the Preloadfmt option in the DEFINE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most other procedures will not honor the sort order of the format and only use the formatted values.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 17:34:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597785#M172276</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-18T17:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597808#M172289</link>
      <description>&lt;P&gt;This worked well. Thanks for the tip!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2019 18:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597808#M172289</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-10-18T18:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597863#M172307</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You have a clear pattern: Sort by the first 2 bytes of VAR, then sort by the re-sequenced third byte. Hence, there's no need to format each complete key-value - or indeed to use a format at all, as it's tedious, counter-productive, and error-prone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather, simply create a control file where the required sequence of the last byte is stored, then join it with what you HAVE by the last byte of VAR, and then just sort accordingly, keeping only the columns you need to keep:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input var $ somedata $ ;                                                                                                              
  cards ;                                                                                                                               
A1A  A33                                                                                                                                
A1Y  A11                                                                                                                                
A1Z  A22                                                                                                                                
B1A  B33                                                                                                                                
B1Y  B11                                                                                                                                
B1Z  B22                                                                                                                                
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data control ;                                                                                                                          
  input lastbyte :$1. seq ;                                                                                                             
  cards ;                                                                                                                               
Y  1                                                                                                                                    
Z  2                                                                                                                                    
A  3                                                                                                                                    
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
proc sql ;                                                                                                                              
  create table want as                                                                                                                  
  select have.* from have, control                                                                                                      
  where  char (var, length (var)) = lastbyte                                                                                            
  order  put (var, $2.), seq                                                                                                            
  ;                                                                                                                                     
quit ;                                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Oct 2019 03:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597863#M172307</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-19T03:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597884#M172315</link>
      <description>&lt;P&gt;Fun fact: A numeric expression providing the desired sort order can be constructed from &lt;EM&gt;existing&lt;/EM&gt; informats and functions:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input c $;
cards;
A1Y
A1Z
A1A
B1Y
B1Z
B1A
ND
P
;

proc sql;
select c from have
order by mod(input(c,bits24.)+7,27);
quit; /* assuming ASCII encoding */&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Oct 2019 10:21:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597884#M172315</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-19T10:21:32Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597927#M172347</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;: Very clever ;).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Oct 2019 21:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597927#M172347</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-19T21:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598003#M172390</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&amp;nbsp;How on earth...?&amp;nbsp; Well done!&lt;/P&gt;</description>
      <pubDate>Sun, 20 Oct 2019 22:10:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598003#M172390</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-10-20T22:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598069#M172413</link>
      <description>&lt;P&gt;Thank you all for the likes. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&amp;nbsp;How on earth...?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;OL&gt;
&lt;LI&gt;The desired sort order is essentially alphabetic, except for the third character. But (Y, Z, A) is a &lt;EM&gt;cyclic&lt;/EM&gt; permutation of the sorted triple (A, Y, Z). ---&amp;gt; Idea: The MOD function (with a suitable divisor) applied to the result of an order-preserving character-to-numeric conversion of the eight treatment codes&amp;nbsp;&lt;EM&gt;might&lt;/EM&gt; yield eight numbers in ascending order. An additive shift of the numeric argument might be necessary to avoid an "overflow" (like &lt;EM&gt;d&lt;/EM&gt;-1 + 1 = 0 modulo &lt;EM&gt;d&lt;/EM&gt;) in the middle of that sequence.&lt;/LI&gt;
&lt;LI&gt;Looked through the list "Informats by Category" and found the BITS&lt;EM&gt;w.&lt;/EM&gt;&amp;nbsp;informat for an easy&amp;nbsp;order-preserving character-to-numeric conversion (here: &lt;EM&gt;w&lt;/EM&gt;=3*8).&lt;/LI&gt;
&lt;LI&gt;Was lucky that a brute-force search for integers &lt;EM&gt;d&lt;/EM&gt; and &lt;EM&gt;e&lt;/EM&gt; such that&lt;BR /&gt;
&lt;PRE&gt;m[i]=mod(input(c[i],bits24.)+e,d)   (i=1, ..., 8; c[1]='A1Y', c[2]='A1Z', ..., c[8]='P')&lt;/PRE&gt;
satisfy&amp;nbsp;&lt;FONT face="courier new,courier"&gt;m[1]&amp;lt;m[2]&amp;lt;&lt;/FONT&gt;&amp;nbsp;... &lt;FONT face="courier new,courier"&gt;&amp;lt;m[8]&lt;/FONT&gt; was successful: &lt;EM&gt;d&lt;/EM&gt;=27 and various possible values for &lt;EM&gt;e&lt;/EM&gt;, the smallest of which was &lt;EM&gt;e&lt;/EM&gt;=7.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 21 Oct 2019 10:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598069#M172413</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-21T10:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598090#M172418</link>
      <description>&lt;P&gt;Kolmogorov reincarnated in Germany coz he thought his math was underutilized in Russia as communism prevented innovation. Oh well, even the spirits recognize that &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 11:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598090#M172418</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-21T11:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598101#M172423</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Kolmogorov reincarnated in Germany (...)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;??? Strangely enough, he was still alive when I was born ...&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 12:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598101#M172423</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-21T12:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598107#M172427</link>
      <description>&lt;P&gt;Oops embarrassing to realize my knowledge of history is awful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Okay, changing it to Euler's reincarnation in Germany bcoz he foresaw the &lt;STRONG&gt;not&lt;/STRONG&gt; so fair secret banking system, so he wanted to impart his knowledge for the industrial prowess of Germany. Hmm, this could change too for the reason I wouldn't be surprised if one is irritated with Merkel's policies. Let me stop right here before moderator steps in. Cheers!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 12:48:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598107#M172427</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-21T12:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598174#M172468</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;: Perhaps even more strange, I've seen&amp;nbsp;and heard him speak.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 16:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598174#M172468</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-21T16:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598286#M172535</link>
      <description>&lt;P&gt;Does the length of the variable have anything to do with this statement?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;My variable length for "c" is $9. And it doesn't work as expected.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;What am I missing?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table test as 
select c from mydata
order by mod(input(c,bits24.)+1,10);
quit; /* assuming ASCII encoding */&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Oct 2019 23:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598286#M172535</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-10-21T23:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598355#M172554</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(...) it doesn't work as expected.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;order by mod(input(c,bits24.)+&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;,&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;10&lt;/FONT&gt;&lt;/STRONG&gt;);&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1717"&gt;@saslove&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My suggestion was tailored to the eight treatment codes 'A1Y', 'A1Z', ..., 'P' (and their desired sort order) from your initial post. In particular, the parameter values 7 and 27 in&lt;/P&gt;
&lt;PRE&gt;order by mod(input(c,bits24.)+&lt;STRONG&gt;7&lt;/STRONG&gt;,&lt;STRONG&gt;27&lt;/STRONG&gt;);&lt;/PRE&gt;
&lt;P&gt;were critical. So, it's not surprising that the trick fails if you change these values to &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt; and &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/FONT&gt;, respectively, without a good reason.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Does the length of the variable have anything to do with this statement?&amp;nbsp;
&lt;P&gt;My variable length for "c" is $9.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The maximum length (3) of your original treatment codes went into the length of the BITS&lt;EM&gt;w&lt;/EM&gt;. informat: 8 bits per character, hence &lt;EM&gt;w&lt;/EM&gt;=24.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It doesn't matter if the defined length of the character variable is greater than 3. (In my code example it was 8.) The BITS24. informat reads only the first 24 bits, i.e., the first 3 characters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your real treatment codes were longer than 3 characters (e.g. '&lt;FONT face="courier new,courier"&gt;Placebo&lt;/FONT&gt;'), the length of the informat would need to be increased. The maximum length is 64 (bits), i.e. 8 characters, though. Also, with a different set of treatment codes the parameter values (7, 27) would change almost certainly. Even worse, &lt;EM&gt;it's not guaranteed that suitable values exist at all&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I would not recommend this type of solution for production code, e.g., in a clinical research project. &lt;/STRONG&gt;That's why I called it a "fun fact." For practical purposes the solutions provided by &lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597772#M172269" target="_blank" rel="noopener"&gt;novinosrin&lt;/A&gt;, &lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597782#M172274" target="_blank" rel="noopener"&gt;mkeintz&lt;/A&gt;, &lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597785#M172276" target="_blank" rel="noopener"&gt;ballardw&lt;/A&gt; and &lt;A href="https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/597863#M172307" target="_blank" rel="noopener"&gt;hashman&lt;/A&gt; are clearly preferable because they are much more transparent and stable and easier to adapt (e.g. to new treatment codes).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, feel free to post a different set of treatment codes and their desired sort order. Then I can try to come up with another "fun" solution (just for demonstration what's possible with SAS-supplied [in]formats and functions).&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 09:40:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598355#M172554</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-22T09:40:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598511#M172599</link>
      <description>&lt;P&gt;Thanks! That makes sense, thanks for all the explanation. It really helps me understand this code.&amp;nbsp;&lt;BR /&gt;For production, I have used&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;'s method.&amp;nbsp;&lt;BR /&gt;I did want to try out what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;suggested, so I was trying on a test window.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 20:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598511#M172599</guid>
      <dc:creator>saslove</dc:creator>
      <dc:date>2019-10-22T20:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Custom Sort</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598512#M172600</link>
      <description>&lt;P&gt;As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;said, do not use his [very] clever demonstration in production.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. This code is very hard to maintain. Do you understand it? Can you explain it? Did you document it properly?&lt;/P&gt;
&lt;P&gt;2. It can give unwanted results if the variable is given new values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 20:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Custom-Sort/m-p/598512#M172600</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-10-22T20:07:15Z</dc:date>
    </item>
  </channel>
</rss>

