Help using Base SAS procedures

PROC RANK on same values

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

PROC RANK on same values

Hi all,

I am struggling with what I would consider an easy thing, but I just cant get it done.

I've got something like

A       B    

1222          180.00

1222          180.00

1222          260.00

And I want...

A       B          RF_RANK

1222 180.00      1

1222 180.00      2

1222 260.00      3

What I get is

A       B        RF_RANK  

1222          180.00      3

1222          180.00      3

1222          260.00      1

with this code:

proc rank data = work.W62KKV70

   out = work.W62KKN4Z

          descending

          ties = high; // also tried DENSE and MEAN and LOW

   by

      TEC_POSITION_ID

   ;

      var RATINGFAKTOR;

      ranks RF_RANK;

run;

How would I get a sequential rank here?

Thanks a lot!

Thomas


Accepted Solutions
Solution
‎02-15-2012 09:05 AM
PROC Star
Posts: 7,474

PROC RANK on same values

Posted in reply to thomash123

You are getting descending values because you are asking for them.  Your choice of ranks appears to be quite arbitrary, with ties being ranked according to location in the data.

For what you want, you didn't even need to run proc rank .. you just had to sort the data and then assign sequential numbers as ranks.

That is all I basically did with the code, below, except I used your proc rank statement rather than just a sort.

proc rank data = work.W62KKV70

   out = work.W62KKN4Z;

   by TEC_POSITION_ID;

      var RATINGFAKTOR;

      ranks RF_RANK;

run;

data work.W62KKN4Z;

  set work.W62KKN4Z (drop=RF_RANK);

  by  TEC_POSITION_ID

   if first.TEC_POSITION_ID then  RF_RANK=1;

  else RF_RANK+1;

run;

But you would have gotten the same result with:

proc sort data = work.W62KKV70

   out = work.W62KKN4Z;

   by TEC_POSITION_ID ratingfaktor;

run;

data work.W62KKN4Z;

  set work.W62KKN4Z;

  by  TEC_POSITION_ID

   if first.TEC_POSITION_ID then  RF_RANK=1;

  else RF_RANK+1;

run;

View solution in original post


All Replies
Solution
‎02-15-2012 09:05 AM
PROC Star
Posts: 7,474

PROC RANK on same values

Posted in reply to thomash123

You are getting descending values because you are asking for them.  Your choice of ranks appears to be quite arbitrary, with ties being ranked according to location in the data.

For what you want, you didn't even need to run proc rank .. you just had to sort the data and then assign sequential numbers as ranks.

That is all I basically did with the code, below, except I used your proc rank statement rather than just a sort.

proc rank data = work.W62KKV70

   out = work.W62KKN4Z;

   by TEC_POSITION_ID;

      var RATINGFAKTOR;

      ranks RF_RANK;

run;

data work.W62KKN4Z;

  set work.W62KKN4Z (drop=RF_RANK);

  by  TEC_POSITION_ID

   if first.TEC_POSITION_ID then  RF_RANK=1;

  else RF_RANK+1;

run;

But you would have gotten the same result with:

proc sort data = work.W62KKV70

   out = work.W62KKN4Z;

   by TEC_POSITION_ID ratingfaktor;

run;

data work.W62KKN4Z;

  set work.W62KKN4Z;

  by  TEC_POSITION_ID

   if first.TEC_POSITION_ID then  RF_RANK=1;

  else RF_RANK+1;

run;

Contributor
Posts: 38

PROC RANK on same values

Yay, of course Smiley Happy

Thanks a lot Art!

Thomas

Respected Advisor
Posts: 3,156

PROC RANK on same values

Posted in reply to thomash123

I am not aware that Proc Rank is capable of doing this. When it run into tied values, it ranks them the SAME regardless, because they are TIED. Those different options of tie= do not change this.

Now in addition to Art's solution, you can also take advantage of _n_ to do the job:

data have;

input A       B     ;

cards;

1222          180.00

1222          180.00

1222          260.00

1223          1

1223          4

1224          2

1224          5

1224          5

1224          5

;

proc sort data=have;

by a b;

run;

data want;

do _n_=1 by 1 until (last.a);

    set have;

by a;

    Rank=_n_;

output;

  end;

run;

proc print;run;

Regards,

Haikuo

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 591 views
  • 0 likes
  • 3 in conversation