Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ranking within rows of a data set [sas]

Tags:

sas

Suppose I've got a data set with n rows and p columns such that each entry in the data set contains a real number. I am looking for a way to rank the p columns within each row. The output of this ranking should be a length-p vector of ranks that accounts for ties.

So, let's say my data set has 5 columns. The first row could be something like row 1 = {10, 13, 3, 3, -4}. I'd like to perform some operations on this row and in the end get back the result row 1 ranks = {3, 4, 2, 2, 1}. The second row could be something like row 2 = {8, 3, -6, 5, 2} and the result on this row should be row 2 ranks = {5, 3, 1, 4, 2}.

Is this functionality implemented in SAS? I've generated code that doesn't account for ties, but they occur often enough that it would take an unreasonable amount of time to correct the row rankings that were done incorrectly.

like image 376
assumednormal Avatar asked Dec 15 '22 15:12

assumednormal


2 Answers

Interesting question; here is one possible solution:

data have;
   p1=10; p2=13; p3=3;  p4=3; p5=-4; output;
   p1=8;  p2=3;  p3=-6; p4=5; p5=2;  output;
run;

data want;
   set have;
   array p(*) p1-p5;
   array c(*) c1-c5;
   array r(*) r1-r5;

   /* Copy vector to temp array and sort */
   do i=1 to dim(p); 
      c(i) = p(i); 
      end;
   call sortn(of c(*));

   /* Search new sorted array for the original position */
   do i=1 to dim(c);
      if i = 1 then rank=1;
      else if c(i) ne c(i-1) then rank + 1;
      do j=1 to dim(p);
         if p(j) = c(i) then do;
            r(j) = rank;
            end;
         end;
      end;

   /* PUT statement to see result in log */
   put +3 p(*)
     / +3 c(*)
     / +3 r(*);

   drop i j rank c1-c5;
run;
like image 177
BellevueBob Avatar answered Dec 30 '22 02:12

BellevueBob


Sounds to me like you'll need several arrays to do this.

  1. Array 1: Array to store the ranks
  2. Array 2: Array to sort the values
  3. Array 3: The original un-altered data

I don't have time right now to write the code but using someething like this would do a lot of the heavy lifting:

http://support.sas.com/kb/24/754.html

like image 26
Robert Penridge Avatar answered Dec 30 '22 02:12

Robert Penridge