Sorting data in SPSS involves reordering the records or cases (rows) in your data set based on the values for one or more of the variables (fields or columns) in that data set.
Some procedures in SPSS require that you sort your data in a specific way before you use them. In addition, sorting can help you to visualize and explore your data.
In this quick tutorial, we will show you two methods for sorting your records in SPSS. The first (and quickest) method can be used if you want to sort your data on one variable only. The second method can be used to sort your data on one or more variables.
Warning: Once you sort your data, the original order of your data set will be lost unless it includes a unique identifier that identifies the correct order of your records.
Import your data into SPSS. (See our tutorials on importing data from Excel or MySQL into SPSS).
Our fictitious data set contains the Research Methods course grades for 60 students (first 20 records displayed) enrolled in one of three majors – Sociology, Political Science, and Economics.
Method 1: Sort Records on One Variable in the Data View
- Right-click on the variable name
- Select Sort Ascending or Sort Descending as applicable
This is the quickest method to sort your data in SPSS, but it works only if you are sorting your data on just one variable. We will illustrate this method by sorting the above dataset on the ID variable.
In the Data View, right-click on the variable name (ID in our example) by which you want to sort your data. Select Sort Ascending if you want the records with the lowest values on your sorting variable to appear first (as in our example). Select Sort Descending if you want the records with the highest values on your sorting variable to appear first.
SPSS will now display your sorted data in the Data View.
As you can see in the screenshot below, our records are now sorted on the variable “ID.” Note that the data for each case or record has remained together. For example, the record with ID 7 appeared in row 13 before we sorted our data. You can see that the this record (ID 7) display the same data for Gender (Male), Major (Sociology), and Research Methods grade (76) as it did before the records were sorted.
Method 2: Sort Records on One or More Variables Using the Data Drop Down Menu
- Click Data -> Sort Cases
- Click Reset (recommended)
- Move the variable(s) on which you want to sort your data to the Sort by box
- Select Ascending or Descending for each variable as applicable
- Check the Save file with sorted data option if you want to keep the original order of your records
- Click OK.
This second method is the only method you can use to sort your data set on multiple variables in SPSS. We will illustrate this method by sorting the above dataset by Major then Gender. That is records will be sorted by Major and, within each Major, they will be sorted by Gender.
From the SPSS menu, select Data -> Sort Cases.
This brings up the Sort Cases dialog box illustrated below.
Click the Reset button to clear any previous settings (recommended).
On the left-hand side of the dialog box, you will see a list of the variables in your data set. Select the first variable on which you want to sort your data set (Major in our example) and use the arrow button to move it into the Sort by box.
By default, SPSS will sort records in ascending order of the variable(s) you select and an (A) will appear after the variable name in the Sort by box. To sort your data in descending order, select the Descending option under Sort Order. You will then see a (D) after that variable name.
If you want to sort your data set on multiple variables, select the second sorting variable (Gender in our example) and use the arrow button to move it to the Sort by box. Remember to select the Descending option if you want SPSS to order your records in descending order for this variable. Repeat this process for all the variables on which you wish to sort your data. As you can see in the screenshot below, we have opted to select on Major in ascending order, followed by Gender in descending order.
When you select multiple sorting variables (as we have), these variables will appear in the Sort by box in the order in which you selected them. SPSS will sort your records starting with the first variable in the list. For the second variable selected, your records will be sorted within categories of the first variable, and so on. So, the records in our dataset will be sorted first by Major (in ascending order) and then, within each Major, by Gender (in descending order).
Note that your records will be sorted using the variable values contained in your data set, not the variable labels. For example, if we had used the values 1 for Sociology, 2 for Political Science and 3 for Economics (we didn’t), sorting on the Major variable in ascending order would mean that the records for Sociology students would be listed first, followed by those for Political Science students, and then Economics students.
If you want to keep the original order of your records, check the Save file with sorted data option. Then select the File button to save the sorted data in a new file.
As you can see, SPSS has sorted our data so that records for students in the Economics major appear first, followed by those for students in the Political Science major. Within the records for each Major, male students appear first (because we selected descending order for the Gender variable).
That’s it for this tutorial. You should now be able to sort your data file on one or more variables in SPSS.