Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To generate a ranking for a defined set of rows in SAS, you can use the RANK function. The process is as follows:

  1. Sort the data by the variable(s) that you want to rank by.

  2. Create a new variable to hold the rankings.

  3. Use the RANK function to assign a rank to each row based on the sorted variable(s).

For example, suppose you have a data set called sales_data with variables for salesperson name and sales amount. If you want to generate a ranking of the top salespeople based on their sales amount, you could do the following:

SORT sales_data;

DATA salesdata; SET salesdata; RANK = RANK(-sales_amount); RUN;

This code sorts the data by salesamount in descending order, creates a new variable called RANK, and assigns a rank to each row based on the salesamount variable. The - sign before the sales_amount variable in the RANK function tells SAS to rank the data in descending order. The resulting data set will have a new variable called RANK that contains the ranking for each row.