Sorted Data Sets
The Sort Indicator
What Is a Sort Indicator?
After a data set is sorted, a sort indicator is added to the data set descriptor information.
The sort indicator is updated without a permanent sort of the data set by using the
SORTEDBY= data set option. The Sortedby and Validated sort information is updated
when the SORTEDBY= data set option is used.
The sort indicator contains some or all of the following sort information of a SAS data
set:
how the data set is sorted by which variable or variables
whether the order for a variable is descending or ascending
the character set used for character variables
the collating sequence used for ordering character data
collation rules if the data set is sorted linguistically
whether there is only one observation for any given BY group (use of NODUPKEY
option)
whether there are no adjacent duplicate observations (use of NODUPREC option)
whether the data set is validated
The sort indicator is set when a data set is sorted by a SORT procedure, an SQL
procedure with an ORDER BY clause, a DATASETS procedure MODIFY statement, or
a SORTEDBY= data set option. If the SORT or SQL procedures were used to sort the
data set, which is being sorted by SAS, the CONTENTS procedure output indicates that
the Validated sort information is YES. If the SORTEDBY= data set option was used to
sort the data set, which is being sorted by the user, the CONTENTS procedure output
indicates the Validated sort information is set to NO and the Sortedby sort information
is updated with the variable or variables specified by the data set option.
Data sets can be sorted outside of SAS. In that case, you might use the SORTEDBY=
data set option or the DATASETS procedure MODIFY statement to add the sort order to
the sort indicator. In this case, they are not validated. For more information, see
“Validating That a Data Set Is Sorted” on page 613.
To view the sort indicator information, use the CONTENTS procedure or the
CONTENTS statement in the DATASETS procedure. The following three examples
show the sort indicator information in the CONTENTS procedure output.
Example 1: Using No Sorting
The first example is a data set created without any type of sort:
options yearcutoff=1920;
libname myfiles 'C:\My Documents';
data myfiles.sorttest1;
input priority 1. +1 indate date7.
+1 office $ code $;
608 Chapter 27 SAS Data Sets
format indate date7.;
datalines;
1 03may11 CH J8U
1 21mar11 LA M91
1 01dec11 FW L6R
1 27feb10 FW Q2A
2 15jan11 FW I9U
2 09jul11 CH P3Q
3 08apr10 CH H5T
3 31jan10 FW D2W
;
proc contents data=myfiles.sorttest1;
run;
Note that the CONTENTS procedure output indicates there was no sort. SAS did not sort
the data set, and the user did not specify that the data is sorted.
Output 27.1 Contents of Sorttest1 Data Set – No Sorting
Example 2: Using the SORTEDBY= Data Set Option
In the second example, the data set is created using the SORTEDBY= data set option in
the DATA statement.
options yearcutoff=1920;
libname myfiles 'C:\My Documents';
data myfiles.sorttest1 (sortedby=priority descending indate);
input priority 1. +1 indate date7.
+1 office $ code $;
format indate date7.;
datalines;
1 03may01 CH J8U
Sorted Data Sets 609
1 21mar01 LA M91
1 01dec00 FW L6R
1 27feb99 FW Q2A
2 15jan00 FW I9U
2 09jul99 CH P3Q
3 08apr99 CH H5T
3 31jan99 FW D2W
;
proc contents data=myfiles.sorttest1;
run;
Note that the CONTENTS procedure output shows that the data set is sorted. Therefore,
a Sort Information section containing sort indicator information is created. In the Sort
Information section, the Sortedby information indicates the data set is sorted by the
PRIORITY variable and is in descending order by the INDATE variable. The data set is
sorted using the SORTEDBY= data set option, so the Validated information is NO. The
Character Set information for the data set is ANSI.
Output 27.2 Contents of Sorttest1 Data Set – Sorted
610 Chapter 27
SAS Data Sets

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.