Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 39 ยท Syntax and Automation

SPSS Syntax Basics: Commands, Subcommands and the Syntax Editor

Learn SPSS syntax from scratch: command structure, periods, subcommands, comments, running selections, the Syntax Editor, the log and a starter analysis script.

Menus are great for learning, but a menu click leaves no trace. Syntax is SPSS's command language: a plain text script that records exactly what you did and can be rerun on new data in seconds. Researchers who can read and write syntax work faster, make fewer mistakes and can show reviewers their method.

Anatomy of a command

Text
FREQUENCIES VARIABLES=gender age
  /STATISTICS=MEAN MEDIAN STDDEV
  /BARCHART FREQ.
  • The command name (FREQUENCIES) comes first. Convention is UPPER CASE; SPSS does not care about case for keywords, but variable names are case-insensitive too, so Age and age are the same variable.
  • Specifications follow: VARIABLES=gender age (names separated by spaces).
  • Subcommands start with a slash (/STATISTICS=...) and may sit on new lines.
  • The command ends with a period (full stop). Forgetting it is the number one beginner error: the next command is swallowed into this one and you get a confusing error.

Spacing and line breaks are free. Indentation is for you. A line can be as long as 251 characters; to continue a long command just keep going on the next line (the period decides where it ends).

Comments

Text
* This is a comment. It must end with a period.
COMMENT Another way, also ending with a period.
/* This one is inline and can sit inside a command */

Comment liberally: the date, the purpose and any decision ("exclude case 17: duplicate").

The Syntax Editor

Open it with File > New > Syntax (or File > Open > Syntax to open a .sps file). Useful features:

  • Auto-complete as you type (Ctrl+Space shows suggestions) and colour coding for commands, keywords and comments.
  • A Run menu: All, Selection (Ctrl+R, runs highlighted text or the command where the cursor is), Current and To End. Run commands in small blocks while developing.
  • Breakpoints and step-through in newer versions for long scripts.
  • Clear results: nothing is deleted from the data; run output goes to the Output Viewer.

Syntax is saved as a plain text .sps file you can open in any editor, put in version control, or email.

Getting your data in and out with syntax

Text
GET FILE='C:\Projects\survey\survey.sav'.
DATASET NAME survey WINDOW=FRONT.

SAVE OUTFILE='C:\Projects\survey\survey_clean.sav'.

Other common data commands: GET DATA /TYPE=TXT and /TYPE=XLSX for text and Excel import, GET STATA, DATASET ACTIVATE name, DATASET CLOSE name. Use a new file name when you save changes (survey_clean.sav), so the original stays intact.

Pasting from dialogs shows exact keywords. For example File > Open > Data pastes a GET FILE line, so you rarely need to memorize paths.

Variable definitions in syntax

Text
VARIABLE LABELS
  q1 'Satisfaction with service'
  q2 'Likelihood to recommend'.
VALUE LABELS gender 1 'Female' 2 'Male' 3 'Other'.
MISSING VALUES income (-9, -8).
VARIABLE LEVEL q1 q2 (ORDINAL) age (SCALE).

Doing this in syntax rather than in Variable View means the dictionary is documented and repeatable.

A first complete script

Text
* Analysis of the satisfaction survey. Created 2025-03-02.
GET FILE='C:\Projects\survey\survey_clean.sav'.

FREQUENCIES VARIABLES=gender region
  /ORDER=ANALYSIS.

DESCRIPTIVES VARIABLES=age income
  /STATISTICS=MEAN STDDEV MIN MAX.

T-TEST GROUPS=gender(1 2)
  /VARIABLES=satisfaction
  /CRITERIA=CI(.95).

Save it as analysis.sps. When the data change or a reviewer asks for a rerun, you press Run > All.

The Log and errors

The Output Viewer begins each run with the Log, which lists the commands executed. Warnings appear as Warnings or errors (>Error # 4285). SPSS stops processing the failing command but may continue with later ones, so always scan the output after a long run. Typical errors: missing period, misspelled variable, quote marks that do not match, a variable used before it was created.

Python Stand-in: a mini syntax checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real SPSS parser is far more thorough, but the most common mistake is exactly the one this stand-in catches.

Working habits

  • Paste from dialogs, then edit; never type everything from memory.
  • Keep one .sps for data preparation and one for analysis.
  • Start each file with a comment header (project, author, date, purpose).
  • Use relative or documented paths and a CD command or one FILE HANDLE at the top so paths change in one place:
Text
FILE HANDLE project /NAME='C:\Projects\survey'.
GET FILE='project\survey_clean.sav'.
  • Run the whole script from top to bottom before trusting it.
Python Stand-in: building a FREQUENCIES command from choices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Missing the final period.
  • Editing the data through menus without pasting, so the script no longer reproduces the data file.
  • Overwriting the raw data file.
  • Running only a selection and forgetting an earlier line that creates a variable.
  • Quoting with curly quotes pasted from Word; SPSS needs straight quotes.

Next you will meet the data-changing commands in data steps in syntax.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What ends every SPSS command?

    A period terminates the command; blank lines and indentation are ignored.
  2. 2. Which key combination runs the selected syntax?

    Ctrl+R (Run Selection) runs the highlighted text or the command at the cursor.
  3. 3. How does a subcommand start?

    Subcommands such as /STATISTICS= begin with a slash.
  4. 4. Why use Paste instead of OK in a dialog?

    Paste records your menu choices as syntax without running them.

Summary

  • Syntax is a plain-text script of commands ending in periods, with /subcommands.
  • Use the Syntax Editor, Run > Selection/All, comments and the Log.
  • Define variables, load and save data in syntax so work is repeatable.
  • Use Paste to learn syntax, save .sps files and never overwrite raw data.

Next: Data Steps in Syntax