Language Reference


RANKTIE Function

RANKTIE (matrix <, method> );

The RANKTIE function creates a new matrix that contains elements that are the ranks of the corresponding elements of matrix. The rank of a missing value is a missing value. The ranks of tied values are computed by using one of several methods.

The arguments to the function are as follows:

matrix

specifies the data.

method

specifies the method used to compute the ranking of tied values. These methods correspond to those defined by using the TIES= option in the RANK procedure. For details, see the "Concepts" section of the documentation for the RANK procedure in the Base SAS Procedures Guide.

The following values are valid:

"Mean"

specifies that tied elements are assigned rankings equal to the mean of the tied elements. This is the default method. This method is known as a fractional competition ranking.

"Low"

specifies that tied elements are assigned rankings equal to the minimum order rank of the tied elements. This method is known as a standard competition ranking.

"High"

specifies that tied elements are assigned rankings equal to the maximum rank of the tied elements. This method is known as a modified competition ranking.

"Dense"

specifies that ranks are consecutive integers that begin with 1 and end with the number of unique, nonmissing values. Tied values are assigned the same rank. This method is known as a dense ranking.

The RANKTIE function differs from the RANK function in that the RANK function breaks ties arbitrarily.

For example, the following statements produce ranks of a vector by using several different methods of breaking ties:

x = {4 4 0 6};
rMean = ranktie(x);   /* default is "Mean" */
rLow = ranktie(x, "Low");
rHigh = ranktie(x, "High");
rDense = ranktie(x, "Dense");
print rMean, rLow, rHigh, rDense;

Figure 25.319: Numerical Ranks of a Vector

rMean
2.5 2.5 1 4

rLow
2 2 1 4

rHigh
3 3 1 4

rDense
2 2 1 3



Although the RANKTIE 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 RANKTIE-like functionality for character matrices */
start ranktiec(x);
   s = unique(x);
   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+(nDups-1)/2; /* =(ctr:ctr+nDups-1)[:] */
      ctr = ctr + nDups;
   end;
   return (idx);
finish;

/* call the RANKTIEC module */
x = {every good boy does fine and good and well every day};
rtc = ranktiec(x);
print rtc[colname=x];

Figure 25.320: Numerical Ranks of a Character Vector

rtc
  EVERY GOOD BOY DOES FINE AND GOOD AND WELL EVERY DAY
ROW1 6.5 9.5 3 5 8 1.5 9.5 1.5 11 6.5 4