SORT Procedure

Example 1: Sorting by the Values of Multiple Variables

Features:

PROC SORT statement option: : OUT=

BY statement

Other features:

PROC PRINT

This example does the following:
  • sorts the observations by the values of two variables
  • creates an output data set for the sorted observations
  • prints the results

Program

data account;
   input Company $ 1-22 Debt 25-30 AccountNumber 33-36
         Town $ 39-51;
   datalines;
Paul's Pizza             83.00  1019  Apex
World Wide Electronics  119.95  1122  Garner
Strickland Industries   657.22  1675  Morrisville
Ice Cream Delight       299.98  2310  Holly Springs
Watson Tabor Travel      37.95  3131  Apex
Boyd & Sons Accounting  312.49  4762  Garner
Bob's Beds              119.95  4998  Morrisville
Tina's Pet Shop          37.95  5108  Apex
Elway Piano and Organ    65.79  5217  Garner
Tim's Burger Stand      119.95  6335  Holly Springs
Peter's Auto Parts       65.79  7288  Apex
Deluxe Hardware         467.12  8941  Garner
Pauline's Antiques      302.05  9112  Morrisville
Apex Catering            37.95  9923  Apex
;
proc sort data=account out=bytown;
   by town company;
run;
proc print data=bytown;
   var company town debt accountnumber;
   title  'Customers with Past-Due Accounts';
   title2 'Listed Alphabetically within Town';
run;

Program Description

Create the input data set ACCOUNT. ACCOUNT contains the name of each business that owes money, the amount of money that it owes on its account, the account number, and the town where the business is located.
data account;
   input Company $ 1-22 Debt 25-30 AccountNumber 33-36
         Town $ 39-51;
   datalines;
Paul's Pizza             83.00  1019  Apex
World Wide Electronics  119.95  1122  Garner
Strickland Industries   657.22  1675  Morrisville
Ice Cream Delight       299.98  2310  Holly Springs
Watson Tabor Travel      37.95  3131  Apex
Boyd & Sons Accounting  312.49  4762  Garner
Bob's Beds              119.95  4998  Morrisville
Tina's Pet Shop          37.95  5108  Apex
Elway Piano and Organ    65.79  5217  Garner
Tim's Burger Stand      119.95  6335  Holly Springs
Peter's Auto Parts       65.79  7288  Apex
Deluxe Hardware         467.12  8941  Garner
Pauline's Antiques      302.05  9112  Morrisville
Apex Catering            37.95  9923  Apex
;
Create the output data set BYTOWN. OUT= creates a new data set for the sorted observations.
proc sort data=account out=bytown;
Sort by two variables. The BY statement specifies that the observations should be first ordered alphabetically by town and then by company.
   by town company;
run;
Print the output data set BYTOWN. PROC PRINT prints the data set BYTOWN.
proc print data=bytown;
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 Alphabetically within Town';
run;

Output

Customers with Past-Due Accounts Listed Alphabetically within Town