Sorting Data
- Overview of Sorting Data
- Sorting by Column
- Sorting by Multiple Columns
- Specifying a Sort Order
- Sorting by Calculated Column
- Sorting by Column Position
- Sorting by Columns That Are Not Selected
- Specifying a Different Sorting Sequence
- Sorting Columns That Contain Missing Values
Overview of Sorting Data
You can sort query results with an ORDER BY clause by specifying any of the columns in the table, including columns that are not selected or columns that are calculated.
Unless an ORDER BY clause is included in the SELECT statement, then a particular order to the output rows, such as the order in which the rows are encountered in the queried table, cannot be guaranteed, even if an index is present. Without an ORDER BY clause, the order of the output rows is determined by the internal processing of PROC SQL, the default collating sequence of SAS, and your operating environment. Therefore, if you want your result table to appear in a particular order, then use the ORDER BY clause.
For more information and examples, see the ORDER BY Clause.
Calculated statistics can vary slightly, depending on the order in which observations are processed. Such variations are due to numerical error introduced by floating-point arithmetic, the results of which should be considered approximate and not exact. Order of observation processing can be affected by non-deterministic effects of multi-threaded or parallel processing. Order of processing can also be affected by inconsistent or non-deterministic ordering of observations produced by a data source, such as a DBMS delivering query results through an ACCESS engine. For more information, see Numerical Accuracy in SAS Software in SAS Language Reference: Concepts and Threading in Base SAS in SAS Language Reference: Concepts.
Sorting by Column
The following example selects countries and their populations from the Sql.Countries table and orders the results by population:
proc sql outobs=12;
title 'Country Populations';
select Name, Population format=comma10.
from sql.countries
order by Population;
quit;
Sorting by Column

Sorting by Multiple Columns
You can sort by more than one column by specifying the column names, separated by commas, in the ORDER BY clause. The following example sorts the Sql.Countries table by two columns, Continent and Name:
proc sql outobs=12;
title 'Countries, Sorted by Continent and Name';
select Name, Continent
from sql.countries
order by Continent, Name;
quit;
Sorting by Multiple Columns

Specifying a Sort Order
To order the results, specify ASC for ascending or DESC for descending. You can specify a sort order for each column in the ORDER BY clause.
When you specify multiple columns in the ORDER BY clause, the first column determines the primary row order of the results. Subsequent columns determine the order of rows that have the same value for the primary sort. The following example sorts the Sql.Features table by feature type and name:
proc sql outobs=12;
title 'World Topographical Features';
select Name, Type
from sql.features
order by Type desc, Name;
quit;
Specifying a Sort Order

Sorting by Calculated Column
You can sort by a calculated column by specifying its alias in the ORDER BY clause. The following example calculates population densities and then performs a sort on the calculated Density column:
proc sql outobs=12;
title 'World Population Densities per Square Mile';
select Name, Population format=comma12., Area format=comma8.,
Population/Area as Density format=comma10.
from sql.countries
order by Density desc;
quit;
Sorting by Calculated Column

Sorting by Column Position
You can sort by any column within the SELECT clause by specifying its numerical position. By specifying a position instead of a name, you can sort by a calculated column that has no alias. The following example does not assign an alias to the calculated Density column. Instead, the column position of 4 in the ORDER BY clause refers to the position of the calculated column in the SELECT clause:
proc sql outobs=12;
title 'World Population Densities per Square Mile';
select Name, Population format=comma12., Area format=comma8.,
Population/Area format=comma10. label='Density'
from sql.countries
order by 4 desc;
quit;
Sorting by Column Position

Sorting by Columns That Are Not Selected
You can sort query results by columns that are not included in the query. For example, the following query returns all the rows in the Sql.Countries table and sorts them by population, even though the Population column is not included in the query:
proc sql outobs=12;
title 'Countries, Sorted by Population';
select Name, Continent
from sql.countries
order by Population;
quit;
Sorting by Columns That Are Not Selected

Specifying a Different Sorting Sequence
SORTSEQ= is a PROC SQL statement option that specifies the sorting sequence for PROC SQL to use when a query contains an ORDER BY clause. Use this option only if you want to use a sorting sequence other than your operating environment's default sorting sequence. Possible values include ASCII, EBCDIC, and some languages other than English. For example, in an operating environment that supports the EBCDIC sorting sequence, you could use the following option in the PROC SQL statement to set the sorting sequence to EBCDIC:
proc sql sortseq=ebcdic;
Beginning with SAS 9.4M3, linguistic collation is supported with the SORTSEQ statement option. For more information, see SORTSEQ=sort-table | LINGUISTIC.
Sorting Columns That Contain Missing Values
PROC SQL sorts nulls, or missing values, before character or numeric data. Therefore, when you specify ascending order, missing values appear first in the query results.
The following example sorts the rows in the Continents table by the LowPoint column:
proc sql;
title 'Continents, Sorted by Low Point';
select Name, LowPoint
from sql.continents
order by LowPoint;
quit;
Because three continents have a missing value in the LowPoint column, those continents appear first in the output. Note that because the query does not specify a secondary sort, rows that have the same value in the LowPoint column, such as the first three rows of output, are not displayed in any particular order. In general, if you do not explicitly specify a sort order, then PROC SQL output is not guaranteed to be in any particular order.
Sorting Columns That Contain Missing Values
