Examples: RANK Procedure


Example 1: Ranking Values of Multiple Variables

Procedure features:

  • PROC RANK statement options:

    • DESCENDING

    • TIES=

  • RANKS statement

  • VAR statement

Other features:

  • PRINT procedure

This example

  • reverses the order of the ranks so that the highest value receives the rank of 1

  • assigns tied values the best possible rank

  • creates ranking variables and prints them with the original variables.

Program

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 CAKE data set. This data set contains each participant s last name , score for presentation, and score for taste in a cake-baking contest.

 data cake;     input Name $ 1-10 Present 12-13 Taste 15-16;     datalines;  Davis      77 84  Orlando    93 80  Ramey      68 72  Roe        68 75  Sanders    56 79  Simms      68 77  Strickland 82 79  ; 

Generate the ranks for the numeric variables in descending order and create the output data set ORDER. DESCENDING reverses the order of the ranks so that the high score receives the rank of 1. TIES=LOW gives tied values the best possible rank. OUT= creates the output data set ORDER.

 proc rank data=cake out=order descending ties=low; 

Create two new variables that contain ranks. The VAR statement specifies the variables to rank. The RANKS statement creates two new variables, PresentRank and TasteRank, that contain the ranks for the variables Present and Taste, respectively.

 var present taste;     ranks PresentRank TasteRank;  run; 

Print the data set. PROC PRINT prints the ORDER data set. The TITLE statement specifies a title.

 proc print data=order;     title "Rankings of Participants' Scores";  run; 

Output

 Rankings of Participants' Scores                      1                                           Present   Taste  Obs    Name          Present    Taste      Rank     Rank   1     Davis            77        84        3        1   2     Orlando          93        80        1        2   3     Ramey            68        72        4        7   4     Roe              68        75        4        6   5     Sanders          56        79        7        3   6     Simms            68        77        4        5   7     Strickland       82        79        2        3 

Example 2: Ranking Values within BY Groups

Procedure features:

  • PROC RANK statement options:

    • DESCENDING

    • TIES=

  • BY statement

  • RANKS statement

  • VAR statement

Other features:

  • PRINT procedure

This example

  • ranks observations separately within BY groups

  • reverses the order of the ranks so that the highest value receives the rank of 1

  • assigns tied values the best possible rank

  • creates ranking variables and prints them with the original variables.

Program

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 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

In the second district, Hinkley and Thrash tied with 912 votes . 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 

Example 3: Partitioning Observations into Groups Based on Ranks

Procedure features:

  • PROC RANK statement option:

    • GROUPS=

  • BY statement

  • VAR statement

Other features:

  • PRINT procedure

  • SORT procedure

This example

  • 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

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 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

The group values pair up 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 



Base SAS 9.1.3 Procedures Guide (Vol. 1)
Base SAS 9.1 Procedures Guide, Volumes 1, 2, 3 and 4
ISBN: 1590472047
EAN: 2147483647
Year: 2004
Pages: 260

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net