While working in SPSS, it's pretty common to reorder your variables. This tutorial shows how to do so the right way. We encourage you try the examples for yourself by downloading and opening hotel_evaluation.sav, a screenshot of which is shown below.
SPSS Reorder Variables Example 1
Right, the most common way for reordering variables in SPSS is by running ADD FILES. Before explaining how it works, let's first show that it works in the first place. So suppose we'd like to swap the variables fname and sex. Running the syntax below does just that.
add files file *
/keep id sex fname all.
execute.
Result
How Does It Work?
Remember that ADD FILES
merges data sources holding different cases but similar variables. Note that in SPSS syntax, an * addresses the active dataset (often the only data that's open in SPSS).
Now, if we specify this as the only data source, it gets merged with nothing. This means that running
add files file *.
does absolutely nothing whatsoever. However, this command becomes useful if we add a /keep
subcommand which tells SPSS which variables to keep (not delete) and in which order.
In our first example, we decided to keep id sex fname
. Note that the original order here was id fname sex
.
Finally, all
is a special keyword that addresses all other variables in their original order. Omitting all
deletes these variables from our data. The final result of all this is that we basically do nothing except keep all variables in a different order than previously, which is exactly what we were intending.
SPSS Reorder Variables Example 2
Right, we just swapped two variables in our data. But what about moving entire blocks of variables? For instance, suppose we want to have variables q1 through q5 right after fname in our data. Well, this doesn't pose any challenge whatsoever if we use SPSS’ TO keyword. The syntax below shows just how easily it's done.
add files file */keep id to fname q1 to q5 all.
execute.
Result
SPSS Reorder Variables Example 3
Recap: if we're having some data open in SPSS, we can easily reorder our variables by using an ADD FILES command with a keep
subcommand.
Is that the only way to get it done? Well, no. If we're done with a dataset, we may use the exact same keep
subcommand in a save
command as shown below (first example). This is never really necessary but it may save a tiny amount of syntax and processing time.
Just for the sake of completeness, if we know in advance which variables are present in our data, we may also reorder them while opening the data file with get
.
SPSS Reorder Variables Syntax Examples
save outfile '10_all_data_prepared.sav'
/keep q1 to q5 all.
*2. Reorder variables when opening data file.
get file 'hotel_evaluation.sav'
/keep q1 to q5 all.
SPSS SORT VARIABLES
There's just one more thing that we need to mention: SPSS (starting from version 16) also has a SORT VARIABLES
command. Like so you can sort variables according to variable name, variable type, variable label, format or a couple of other properties.
We obviously needed to mention this command here. However, we very rarely use it in practice. The reason is that the aforementioned sorting options virtually never correspond to the order we desire for our variables. Those who like to give it a quick try anyway, may run something like
sort variables by name.
SPSS Reorder Variables - Final Note
In some cases, you may need to sort your variables in a structured manner that's more complex than covered by this tutorial. An example is sorting (many) variables according to subscripts in their variable names. The way to get such cases done efficiently is by using Python in SPSS.
THIS TUTORIAL HAS 22 COMMENTS:
By Michelle on August 31st, 2022
I have copied and pasted the results from the DISPLAY DICTIONARY into excel but the variables are not in the same order and the response options are often slightly different (eg, one survey had 3 response options for the variable and the second survey had an additional response option). Is there a formula to sort and match the by variable name?
By Ruben Geert van den Berg on August 31st, 2022
You could try a VLOOKUP or similar in Excel but it's somewhat tedious.
I used to fix this problem by converting the DISPLAY DICTIONARY tables into SPSS data files (with OMS) and merging them with MATCH FILES but that's fairly tedious as well. Also, you need to do it at least twice (for variable/value labels).
Another option is to try the SPSSINC COMPARE DATASETS extension although I've no experience with it.
On recent SPSS versions, you may find it under Data -> Compare Datasets. If not present, look for it under Extensions -> Extension Hub or try Googling it and download/install from Github.
In any case, it's a pretty nasty problem that doesn't have any magic solution (even though this wouldn't take too much time to develop)...