Sorting cases or variables in SPSS

Sorting a dataset rearranges the rows with respect to one or more variables. This tutorial discusses how to sort data using the drop-down menus in SPSS. The data used for example is available to download here.

1. Sorting Data

Sorting data allows us to re-organize the data in ascending or descending order with respect to a specific variable. Some procedures in SPSS require that your data be sorted in a certain way before the procedure will execute. There are two options for sorting data:

We cover how to perform each sorting option below.

2. Sort Cases

Sorting cases will rearrange the rows based on a given variable (or variables). The values for the selected variables can be sorted in ascending (smallest to largest, or alphabetical) or descending order (largest to smallest, or reverse alphabetical).

Once you sort the cases of a dataset, it is not possible to “un-sort” the data to its original order. If the original order of your rows is important, make sure you have a variable of that specifically and uniquely identifies the correct order of the cases first! That way, you can return to the original row order by sorting on the “order identifier” variable.

If you do not have an “order ID” variable in your dataset, a convenient way to generate one is to use the system variable $CASENUM. You can use the Compute Variables procedure (simply enter $CASENUM in the Numeric Expression box), or by running the following syntax after all of your data has been entered:

COMPUTE id=$CASENUM.
EXECUTE.

Sorting from the Data View

In the Data View, you can quickly sort your data with respect to a single variable by right-clicking on the variable name and selecting Sort Ascending or Sort Descending.

Sorting with the Sort Cases procedure

If you want to sort your data with respect to two or more variables, or if you want to have the sorted data written to a new file, you’ll want to use the Sort Cases procedure:

You can check that your cases were sorted correctly by visually inspecting the data in Data View. In this example, cases have been sorted according to class rank, and then sorted by most to least recent birthdate.

Note: SPSS considers missing values the “smallest” value, so they will appear first if sorting in ascending order, and will appear last if sorting in descending order.

Sorting with Syntax

This syntax performs the same sort shown in the screencap above (sort by ascending class rank, then descending birth date).

SORT CASES BY Rank(A) bday(D).

3. Sort Variables

Sorting variables will rearrange the order of the variables (columns) in your data. Variables can be sorted on only one attribute at a time: Name, Type, Width, Decimals, Label, Values, Missing, Columns, Align, Measure, or a custom attribute. Variables can be sorted in ascending or descending order with respect to the selected attribute.

To sort variables, follow these steps:

Now your variables will be sorted according to the attribute you selected. In this example, the variables are sorted in ascending order according to their names (i.e., alphabetically).

Syntax

SORT VARIABLES BY NAME (A).

Leave a Reply

Your email address will not be published. Required fields are marked *