RANK Procedure

Example 3: Partitioning Observations into Groups Based on Ranks

Features:

PROC RANK statement option: GROUPS=

BY statement

VAR statement

Other features:

PRINT procedure

SORT procedure

Details

This example performs the following actions:
  • partitions observations into groups on the basis of values of two input variables
  • groups observations separately within BY groups
  • replaces the original variable values with the group values

Program

options nodate pageno=1 linesize=80 pagesize=60;
data swim;
   input Name $ 1-7 Gender $ 9 Back 11-14 Free 16-19;
   datalines;
Andrea  F 28.6 30.3
Carole  F 32.9 24.0
Clayton M 27.0 21.9
Curtis  M 29.0 22.6
Doug    M 27.3 22.4
Ellen   F 27.8 27.0
Jan     F 31.3 31.2
Jimmy   M 26.3 22.5
Karin   F 34.6 26.2
Mick    M 29.0 25.4
Richard M 29.7 30.2
Sam     M 27.2 24.1
Susan   F 35.1 36.1
;
proc sort data=swim out=pairs;
   by gender;
run;
proc rank data=pairs out=rankpair groups=3;
   by gender;
   var back free;
run;
proc print data=rankpair n;
   by gender;
   title 'Pairings of Swimmers for Backstroke and Freestyle';
run;

Program Description

Set the SAS system options. The NODATE option specifies to omit the date and time when the SAS job began. 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 SWIM data set. This data set contains swimmers' first names and their times, in seconds, for the backstroke and the freestyle. This example groups the swimmers into pairs, within male and female classes, based on times for both strokes so that every swimmer is paired with someone who has a similar time for each stroke.
data swim;
   input Name $ 1-7 Gender $ 9 Back 11-14 Free 16-19;
   datalines;
Andrea  F 28.6 30.3
Carole  F 32.9 24.0
Clayton M 27.0 21.9
Curtis  M 29.0 22.6
Doug    M 27.3 22.4
Ellen   F 27.8 27.0
Jan     F 31.3 31.2
Jimmy   M 26.3 22.5
Karin   F 34.6 26.2
Mick    M 29.0 25.4
Richard M 29.7 30.2
Sam     M 27.2 24.1
Susan   F 35.1 36.1
;
Sort the SWIM data set and create the output data set PAIRS. PROC SORT sorts the data set by Gender. This action is required to obtain a separate set of ranks for each group. OUT= creates the output data set PAIRS.
proc sort data=swim out=pairs;
   by gender;
run;
Generate the ranks that are partitioned into three groups and create an output data set. GROUPS=3 assigns one of three possible group values (0,1,2) to each swimmer for each stroke. OUT= creates the output data set RANKPAIR.
proc rank data=pairs out=rankpair groups=3;
Create a separate set of ranks for each BY group. The BY statement separates the rankings by Gender.
   by gender;
Replace the original values of the variables with the rank values. The VAR statement specifies that Back and Free are the variables to rank. With no RANKS statement, PROC RANK replaces the original variable values with the group values in the output data set.
   var back free;
run;
Print the data set. PROC PRINT prints the RANKPAIR data set. The N option prints the number of observations in each BY group. The TITLE statement specifies a title.
proc print data=rankpair n;
   by gender;
   title 'Pairings of Swimmers for Backstroke and Freestyle';
run;

Output: Listing

In the following output, the group values pair swimmers with similar times to work on each stroke. For example, Andrea and Ellen work together on the backstroke because they have the fastest times in the female class. The groups of male swimmers are unbalanced because there are seven male swimmers; for each stroke, one group has three swimmers.
               Pairings of Swimmers for Backstroke and Freestyle               1

----------------------------------- Gender=F -----------------------------------

                         Obs    Name      Back    Free

                           1    Andrea      0       1
                           2    Carole      1       0
                           3    Ellen       0       1
                           4    Jan         1       2
                           5    Karin       2       0
                           6    Susan       2       2

                                     N = 6


----------------------------------- Gender=M -----------------------------------

                         Obs    Name       Back    Free

                           7    Clayton      0       0
                           8    Curtis       2       1
                           9    Doug         1       0
                          10    Jimmy        0       1
                          11    Mick         2       2
                          12    Richard      2       2
                          13    Sam          1       1

                                     N = 7