SAS : PROC RANK

Deepanshu Bhalla 11 Comments

This tutorial explains how to calculate the rank for one or more variables using PROC RANK.

Introduction

In SAS, there are multiple ways to calculate overall rank or rank by a grouping variable. In data step, it can be done via RETAIN statement. SAS made it easy to compute rank with PROC RANK.

Create Sample Data

The following SAS program creates a dataset which will be used to explain examples in this tutorial.

data temp;
input ID Gender $ Score;
cards;
1 M 33
2 M 94
3 M 66
4 M 46
5 F 92
6 F 95
7 F 18
8 F 11
;
run;
How to Compute Rank of a Variable

In the following SAS Program, we are calculating the rank of the variable "Score".

proc rank data= temp out = result;
var Score;
ranks ranking;
run;
Notes :
  1. The OUT option is used to store output of the rank procedure.
  2. The VAR option is used to specify numeric variable (s) for which you want to calculate rank
  3. The RANKS option tells SAS to name the rank variable
  4. By default, it calculates rank in ascending order.
SAS : PROC RANK
Reverse order of ranking (Descending)

Suppose you need to assign the largest value of a variable as rank 1 and the last rank to the lowest value. The descending keyword tells SAS to sort the data in descending order and assign rank to the variable accordingly.

proc rank data= temp descending out = result;
var Score;
ranks ranking;
run;
Percentile Ranking

Suppose you need to split the variable into four parts, you can use the groups option in PROC RANK. It means you are telling SAS to assign only 4 ranks to a variable.

proc rank data= temp descending groups = 4 out = result;
var Score;
ranks ranking;
run;

Use GROUPS=4 for quartile ranks, and GROUPS=10 for decile ranks, GROUPS = 100 for percentile ranks.

Ranking by Group

Suppose you need to calculate rank by a grouping variable (Gender). To accomplish this task, you can use the by statement in proc rank. Please note that it is required to sort the data before using by statement.

proc sort data = temp;
by gender;
run;

proc rank data= temp descending out = result;
var Score;
ranks ranking;
by Gender;
run;
How to Handle Ties When Ranking

Let's create a sample dataset. See the variable "score" having same values (33 appearing twice).

data temp2;
input ID Gender $ Score;
cards;
1 M 33
2 M 33
3 M 66
4 M 46;
run;

To handle duplicate values in ranking, you can use option TIES = HIGH | LOW | MEAN | DENSE in PROC RANK.

See the comparison between these options in the image below -

SAS : Handle Ties in PROC RANK
proc rank data= temp2 ties = dense out = result;
var Score;
ranks rank_dense;
run;
  1. LOW - assigns the smallest of the corresponding ranks.
  2. HIGH - assigns the largest of the corresponding ranks.
  3. MEAN - assigns the mean of the corresponding ranks (Default Option).
  4. DENSE - assigns the smallest of the corresponding rank and add +1 to the next rank (don't break sequence)
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

11 Responses to "SAS : PROC RANK"
  1. very good and easy explanation....
    Please add predictive modelling steps...step by step.

    ReplyDelete
  2. I didn't get the concept of ties that you have explained in the last!

    ReplyDelete
    Replies
    1. Thank you for your feedback. I have added more description to the concept of ties. Hope it helps.

      Delete
  3. Nice explanation... keep going guys.. Thanks!!

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Nice explanation..

    ReplyDelete
  6. How can we get rank for character variables

    ReplyDelete
  7. How can I get the cuts of deciles along with ranks and frequency as provided by proc rank?

    ReplyDelete
  8. Can you please the proc rank concept along with proc sql?

    ReplyDelete
  9. gracias, muy buena explicación

    ReplyDelete
Next → ← Prev