SEARCH UNIVERSITY LIBRARIES
SPSS syntax is a programming language that is unique to SPSS. It allows you to write commands that run SPSS procedures, rather than using the graphical user interface.
Syntax allows users to perform tasks that would be too tedious or difficult to do using the drop-down menus. This is the case when you are re-running the same analysis many times, or doing complex transformations on data. Syntax also provides a record of how you transformed and analyzed your data, and allows you to instantly reproduce those steps at any time.
Note that the two methods of interacting with SPSS—drop-down menus and syntax—are not mutually exclusive. You can use both methods if you wish (they will produce the same results), or switch from one method to the other depending on the actions you want to perform and your comfort level with menus or syntax.
A comment is a line of text in a program that is not read by the computer as a command. Comments do not affect how the program functions; they exist purely for the humans reading and writing the program. Comments help the reader understand what the program is doing. In general, it is good practice to use brief but descriptive comments in your code. Your comments should be clear enough that a reader completely unfamiliar with your work can understand what your program is doing.
In SPSS syntax, placing an asterisk (
*) or a forward-slash followed by an asterisk (
/*) at the start of a line will turn all text on that line into a comment. Hitting the Enter key will create a new, un-commented line. Typically, comments in SPSS syntax are color-coded with the color gray.
By default, SPSS uses color and bolding to indicate the roles of the words in the syntax.
|Dark blue/purple||Procedure names; execution statements|
|Green||Statements associated with the given procedure|
|Dark red/orange||Option keywords|
|Black||Variable names; other text|
To open a new Syntax Editor window, click File > New > Syntax.
After you've opened a Syntax Editor window, you can start writing your syntax directly in this window. Alternatively, you can generate syntax while using the graphical user interface: almost all SPSS procedures accessed through the dropdown menus can generate syntax by clicking the Paste button instead of clicking OK/Run. After clicking the Paste button, the new syntax will automatically be added to your open Syntax Editor window.
To execute (or run) the commands, highlight the lines you want to run, then click Run > Selection, or press Ctrl + R on your keyboard.
You can save your SPSS syntax as an *.sps file so that you can re-use it later. To save your syntax file, make sure that you have the Syntax Editor window open and active, then click File > Save or File > Save As to save the syntax file.
To open a syntax file on your computer, click File > Open > Syntax. You can do this from any open window (including the Data View or Output View).
You can view the content of a syntax file (*.sps) using a text editor like Notepad or Notepad++, even on computers that do not have SPSS installed.
By default, SPSS conveniently records the syntax for all of the commands run in SPSS (whether you used drop-down menus or syntax) in a Journal File (extension “.jnl”). This is convenient if you wish to review what commands you ran or if you want to edit or save the syntax commands for future use.
To find out where SPSS is storing this Journal File, click Edit > Options. Click File Locations and you will see the pathname for the Journal File in the Session Journal area. You can also change the location where this file is stored.
To open the Journal File, click File > Open > Syntax. Find the folder in which the Journal File is stored. In the Files of type list, select “All Files (*.*).” Now your Journal File should appear (e.g., statistics.jnl). Select this file and click Open. Now you can see the commands you ran during the most recent SPSS session. You can also save this file for future use. The file will save as a “.sps” file.
Here is an example of how to run the same command using both drop-down menus and syntax. Although we could choose any command, we will run a simple descriptives command on the variable height in our sample data set.
To run the Descriptives command using the drop-down menus, click Analyze > Descriptive Statistics > Descriptives.
From the left menu, select the variable(s) you wish to run the descriptives command on, and use the arrow button to move them to the Variable(s) box. In this example, we select the variable Height. Click OK.
The results for the command will appear in the Output Viewer window.
Notice that there is also syntax printed above the descriptive statistics in the output. The syntax provides a text-based map of the command we just ran using drop-down menus. If we had executed this syntax in a Syntax Editor window instead, we would get the exact same results as we did using the drop-down menu.
Let’s now use syntax to run the same Descriptives procedure as before.
Reopen the Descriptives procedure. All of your previous settings should still be active. Instead of clicking OK, click Paste. This should open a new Syntax Editor file with the descriptives syntax in it. (Alternatively, you can create a new syntax file by clicking File > New > Syntax, and typing or copy/pasting the following syntax into that window.)
Notice that that the text in the syntax editor appears in certain colors, and some words become bold. These stylistic formats simply define different parts of the syntax command.
DESCRIPTIVES VARIABLES= Height
/STATISTICS=MEAN STDDEV MIN MAX.
When you are finished typing the syntax, you need to tell SPSS to run the command by clicking the green arrow at the top of the window. Voila! We have produced the very same output using both drop-down menus and syntax.
Note: You can copy the syntax from an output window and paste it into a new Syntax Editor window to re-use, modify, and save the syntax. To copy syntax from the output (in the Output Viewer window), simply click the syntax, copy it, and paste it into a Syntax Editor window.
Once you have syntax in the Syntax Editor window, you are free to modify the syntax and/or save it. For example, perhaps I decide that I want to look at some different variables, English and Writing, and I would like to get the range statistic instead of the minimum and maximum. I can easily modify the syntax I already have to accommodate these changes:
DESCRIPTIVES VARIABLES= English Writing
/STATISTICS=MEAN STDDEV RANGE.
Now when SPSS runs the descriptives command, it shows the range, mean, and standard deviation for the variables English and Writing.
After the previous example, it may seem like the menus are easier to use than syntax. So why would anyone want to use syntax?
The biggest benefits of using SPSS syntax are reproducibility and communication.
It is far easier to "retrace your steps" with syntax if you need to recall the modifications you've made to your dataset. It's also far easier to find and correct mistakes in your analysis if you have the syntax, as opposed to if you had used the menus.
Syntax can easily be modified and re-used on future projects, saving you time and effort in the long run.
If you are a Qualtrics user and have downloaded your "raw" survey data in SPSS format, using syntax to modify variable labels or compute new variables means that you don't have to worry if you lose the SPSS data file you've been working in; you can just re-download the raw data from Qualtrics and rerun your syntax to get your data back.
It is much easier to communicate to others what actions you performed in SPSS by showing someone your syntax than it is to describe how you used the menus. The content of the menus and dialog windows has changed over time with each version of SPSS, while the syntax has remained relatively unchanged (though there are a few notable exceptions).
In general, if you are working on a major project (like a thesis, dissertation, or research for publication), or if you are collaborating with others on data analysis, we strongly recommend using SPSS syntax.
The example above is only the beginning of learning how to write syntax commands. You can begin learning syntax by using the dropdown menus to call certain procedures and then clicking the Paste button to have SPSS write the corresponding syntax to a syntax file. You can also check out the Command Syntax Reference guide, which outlines all of the rules for writing syntax and provides examples.
To access the Command Syntax Reference, click Help > Command Syntax Reference. (This will open a PDF containing the reference manual.)