IBM Support

Rearranging variables alphabetically in the Data Editor

Troubleshooting


Problem

I need to rearrange my data so that my variables appear in the Data Editor in alphabetical order. How do I accomplish this?

Resolving The Problem

From SPSS 16 on, you can do this via the menu Data->Sort variables or via the SORT VARIABLES syntax command. In previous versions of SPSS, this can be done by using the FLIP command or via transposing cases as described below.

Provided your data file is less than 32,000 cases, this task can be done by transposing the data file, sorting it, then transposing it again to restore its original structure, as in

FLIP.
SORT CASES BY case_lbl.
FLIP NEWNAMES=case_lbl.

The remaining case_lbl variable in the file can be dropped at this point.

This process can also be done via the menus in any version of IBM SPSS Statistics which features them. In recent versions of the software, this procedure would accomplish the same thing:

First, flip the file using the following steps:
1. Go into Data->Transpose.
2. Highlight all your variables and move them to the Variables box.
3. Click "Ok".

Then, sort your file:
1. Go into Data->Sort Cases...
2. Highlight the variable "case_lbl" (this variable is created in the Transpose process) and move it to the "sort by..."box.
3. Select "Ascending".
4. Click "Ok".

Finally, flip the file back:
1. Go into Data->Transpose...
2. Highlight all the variables EXCEPT "case_lbl" and move them into the "Variables" box.
3. Highlight "case_lbl" and move it into the "Name Variable" box.
4. Click "Ok".

Your file is rearranged. As with the command syntax route, you may then delete the "case_lbl" variable, if you wish.

It is important to note that the relatively straightforward procedures described above will be effective only for files composed strictly of numeric variables with no variable or value labels, since

* any string variable in the data file will be lost in the act of transposing the file

* the formats and labels of your variables will be lost as well


With a few extra steps, however, the substantive content of string variables as well as variable and value labels can be preserved.

Here is an annotated example of this involving a file with any number of variable and value labels and a single string variable:

*This creates a numeric variable with the string variable's content as
its labels.
AUTORECODE VARS=stringvar /INTO numvar/PRINT.

*We then save a copy of the file to preserve the data dictionary for later use.
SAVE OUTFILE='format.sav'.

*We can then flip the file twice, as described in the simple case above.
FLIP.
SORT CASES BY case_lbl.
FLIP NEWNAMES=case_lbl.

*Matching the file to itself allows us to drop the unneeded variables.
MATCH FILES FILE=*/DROP stringvar case_lbl.

*The file currently has no labels (including those that tell us what the
former string values in "stringvar" were) or formatting information. We
can, however, apply the dictionary from the backup version of the file
to restore this.
APPLY DICTIONARY FROM='format.sav'.
EXE.


In the above...

*..."stringvar" is your string variable(s)(You'll need to specify each on the AUTORECODE command)...

*..."numvar" is your newly recoded variable...

*..."case_lbl" is an identifier variable created in the FLIP procedure...

*... and "format" is your data file with the string variables converted to numeric ones, for purposes of reimposing the variable labels and formats.

As with the simple case of numeric variables with no additional dictionary information, you may also perform this procedure through the menus, if you wish.


First, recode the string variables in your file:

1. Go into Transform->Automatic Recode.
2. Specify your string variable(s) and move into the "Variable->New Name" box.
3. Enter the new names and click the "New Name" button.
4. Click "Ok".

Save the resulting file.

Then, flip your file:
1. Go into Data->Transpose.
2. Highlight all your variables and move them to the Variables box.
3. Click "Ok".

Next, sort your file:
1. Go into Data->Sort Cases...
2. Highlight the variable "case_lbl" (this variable is created in the Transpose process) and move it to the "sort by..."box.
3. Select "Ascending".
4. Click "Ok".

Then, flip the file back:
1. Go into Data->Transpose...
2. Highlight all the variables EXCEPT "case_lbl" and move them into the "Variables" box.
3. Highlight "case_lbl" and move it into the "Name Variable" box.
4. Click "Ok".

Your file is rearranged. You may then delete the "case_lbl" variable and your string variables, if you wish.

Finally, Go into Data->Apply Data Dictionary and choose the file you saved previously. This will restore your labels and variable formats.

If your file is greater than 32,000 cases, or you don't wish to transpose the file for some reason, you may also sort the variables by using the syntax SAVE OUTFILE with the KEEP subcommand, listing your variables in the order you wish them to appear. A list of your variable names can be copied from the name column of the variable view, pasted as a string variable into a new IBM SPSS Statistics file, and sorted. Then recopy them and paste them into the syntax window after the KEEP subcommand.

Another way to get this list of your variables in alphabetical order is to first ensure that you've set your variable list options to display variable names, in alphabetical order (Edit->Options, General tab). If you have SPSS 11, this takes effect immediately; if you have an older version, you must exit and restart SPSS. Next, bring up the Frequencies dialog (Analyze->Descriptive Statistics->Frequencies), select all the variables (ctrl+a) and move them into the target list. Don't run the command: just paste. You will then have in the syntax window an alphabetical list of your variables which you can then put into a KEEP subcommand.

Run the resulting SAVE OUTFILE with the KEEP subcommand containing that variable list on your original data file. The new copy will have the variables sorted in alphabetical order. See your syntax guide for further advice on using SAVE OUTFILE

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF033","label":"Windows"}],"Version":"Not Applicable","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

17989

Document Information

More support for:
IBM SPSS Statistics

Software version:
Not Applicable

Operating system(s):
Windows

Document number:
417797

Modified date:
16 April 2020

UID

swg21476187

Manage My Notification Subscriptions