Skip to main Content

Using the ‘PRESORTED’ Option with Proc SORT in SAS 9.2

In SAS 9.2 Proc SORT the 'PRESORTED' option has been added; we can use this option if we believe that our data is already sorted, causing SAS to simply verify that our assumption is correct rather than re-sorting the data.

proc contents data=sashelp.buy;
run;

 

If we examine the partial output from Proc CONTENTS for this data set, we can see that the sorted flag is set to 'NO'

The CONTENTS Procedure

  
Observations          11
Variables             2
Indexes               0
Observation Length    16
Deleted Observations  0
Compressed            NO
Sorted                NO

 

Using the 'Presorted' option in PROC SORT, the log shows that SAS only verified that the data was in the correct order:

proc sort data=buy presorted;
    by date;
run;

  
9    proc sort data=buy presorted;
10       by date;
11   run;

  
NOTE: Sort order of input data set has been verified.
NOTE: There were 11 observations read from the data set WORK.BUY.
NOTE: Input data set is already sorted, no sorting done.

 

If, however, it turns out that data is not sorted as we supposed then the sort will be automatically performed and a note written to the log.

proc sort data=buy presorted;
    by amount;
run;

  
14   proc sort data=buy presorted;
15       by amount;
16   run;

  
NOTE: Input data set is not in sorted order.
NOTE: There were 11 observations read from the data set WORK.BUY.
NOTE: The data set WORK.BUY has 11 observations and 2 variables.

 

Caution should be exercised when using this option when directly reading data into Proc SORT from a DBMS. Since external databases can generally not be relied on to return data in the same order each time, this means that although we will still get our data sorted if required, data with the same values for our ‘by’ variables may be returned in a different order each time.