RANK Procedure

Example 2: Ranking Values within BY Groups

Features:
PROC RANK statement options:
DESCENDING
TIES=

BY statement

RANKS statement

VAR statement

Other features:

PRINT procedure

Details

This example performs the following actions:
  • ranks observations separately within BY groups
  • reverses the order of the ranks so that the highest value receives the rank of 1
  • assigns the best possible rank to tied values
  • creates ranking variables and prints them with the original variables

Program

options nodate pageno=1 linesize=80 pagesize=60;
data elect;
   input Candidate $ 1-11 District 13 Vote 15-18 Years 20;
   datalines;
Cardella    1 1689 8
Latham      1 1005 2
Smith       1 1406 0
Walker      1  846 0
Hinkley     2  912 0
Kreitemeyer 2 1198 0
Lundell     2 2447 6
Thrash      2  912 2
;
proc rank data=elect out=results ties=low descending;
   by district;
   var vote years;
   ranks VoteRank YearsRank;
run;
proc print data=results n;
   by district;
   title 'Results of City Council Election';
run; 

Program Description

Set the SAS system options. The NODATE option specifies to omit the date and time when the SAS job begins. The PAGENO= option specifies the page number for the next page of output that SAS produces. The LINESIZE= option specifies the line size. The PAGESIZE= option specifies the number of lines for a page of SAS output.
options nodate pageno=1 linesize=80 pagesize=60;
Create the ELECT data set. This data set contains each candidate's last name, district number, vote total, and number of years' experience on the city council.
data elect;
   input Candidate $ 1-11 District 13 Vote 15-18 Years 20;
   datalines;
Cardella    1 1689 8
Latham      1 1005 2
Smith       1 1406 0
Walker      1  846 0
Hinkley     2  912 0
Kreitemeyer 2 1198 0
Lundell     2 2447 6
Thrash      2  912 2
;
Generate the ranks for the numeric variables in descending order and create the output data set RESULTS. DESCENDING reverses the order of the ranks so that the highest vote total receives the rank of 1. TIES=LOW gives tied values the best possible rank. OUT= creates the output data set RESULTS.
proc rank data=elect out=results ties=low descending;
Create a separate set of ranks for each BY group. The BY statement separates the rankings by values of District.
   by district;
Create two new variables that contain ranks. The VAR statement specifies the variables to rank. The RANKS statement creates the new variables, VoteRank and YearsRank, that contain the ranks for the variables Vote and Years, respectively.
   var vote years;
   ranks VoteRank YearsRank;
run;
Print the data set. PROC PRINT prints the RESULTS data set. The N option prints the number of observations in each BY group. The TITLE statement specifies a title.
proc print data=results n;
   by district;
   title 'Results of City Council Election';
run; 

Output: Listing

In the following output, Hinkley and Thrash tied with 912 votes in the second district. They both receive a rank of 3 because TIES=LOW.
                        Results of City Council Election                       1

---------------------------------- District=1 ----------------------------------

                                                    Vote    Years
               Obs    Candidate    Vote    Years    Rank     Rank

                1     Cardella     1689      8        1       1
                2     Latham       1005      2        3       2
                3     Smith        1406      0        2       3
                4     Walker        846      0        4       3

                                     N = 4


---------------------------------- District=2 ----------------------------------

                                                     Vote    Years
              Obs    Candidate      Vote    Years    Rank     Rank

               5     Hinkley         912      0        3       3
               6     Kreitemeyer    1198      0        2       3
               7     Lundell        2447      6        1       1
               8     Thrash          912      2        3       2

                                     N = 4