SORT Procedure

Example 3: Maintaining the Relative Order of Observations in Each BY Group

Features:

PROC SORT statement option: : EQUALS | NOEQUALS

Other features:

PROC PRINT

Details

This example does the following:
  • sorts the observations by the value of the first variable
  • maintains the relative order with the EQUALS option
  • does not maintain the relative order with the NOEQUALS option

Program

data insurance;
   input YearsWorked 1 InsuranceID 3-5;
   datalines;
5 421
5 336
1 209
1 564
3 711
3 343
4 212
4 616
;
proc sort data=insurance out=byyears1 equals;
   by yearsworked;
run;
proc print data=byyears1;
   var yearsworked insuranceid;
   title 'Sort with EQUALS';
run;
proc sort data=insurance out=byyears2 noequals;
   by yearsworked;
run;
proc print data=byyears2;
   var yearsworked insuranceid;
   title 'Sort with NOEQUALS';
run;

Program Description

Create the input data set INSURANCE. INSURANCE contains the number of years worked by all insured employees and their insurance IDs.
data insurance;
   input YearsWorked 1 InsuranceID 3-5;
   datalines;
5 421
5 336
1 209
1 564
3 711
3 343
4 212
4 616
;
Create the output data set BYYEARS1 with the EQUALS option. OUT= creates a new data set for the sorted observations. The EQUALS option maintains the order of the observations relative to each other.
proc sort data=insurance out=byyears1 equals;
Sort by the first variable. The BY statement specifies that the observations should be ordered numerically by the number of years worked.
   by yearsworked;
run;
Print the output data set BYYEARS1. PROC PRINT prints the data set BYYEARS1.
proc print data=byyears1;
Specify the variables to be printed. The VAR statement specifies the variables to be printed and their column order in the output.
   var yearsworked insuranceid;
Specify the title.
   title 'Sort with EQUALS';
run;
Create the output data set BYYEARS2. OUT= creates a new data set for the sorted observations. The NOEQUALS option will not maintain the order of the observations relative to each other.
proc sort data=insurance out=byyears2 noequals;
Sort by the first variable. The BY statement specifies that the observations should be ordered numerically by the number of years worked.
   by yearsworked;
run;
Print the output data set BYYEARS2. PROC PRINT prints the data set BYYEARS2.
proc print data=byyears2;
Specify the variables to be printed. The VAR statement specifies the variables to be printed and their column order in the output.
   var yearsworked insuranceid;
Specify the title.
   title 'Sort with NOEQUALS';
run;

Output

Note that sorting with the EQUALS option versus sorting with the NOEQUALS option causes a different sort order for the observations where YearsWorked=3.
Sort with EQUALS
Sort with NOEQUALS