How do you use PROC RANK in SAS?

The PROC RANK procedure in SAS is used to rank the data values in a range of data sets. It allows users to assign a rank to each observation based on the values of one or more variables, and then compare the ranking of observations across groups. PROC RANK also provides options to break ties when multiple observations have the same value, and to generate descriptive statistics for the rankings.


You can use PROC RANK in SAS to calculate the rank for one or more numeric variables.

Here are the four most common ways to use this procedure:

Method 1: Rank One Variable

proc rank data=original_data out=ranked_data;
   var var1;
   ranks var1_rank;
run;

Method 2: Rank One Variable by Group

proc rank data=original_data out=ranked_data;
   var var1;
   by var2;
   ranks var1_rank;
run;

Method 3: Rank One Variable into Percentiles

proc rank data=original_data groups=4 out=ranked_data;
   var var1;
   ranks var1_rank;
run;

Method 4: Rank Multiple Variables

proc rank data=original_data out=ranked_data;
   var var1 var2;
   ranks var1_rank var2_rank;
run;

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data original_data;
    input team $ points rebounds;
    datalines;
A 25 10
A 18 4
A 18 7
A 24 8
B 27 9
B 33 13
B 31 11
B 30 16
;
run;

/*view dataset*/
proc print data=original_data;

Example 1: Rank One Variable

The following code shows how to create a new variable called points_rank that ranks the points scored by each team:

/*rank points scored by team*/
proc rank data=original_data out=ranked_data;
   var points;
   ranks points_rank;
run;

/*view ranks*/
proc print data=ranked_data;

Any ties in points scored are assigned a mean rank. For example, the rows with the first and second lowest points scored both receive a rank of 1.5, since this is the average of 1 and 2.

Note that you can instead use the descending statement to assign the team with the most points the lowest rank:

/*rank points scored by team in descending order*/
proc rank data=original_data descending out=ranked_data;
    var points;
    ranks points_rank;
run;

/*view ranks*/
proc print data=ranked_data;

Example 2: Rank One Variable by Group

The following code shows how to create a new variable called points_rank that ranks the points scored, grouped by team:

/*rank points scored, grouped by team*/
proc rank data=original_data out=ranked_data;
    var points;
    by team;
    ranks points_rank;
run;

/*view ranks*/
proc print data=ranked_data;

Example 3: Rank One Variable into Percentiles

We can use the groups statement to rank variables into percentile groups. For example, we can rank each value of points into a quartile (four groups):

/*rank points into quartiles*/
proc rank data=original_data groups=4 out=ranked_data;
   var points;
   ranks points_rank;
run;

/*view ranks*/
proc print data=ranked_data;

The rows with the points values in the lowest quartile are assigned a group of 0, the rows with the points in the next lowest quartile are assigned a group of 1, and so on.

Note: To assign values into deciles instead, simply use groups=10.

Example 4: Rank Multiple Variables

The following code shows how to create multiple new variables to rank both points and rebounds:

proc rank data=original_data out=ranked_data;
   var points rebounds;
   ranks points_rank rebounds_rank;
run;

The following tutorials explain how to perform other common tasks in SAS:

x