RANK Function

RANK (matrix) ;

The RANK function creates a new matrix that contains elements that are the ranks of the corresponding elements of the numerical argument, matrix. The rank of a missing value is a missing value. The ranks of tied values are assigned arbitrarily. (See the description of the RANKTIE function for alternate approaches.)

For example, the following statements produce the ranks of a vector:

x = {2 2 1 0 5};
r = rank(x);
print r;

Figure 23.267: Ranks of a Vector

r
3 4 2 1 5


Provided that a vector, x, does not contain missing values, the RANK function can be used to sort the vector, as shown in the following statements:

b = x;
x[,rank(x)] = b;
print x;

Figure 23.268: Sorted Vector

x
0 1 2 2 5


You can also sort a matrix by using the SORT subroutine. The SORT subroutine handles missing values in the data.

The RANK function can also be used to find anti-ranks of $\mb {x}$, as follows:

x = {2 2 1 0 5};
r = rank(x);
a = r;
a[,r] = 1:ncol(x);
print a;

Figure 23.269: Anti-Ranks of a Vector

a
4 3 1 2 5


Although the RANK function ranks only the elements of numerical matrices, you can rank the elements of a character matrix by using the UNIQUE function, as demonstrated by the following statements:

/* Create RANK-like functionality for character matrices */
start rankc(x);
   s = unique(x);              /* the unique function returns a sorted list */
   idx = j(nrow(x), ncol(x));
   ctr = 1;                    /* there can be duplicate values in x */
   do i = 1 to ncol(s);        /* for each unique value */
      t = loc(x = s[i]);
      nDups = ncol(t);
      idx[t] = ctr : ctr+nDups-1;
      ctr = ctr + nDups;
   end;
   return (idx);
finish;

/* call the RANKC module */
x = {every good boy does fine and good and well every day};
rc = rankc(x);
print rc[colnam=x];

/* Notice that ranking is in ASCII order, in which capital
   letters precede lower case letters.  To get case-insensitive
   behavior, transform the matrix before comparison */
x = {"a" "b" "X" "Y" };
asciiOrder = rankc(x);
alphaOrder = rankc(upcase(x));
print x, asciiOrder, alphaOrder;

Figure 23.270: Ranks of Character Matrices

rc
  EVERY GOOD BOY DOES FINE AND GOOD AND WELL EVERY DAY
ROW1 6 9 3 5 8 1 10 2 11 7 4

x
a b X Y

asciiOrder
3 4 1 2

alphaOrder
1 2 3 4


There is no SAS/IML function that directly computes the linear algebraic rank of a matrix. In linear algebra, the rank of a matrix is the maximal number of linearly independent columns (or rows). You can use the following technique to compute the numerical rank of matrix a:

/* Only four linearly independent columns */
A = {1 0 1 0 0,
     1 0 0 1 0,
     1 0 0 0 1,
     0 1 1 0 0,
     0 1 0 1 0,
     0 1 0 0 1 };
rank = round(trace(ginv(a)*a));
print rank;

Figure 23.271: Numerical Rank of a Matrix

rank
4


Another common technique used to examine the rank of a matrix is to look at the number of nonzero singular values in the singular value decomposition of a matrix (see the SVD call). However, keep in mind that numerical computations might result in singular values for a rank-deficient matrix that are small but nonzero.