How to Use PROC RANK in SAS (With Examples)


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;

The team with the most points receives the highest rank and the team with the lowest points receives the lowest rank.

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;

Additional Resources

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

How to Use Proc Summary in SAS
How to Use Proc Tabulate in SAS
How to Use PROC Transpose in SAS
How to Create Frequency Tables in SAS

Leave a Reply

Your email address will not be published.