Sorting Data

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.

Sorting by Column

The following example selects countries and their populations from the SQL.COUNTRIES table and orders the results by population:
libname sql 'SAS-library';

proc sql outobs=12;
   title 'Country Populations';
   select Name, Population format=comma10.
      from sql.countries
      order by Population;
Note: When you use an ORDER BY clause, you change the order of the output but not the order of the rows that are stored in the table.
Note: The PROC SQL default sort order is ascending.
Sorting by Column
Country Populations

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:
libname sql 'SAS-library';

proc sql outobs=12;
   title 'Countries, Sorted by Continent and Name';
   select Name, Continent
      from sql.countries
      order by Continent, Name;
Sorting by Multiple Columns
Countries, Sorted by Continent and Name
Note: The results list countries without continents first because PROC SQL sorts missing values first in an ascending sort.

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:
libname sql 'SAS-library';

proc sql outobs=12;
   title 'World Topographical Features';
   select Name, Type
      from sql.features
      order by Type desc, Name;
Note: The ASC keyword is optional because the PROC SQL default sort order is ascending.
Specifying a Sort Order
World Topographical Features

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:
libname sql 'SAS-library';

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;
Sorting by Calculated Column
World Population Densities per Square Mile

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:
libname sql 'SAS-library';

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;
Note: PROC SQL uses a label, if one has been assigned, as a heading for a column that does not have an alias.
Sorting by Column Position
World Population Densities per Square Mile

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:
libname sql 'SAS-library';

proc sql outobs=12;
   title 'Countries, Sorted by Population';
   select Name, Continent 
      from sql.countries
      order by Population;
Sorting by Columns That Are Not Selected
Countries, Sorted by Population

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;
Note: SORTSEQ= affects only the ORDER BY clause. It does not override your operating environment's default comparison operations for the WHERE clause.
Operating Environment Information: See the SAS documentation for your operating environment for more information about the default and other sorting sequences for your operating environment.

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:
libname sql 'SAS-library';

proc sql;
   title 'Continents, Sorted by Low Point';
   select Name, LowPoint
      from sql.continents
      order by LowPoint;
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
Continents, Sorted by Low Point