📊 PROC RANK in SAS – Rank, Percentile, and Group Your Data Easily

Introduction

In data analysis, ranking values is essential for identifying top performers, segmenting data, and calculating percentiles. PROC RANK in SAS makes this process easy by assigning ranks, percentiles, or group numbers to numeric variables.

Proc Rank by Datahark


🔧 Syntax of PROC RANK

PROC RANK DATA=input_dataset OUT=output_dataset
RANKS=rank_variable <TIES=LOW|HIGH|MEAN|DENSE>; VAR variable_to_rank; BY group_variable; <GROUPS=n>; RUN;

Key Options Explained:

OptionDescription
DATA=Input dataset
OUT=Output dataset with new rank variable
RANKS=Name of the new variable that stores the rank
TIES=Specifies how tied values are handled (default is MEAN)
BYPerform ranking within each BY-group
VARVariable to rank
GROUPS=Divide data into equal-sized groups (like quantiles or deciles)

📌 Example 1: Basic Ranking

proc rank data=sashelp.class out=ranked_class;
var height; ranks height_rank; run;

Explanation:
Ranks students in sashelp.class by their height, storing the result in height_rank.


📌 Example 2: Ranking within Groups

proc sort data=sashelp.class out=sorted_class;
by sex; run; proc rank data=sorted_class out=ranked_sex; by sex; var weight; ranks weight_rank; run;

Explanation:
Ranks weight within each sex group.


📌 Example 3: Create Percentile or Quantile Groups

proc rank data=sashelp.class out=grouped_class groups=4;
var age; ranks age_quartile; run;

Explanation:
Divides age into 4 quartile groups (0 to 3).


📌 TIES= Option in Action

proc rank data=sashelp.class out=ranked_ties ties=low;
var height; ranks height_rank; run;

TIES= Options:

  • LOW – Lowest rank for all ties
  • HIGH – Highest rank for all ties
  • MEAN – Average rank (default)
  • DENSE – No gaps between ranks


✅ When to Use PROC RANK

  • Ranking top N values
  • Creating quantile-based bins (e.g., deciles, quartiles)
  • Calculating percentiles
  • Segmenting customers or products
  • Normalizing scorecards


🧠 Tips for Using PROC RANK

  • Always sort the dataset before using BY.
  • Use GROUPS= for percentiles or bucketing.
  • For multiple variables, use multiple VAR and RANKS pairs.
  • Combine with PROC SQL or PROC PRINT for better reporting.


📎 Final Thoughts

PROC RANK is a powerful yet simple procedure in SAS that enables effective data ranking and segmentation. It’s especially useful in scoring, customer segmentation, and exploratory data analysis.

Post a Comment

0 Comments