SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
thomash123
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

3 REPLIES 3
art297
Opal | Level 21

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;

thomash123
Calcite | Level 5

Yay, of course Smiley Happy

Thanks a lot Art!

Thomas

Haikuo
Onyx | Level 15

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

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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