*Copyright @ www.mycsg.in;


What is the word meaning of 'sort'?

  1. arrange systematically in groups; separate according to type.
  2. the arrangement of data in a prescribed sequence.

Common Scenarios where ordering (sorting of observations in a dataset) is required:

What does 'proc sort' do?

Create some example datasets

Sorting a dataset based on the values of a single variable in ascending sequence and creating a new dataset

Question Answer
How do we specify the name of input dataset to be used by proc sort? We need to specify the name of the input dataset using data= option on the proc sort statement
What happens if we do not provide data= option? The last created dataset in the current SAS session is used as input for proc sort
How do we specify the name of the output dataset in which the sorted observations are to be stored? We need to specify the name of the output dataset using out=option on the proc sort statement
What happens if we do not provide out= option? Input dataset gets overwritten with the sorted dataset
How do we instruct SAS to sort the observations based on particular variables? We need to specify the names of the sorting variables in the by statement
What happens if we do not provide by variables or a by statement? Returns an Error as by statement is a mandatory statment in proc sort
What is the default sort order for a variable? By default SAS sorts observations based on the ascending values of the variables listed

Create a dataset named class_sort_asc_age by sorting the observations of class01 dataset based on ascending values in age variable

Create a dataset named class_sort_asc_height by sorting the observations of class01 dataset based on ascending values in height variable

Question Answer
Which observation appears first when there is more than one record with the same value in the specified by variable? When there is more than one observation in the input dataset with the same value in the specified by variables, the observation which appears first in the input dataset with that value will appear first in the output dataset

Sorting a dataset based on the values of a single variable in descending sequence and creating a new dataset

Question Answer
How do you instruct SAS to sort the observations based on the descending values of a variable? We need to specify the keyword 'descending' before the name of the variable for which we want to sort based on descending values

Create a dataset named class_sort_dsc_age by sorting the observations of class01 dataset based on descending values in age variable

Create a dataset named class_sort_dsc_height by sorting the observations of class01 dataset based on descending values in height variable

Sorting a dataset based on multiple variables and creating a new dataset

Create a dataset named class_sort_age_sex by sorting the observations of class01 dataset based on ascending values in age and sex variables

Create a dataset named classes_sort_class_age_sex by sorting the observations of classes01 dataset based on ascending values in class, age and sex variables

Create a dataset named classes_sort_name_class by sorting the observations of classes01 dataset based on ascending values in name and class variables

Create a dataset named class_sort_aage_dheight by sorting class01 dataset based on ascending age and descending height

Create a dataset named class_sort_dage_dheight by sorting classes01 dataset based on descending age and descending height

Create a dataset named class_sort_dage_aheight by sorting classes01 dataset based on descending age and ascending height