Sort Data Task

About the Sort Data Task

The Sort Data task enables you to sort the table by any of its columns. The output data set from this task is a sorted table in the Work library.

Example: Sort the SASHELP.CLASS Data Set by Sex and Age

To create this example:
  1. In the Tasks section, expand the Data folder and double-click Sort Data. The user interface for the Sort Data task opens.
  2. On the Data tab, select the SASHELP.CLASS data set.
  3. To the Sort by role, assign the Sex and Age columns.
  4. To run the task, click Submit SAS Code.
The newly created WORK.SortDS data set is available from the Output Data tab. In the output data, the observations are first sorted by Sex (whether Female or Male). Within each group, the observations are sorted in ascending order by age.
Content of the WORK.SortDS Data Set

Assigning Data to Roles

To run the Sort Data task, you must assign a column to the Sort by role.
Role
Description
Sort by
When you assign one or more variables to this role, the table is grouped by the selected variable or variables. The order in which the variables appear within this role determines which variable is the primary sort key, which variable is the secondary sort key, and so on. The primary sort key is always the first variable that is listed within the Sort by role.
Columns to drop
When you assign one or more variables to this role, the output that is generated does not contain the specified variables. You can assign a maximum of (n – 1) variables to this role, where n is the total number of variables in the table.

Setting Options

Option Name
Description
Output Order
Sort order
specifies whether to display the output data in ascending or descending order.
Maintain original order within groups
maintains the order of the observations relative to each other within a BY group.
Duplicate Records
Keep all records
keeps all of the records that are in the output table, including all duplicate records.
Keep only the first record for each ‘Sort by’ group
eliminates any duplicate observations that have the same values for the Sort by group.
Results
Sort in place
specifies to sort the existing data set in place. If you select this option, the Sort task keeps any variables that you assigned to the Columns to drop role and keeps all duplicate records.
Output data set
specifies the name of the output data set.