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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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