Language Reference |
RANKTIE Function |
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 averaged.
For example, the following statements produce the ranks of a vector, and average the ranks of tied values:
x = {2 2 1 0 5}; rt = ranktie(x); print rt;
The RANKTIE function differs from the RANK function in that RANKTIE averages the ranks of tied values, whereas RANK breaks ties arbitrarily.
Although the RANK function only ranks 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];
Copyright © SAS Institute, Inc. All Rights Reserved.