<?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: Rank Rows of a Matrix Without Looping in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588694#M4810</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9250"&gt;@stevo642&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Had no SAS access on the first response, but now that I do and can test, consider:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                                                                                                                                                     
  input v1-v3 ;                                                                                                                                                                                                                                                 
  cards ;                                                                                                                                                                                                                                                       
6 9  5                                                                                                                                                                                                                                                          
1 3 10                                                                                                                                                                                                                                                          
0 0  0                                                                                                                                                                                                                                                          
0 0  1                                                                                                                                                                                                                                                          
0 1  1                                                                                                                                                                                                                                                          
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
proc transpose data = have out = t (drop = _:) ;                                                                                                                                                                                                                
run ;                                                                                                                                                                                                                                                           
proc rank data = t out = r ;                                                                                                                                                                                                                                    
  var col: ;                                                                                                                                                                                                                                                    
run ;                                                                                                                                                                                                                                                           
proc transpose data = r out = want (drop=_:) prefix=v ;                                                                                                                                                                                                         
run ;                                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt; v1     v2     v3
-----------------
2.0    3.0    1.0
1.0    2.0    3.0
2.0    2.0    2.0
1.5    1.5    3.0
1.0    2.5    2.5
&lt;/PRE&gt;
&lt;P&gt;Plus, you have all the bells and whistles of proc RANK to handle the ties differently, should you wish.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 14 Sep 2019 02:42:20 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-09-14T02:42:20Z</dc:date>
    <item>
      <title>Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588380#M4802</link>
      <description>&lt;P&gt;Is there a technique that would allow ranking each row of a matrix without looping?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the returned matrix would have within-row ranks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example if A held all non-missing numeric data, then imaginary function rowRank(A) would return values 1:ncol(A) in each row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2019 21:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588380#M4802</guid>
      <dc:creator>stevo642</dc:creator>
      <dc:date>2019-09-12T21:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588393#M4803</link>
      <description>Show an example with data please. Sounds like you want to rank each row of data.</description>
      <pubDate>Thu, 12 Sep 2019 22:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588393#M4803</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-12T22:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588396#M4804</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a = {1 3 10, 6 9 5};  &lt;BR /&gt;print a ,, &lt;BR /&gt;  (rank(a))[l="Ranks from rank(a)"] ,, &lt;BR /&gt;  ({1 2 3, 2 3 1})[l="Desired Ranks (row-wise ranks)"];&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;A&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 3 10&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;6 9 5&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Ranks from rank(a)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 2 6&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;4 5 3&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Desired Ranks (row-wise ranks)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 2 3&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 3 1&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2019 22:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588396#M4804</guid>
      <dc:creator>stevo642</dc:creator>
      <dc:date>2019-09-12T22:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588454#M4805</link>
      <description>&lt;P&gt;I think this can be done with a two pass solution.&amp;nbsp; The initial rank matrix needs to be adjusted so that each row has values that are larger than the row before, then the second ranking is operating within row.&amp;nbsp; For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
  a = {1 3 10, 6 9 5};
  print a;

  b = row(a) - 1;
  r = rank( rank(a) + nrow(a) # ncol(a) # b ) - ncol(a) # b;
  print r;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 09:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588454#M4805</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2019-09-13T09:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588483#M4806</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9250"&gt;@stevo642&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ranks given to second row seems to be wrong. I think it has to be 3 1 2. I have less expertise in IML. Do you accept a Data Step solution?&lt;/P&gt;
&lt;P&gt;The steps include the sorting of each row by using SORTN() function and at the same time switching the index of the array. Then re-ordered index will be the rank. SMALLEST() takes care of the sorting in the background and returns&lt;/P&gt;
&lt;P&gt;the i-th smallest value. Using WHICHN() with the i-th smallest value gives the desired Rank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note, no attempt is made to check for ties.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let vn = 3;
data rank;
   set A;
   array x x1-x&amp;amp;vn;
   array R R1 - R&amp;amp;vn;
   do i = 1 to dim(x);
      R[i] = whichN(smallest(i, of x[*]), of x[*]);
      put R[i] =;
   end;
keep R:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 11:45:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588483#M4806</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-09-13T11:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588616#M4807</link>
      <description>&lt;P&gt;Great ideas -- thank you. The "r2" array in DO loop below is what I have been using.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;

a = {2 4 6, 15 11 13, 29 28 27};

r1 = rank(a);

r2 = j(nrow(a), ncol(a), .);

do i = 1 to nrow(a);
  r2[i,] = rank(a[i,]);
end;

print a ,, r1[l="Rank(a) - whole matrix - don't want"] ,, r2[l="Desired 'Row Ranks' from looping rank(row-by-row)"];

quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 18:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588616#M4807</guid>
      <dc:creator>stevo642</dc:creator>
      <dc:date>2019-09-13T18:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588621#M4808</link>
      <description>&lt;P&gt;Ian is a master at these manipulation tricks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you can make a small improvement to his idea to make it more efficient. This computation requires only a translation, a rank, and another translation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
a = {2 4 6, 
     15 11 13, 
     29 28 27,
     2 4 6, 
     4 1 6, 
     2 6 4, 
     6 4 2 };

maxDiff = range(a) + 1;  /* largest difference between elements */
b = a + maxDiff*row(a);  /* uniquify each row */
r = rank(b) - ncol(a)* T(0:(nrow(a)-1));
print r;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 18:24:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588621#M4808</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-09-13T18:24:01Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588644#M4809</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9250"&gt;@stevo642&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;If your matrix is in the form of a SAS data set, it is a simple 3 step process and 7 statements (including 3 RUN statements):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Proc TRANSPOSE.&lt;/P&gt;
&lt;P&gt;2. Proc RANK.&lt;/P&gt;
&lt;P&gt;3. Proc TRANSPOSE.&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;</description>
      <pubDate>Fri, 13 Sep 2019 19:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588644#M4809</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-13T19:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588694#M4810</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9250"&gt;@stevo642&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Had no SAS access on the first response, but now that I do and can test, consider:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                                                                                                                                                     
  input v1-v3 ;                                                                                                                                                                                                                                                 
  cards ;                                                                                                                                                                                                                                                       
6 9  5                                                                                                                                                                                                                                                          
1 3 10                                                                                                                                                                                                                                                          
0 0  0                                                                                                                                                                                                                                                          
0 0  1                                                                                                                                                                                                                                                          
0 1  1                                                                                                                                                                                                                                                          
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
proc transpose data = have out = t (drop = _:) ;                                                                                                                                                                                                                
run ;                                                                                                                                                                                                                                                           
proc rank data = t out = r ;                                                                                                                                                                                                                                    
  var col: ;                                                                                                                                                                                                                                                    
run ;                                                                                                                                                                                                                                                           
proc transpose data = r out = want (drop=_:) prefix=v ;                                                                                                                                                                                                         
run ;                                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt; v1     v2     v3
-----------------
2.0    3.0    1.0
1.0    2.0    3.0
2.0    2.0    2.0
1.5    1.5    3.0
1.0    2.5    2.5
&lt;/PRE&gt;
&lt;P&gt;Plus, you have all the bells and whistles of proc RANK to handle the ties differently, should you wish.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 02:42:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588694#M4810</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-14T02:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588699#M4811</link>
      <description>&lt;P&gt;I like your modification Rick, surely the way to go if the OP is looking for an IML solution.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 05:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588699#M4811</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2019-09-14T05:31:27Z</dc:date>
    </item>
    <item>
      <title>Re: Rank Rows of a Matrix Without Looping</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588718#M4812</link>
      <description>&lt;P&gt;If the OP wants to control how to rank tied values, &lt;A href="https://go.documentation.sas.com/?docsetId=imlug&amp;amp;docsetTarget=imlug_langref_sect401.htm&amp;amp;docsetVersion=15.1&amp;amp;locale=en" target="_self"&gt;the RANKTIE function in SAS/IML&lt;/A&gt;&amp;nbsp;supports exactly the same methods as PROC RANK.&lt;/P&gt;</description>
      <pubDate>Sat, 14 Sep 2019 10:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Rank-Rows-of-a-Matrix-Without-Looping/m-p/588718#M4812</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-09-14T10:15:55Z</dc:date>
    </item>
  </channel>
</rss>

