SORT Procedure

Example 2: Sorting in Descending Order

Features:

This example BY statement option: : DESCENDING

Other features:

PROC PRINT

Data set: Account

This example does the following:
  • sorts the observations by the values of three variables
  • sorts one of the variables in descending order
  • prints the results

Program

proc sort data=account out=sorted;
   by town descending debt accountnumber;
run;
proc print data=sorted;
   var company town debt accountnumber;
   title  'Customers with Past-Due Accounts';
   title2 'Listed by Town, Amount, Account Number';
run;

Program Description

Create the output data set SORTED. OUT= creates a new data set for the sorted observations.
proc sort data=account out=sorted;
Sort by three variables with one in descending order. The BY statement specifies that observations should be first ordered alphabetically by town, then by descending value of amount owed, then by ascending value of the account number.
   by town descending debt accountnumber;
run;
Print the output data set SORTED. PROC PRINT prints the data set SORTED.
proc print data=sorted;
Specify the variables to be printed. The VAR statement specifies the variables to be printed and their column order in the output.
   var company town debt accountnumber;
Specify the titles.
   title  'Customers with Past-Due Accounts';
   title2 'Listed by Town, Amount, Account Number';
run;

Output

Note that sorting last by AccountNumber puts the businesses in Apex with a debt of $37.95 in order of account number.
Customers with Past-Due Accounts Listed by Town, Amount, Account Number