Python for SPSS – How to Use It?
- SPSS Python Essentials
- Run Python from SPSS Syntax Window
- Wrap Python Code into Functions
- Write Your Own Python Module
- Create an SPSS Extension
SPSS Python Essentials
First off, using Python in SPSS always requires that you have
- SPSS,
- Python and
- the SPSS-Python plugin files installed on your computer.
These components are collectively known as the SPSS Python essentials. For recent SPSS versions, the Python essentials are installed by default. One way to check this is navigating to
in which you'll probably find some Python location(s) as shown below.
So what should you see here? Well,
- if you see an active Python 3 location here, you're good to go;
- if you only see an active Python 2 location, then you can only use Python 2, which is no longer supported. Your best option is to upgrade to SPSS version 24 or (preferably) higher;
- if all locations are greyed out (or even absent), you have SPSS without any Python essentials installed. In this case, you'll need to (re)install a recent SPSS version.
Run Python from SPSS Syntax Window
Right. So if you've SPSS with the Python essentials properly installed, what's next?
Well, the simplest way to go is to run Python from an SPSS syntax window. Enclose all lines of Python between BEGIN PROGRAM PYTHON3. and END PROGRAM. as shown below.

Try and copy-paste-run the entire syntax below. Note that this Python block simply lowercases all variable names, regardless what or how many they are.
data list free/V1 V2 v3 v4 EDUC gender SAlaRY.
begin data
end data.
*Run Python block for lowercasing all variable names.
begin program python3.
import spss,spssaux
oldNames = spssaux.GetVariableNamesList()
newNames = [var.lower() for var in oldNames]
spss.Submit("RENAME VARIABLES (%s = %s)."%(' '.join(oldNames),' '.join(newNames)))
end program.
Wrap Python Code into Functions
Right, so we just ran some Python from an SPSS syntax window. Now, this works fine but doing so has some drawbacks:
- our syntax becomes less readable and manageable if it contains long Python blocks;
- if we use some Python block in several SPSS syntax files and we'd like to correct it, we'll need to correct it in each syntax file;
- the SPSS syntax editor is a poor text editor.
A first step towards resolving these issues is to first wrap our Python code into a Python function.
data list free/V1 V2 v3 v4 EDUC gender SAlaRY.
begin data
end data.
*Define lowerCaseVars as Python function.
begin program python3.
def lowerCaseVars():
import spss,spssaux
oldNames = spssaux.GetVariableNamesList()
newNames = [var.lower() for var in oldNames]
spss.Submit("RENAME VARIABLES (%s = %s)."%(' '.join(oldNames),' '.join(newNames)))
end program.
*Run function.
begin program python3.
lowerCaseVars()
end program.
Note that we first define a Python function and then run it. Like so, you can develop a single SPSS syntax file containing several such functions.
Running this file just once (preferably with INSERT) defines all of your Python functions. You can now use these for all projects you'll work on during your SPSS session.
Write Your Own Python Module
We just defined and then ran a function. The next step is moving our function into a Python file: a plain text file with the .py extension that we'll place in C:\Program Files\IBM\SPSS Statistics\Python3\Lib\site-packages or wherever our site-packages folder is located.

We can now edit this file with Notepad++, which is much nicer than SPSS’ syntax editor. Since a Python file contains only Python, we'll leave out BEGIN PROGRAM PYTHON3. and END PROGRAM.

If we now import our module in SPSS, we can readily run any function it contains as shown below.
data list free/V1 v2 V3 V4 v5 V6.
begin data
end data.
*Import module and lowercase variable names.
begin program python3.
import ruben
ruben.lowerCaseVars()
end program.
Developing and using our own Python module has great advantages:
- each function is defined only once and it doesn't clutter up our syntax window;
- if we need to correct some function, we need to correct it only in one module that can be used by several SPSS syntax files;
- we can use functions within functions in our module. Doing so can make our code shorter and easier to manage.
A quick tip: if you're developing your module, reload it after each edit.
begin program python3.
import ruben,importlib # import ruben and importlib modules
importlib.reload(ruben) # use importlib to reload ruben module
ruben.lowerCaseVars() # run function from ruben module
end program.
Create an SPSS Extension
SPSS extensions are tools that can be developed by all SPSS users for a wide variety of tasks. For an outstanding collection of SPSS extensions, visit SPSS Tools - Overview.
Extensions are easy to install and can typically be run from SPSS menu dialogs as shown below.

So how does this work and what does it have to do with Python?
Well, most extensions define new SPSS syntax commands. These are not much different from built-in commands such as FREQUENCIES or DESCRIPTIVES. The syntax below shows an example from SPSS - Create All Scatterplots Tool.
SPSS TUTORIALS SCATTERS YVARS=costs XVARS=alco cigs exer age
/OPTIONS ANALYSIS=FITALLTABLES ACTION=RUN.
Now, running this SPSS syntax command basically passes its arguments -such as input/output variables, values or titles- on to an underlying Python function and runs it. This Python function, in turn, creates and runs SPSS syntax that gets the final job done.
Note that SPSS users don't see any Python when running this syntax -unless they can make the Python code crash. For actually seeing the Python code, you may unzip the SPSS extension (.spe) file and look for some Python (.py) file in the resulting folder.

Some final notes on SPSS extensions is that developing them is seriously challenging and takes a lot of practice. However, well-written extensions can save you tons of time and effort over the years to come.
Thanks for reading!
Python for SPSS – What is It?
Serious SPSS users have probably heard that
using Python in SPSS can dramatically speed up
your daily SPSS tasks.
So what is it and how does it work? This brief tutorial quickly walks you through.
- How does Python relate to SPSS?
- What Is Python Known For?
- Why Should I Use Python in SPSS?
- Where Can I Get Python for SPSS?
- Which are Some SPSS Python Examples?
Introduction
Python is one of the main general programming languages today and was first launched in 1989.

SPSS -short for “statistical package for the social sciences”- is user friendly software for data editing/analysis and statistical procedures. SPSS is much older than Python: it's already been in use since 1968. Originally, Python had nothing whatsoever to do with SPSS. They were simply 2 completely unrelated software packages until roughly 2005.
So How does Python relate to SPSS?
Around 2005, the SPSS developers created software that connects SPSS with Python: the SPSS-Python plugin. This plugin made it possible to
- send Python code to Python from SPSS;
- have Python look up anything in SPSS: variable names or labels, data values, output tables and more;
- use such information to create (large amounts of) custom-made SPSS syntax;
- have Python send such syntax back to SPSS and execute it;
- or, alternatively, have Python directly modify SPSS output tables & charts, syntax or data.
The figure below sketches some of such interactions between SPSS and Python.

What Is Python Known For?
- Python is among the most important programming languages in use today. It is open source software and it may be freely downloaded, used and distributed, even for commercial use.
- Python was deliberately designed to be intuitive and easy to learn, a programming language suitable for non programmers -which probably includes most SPSS users.
- Python easily handles a huge variety of tasks: it can read, write or modify text files, Excel files, MySQL databases, SPSS and much more.
- Python does a couple of things very differently than the bulk of programming languages. Especially its use of indentation for control structures (looping and conditional processing) is original but effective;
- Just like www.spss-tutorials.com, Python was invented and created in Amsterdam, the Netherlands.
Why Should I Use Python in SPSS?
- For some larger SPSS tasks, using Python for SPSS may decimate the amount of time and effort they require.
- Also, Python may drastically decrease the amount of SPSS syntax required by some tasks. Shorter syntax is much easier to read, adjust and correct.
- Some SPSS tasks are not possible at all with basic syntax but are easily accomplished by Python.
- There are no costs associated with using Python in SPSS.
- There's an ever growing number of SPSS extensions freely available that can be used from SPSS’ menu. Most of these require Python to actually run.

Where Can I Get Python for SPSS?
Finally an easy question... Recent SPSS versions are integrated with Python by default. It is located in the Python3 folder in your SPSS installation folder as shown below.

For more details on this, read up on Python for SPSS - How to Use It?
Which are Some SPSS Python Examples?
Some excellent SPSS-Python code is found in many of our SPSS tools:
- SPSS Mean Centering and Interaction Tool is super useful for moderation regression;
- SPSS - Recode with Value Labels Tool recodes values and moves their value labels from the old onto the new values;
- SPSS - Create All Scatterplots Tool runs scatterplots with(out) regression lines and tables for many pairs of variables in one go;
- SPSS - Clean Labels Tool performs text replacements over many value and/or variable labels;
- SPSS Create Dummy Variables Tool creates dummy variables for regression.

Note: when using these tools, you don't immediately see the underlying Python code. However, if you unzip the SPSS extension (.spe) files, you'll find that each of them contains a Python (.py) file that contains the SPSS-Python code being used.
Thanks for reading!
Main Differences Python and SPSS Syntax
- Python is Fully Case Sensitive
- Check Object Types in Python
- Indentation Matters in Python
- Python Assignment or Comparison Operator
- Escape Sequences in SPSS and Python
- Comments in SPSS and Python
Python is Fully Case Sensitive
Experienced SPSS users probably know that SPSS syntax is mostly case insensitive: it doesn't usually matter whether you write syntax in lower case, upper case or a mixture of these. Like so, the FREQUENCIES commands below are all equivalent.
FREQUENCIES V01 TO V10.
frequencies v01 to v10.
fReQuEnCiEs v01 tO V10.
In contrast, Python is fully case sensitive. As a consequence, you always need to use the exact right casing in Python or you may otherwise trigger a wide variety of errors and warnings.
begin program python3.
myName = 'Ruben'
print(myName) # Ruben
print(myname) # [...] NameError: name 'myname' is not defined
end program.
Note that you also need use the correct casing for SPSS variable names in Python. Avoiding all upper case for SPSS variable names tends to simplify things.
Check Object Types in Python
In SPSS, “data types” mostly refer to variable types. Keep in mind that “variables” in SPSS are columns of cells that hold data values. There's only 2 SPSS variable types:
- numeric variables and
- SPSS string variables.
These 2 types determine what you can (not) do with a variable in SPSS. Confusingly, the Type column in variable view suggests that there's many more variable types but these are formats, not types as explained in SPSS Variable Types and Formats.

In Python, we'll also encounter strings and numbers but these are called objects instead of variables and only hold a single value. An entire column of values is usually represented as a Python list object or a Python tuple. And these are different object types than the strings or integers they may contain.
Like so, there's many different Python object types which we briefly cover in Quick Overview Python Object Types. Just as with SPSS variable types, Python object types determine what you can (not) do with them and how. That's why you sometimes need to check what type of object you're dealing with. This is simply done with print(type(myObject)) as shown below.
begin program python3.
myName = 'Ruben'
print(type(myName)) # <class 'str'>
end program.
Note: Python users can create new object types known as Classes. Such classes typically have their own methods and properties. You may need a bit of study for dealing with some of them.
Indentation Matters in Python
Basic looping in SPSS is done with
- a DO REPEAT or LOOP command indicating the start of some loop;
- 1(+) SPSS transformation commands that we'll loop over and
- an END REPEAT or END LOOP command in order to end some loop.
The syntax below briefly illustrates this structure.
do repeat #vars = v01 to v05.
compute #vars = 0.
end repeat.
In Python, the start of a loop is indicated by a for
or while
statement. After that, the indentation of the lines that follow indicate where a loop ends. The examples below illustrate how this works.
begin program python3.
for ind in range(10):
print('COMPUTE V{} = {}.'.format(ind,ind))
print("VARIABLE LABELS V{} 'Mean Job Satisfaction Score'.".format(ind))
end program.
*LOOP OVER 1 PRINT STATEMENT.
begin program python3.
for ind in range(10):
print('COMPUTE V{} = {}.'.format(ind,ind))
print("VARIABLE LABELS V{} 'Mean Job Satisfaction Score'.".format(ind))
end program.
This means that in order for your Python code to function properly, you must apply the correct indentation levels. This goes for
- Python
for
loops; - Python
while
loops; - Python
if
structures, possibly includingelif
and/orelse
; - Python try-except structures.
Note that we cover most of these in Conditions and Loops in Python.
Python Assignment or Comparison Operator
In SPSS syntax, the = operator is used for
- assigning a value or function to some variable as in COMPUTE or IF or
- comparing a variable/value to another variable/value.
The syntax below shows 2 minimal examples.
compute m01 = mean(v01 to v05).
*USE = FOR COMPARISON IN SPSS.
do if (gender = 0).
...
end if.
In contrast, Python uses
- = for assigning a value to some object but
- == for comparing and object/value to another object/value.
The examples below briefly illustrate the difference.
begin program python3.
myAction = 'PRINT' # = assigns value to object
if myAction == 'PRINT': # == compares values
print('FREQUENCIES ALL.')
end program.
For an overview of these and many other Python operators, read up on Python Operators - Quick Overview & Examples.
Escape Sequences in SPSS and Python
In SPSS, we rarely use escape sequences but 2 main exceptions are
- escaping a single quote in a value/variable label by doubling it and
- using \n in variable and value labels to have them break over 2 or more lines.
Few SPSS users are aware of such escape sequences but running the syntax below nicely illustrates both.
data list free/v01.
begin data.
6
end data.
*ESCAPE SINGLE QUOTE BY DOUBLING IT.
add value labels v01 6 'Don''t know'.
*ESCAPE "n" BY BACKSLASH.
variable labels v01 'This label continues on\nline 2 and\nline 3'.
*SHOW LABELS IN SUCCEEDING OUTPUT TABLES.
set tnumbers both tvars both.
*QUICK CHECK.
frequencies v01.
Result

So those are basic escape sequences in SPSS syntax. Now, what about Python?
In Python, escaping is always done with a backslash. A backslash itself is also escaped by a backslash. Alternatively, specify a string as a raw string by preceding it with “r”. The syntax below shows some basic examples.
begin program python3.
vallab = 'Don\'t know'
print(vallab)
end program.
*ESCAPE BACKSLASH WITH BACKSLASH.
begin program python3.
rDir = 'D:\\analyses_example\\new-data'
print(rDir)
end program.
*ESCAPE BACKSLASH BY RAW STRING.
begin program python3.
rDir = r'D:\analyses_example\new-data'
print(rDir)
end program.
Comments in SPSS and Python
In SPSS we usually comment entire lines of syntax by
- starting some line with an asterisk and
- ending it with a period.
In rare cases, we may enter a comment within some command enclosed by /* and */ as shown below.
frequencies all /* AND HERE'S A COMMENT WITHIN A COMMAND */
/barchart.
In Python, everything between a hashtag and the end of the line in which it occurs is a comment. Alternatively, enclose a (multiline) comment by triple quotes.
begin program python3.
#FIRST GREET AUDIENCE BEFORE PROCEEDING
print('Hello!') #DONE
end program.
*PYTHON COMMENT BETWEEN TRIPLE SINGLE QUOTES.
begin program python3.
'''
This is a multiline
comment enclosed by
triple single quotes.
'''
print('Hello!')
end program.
Thanks for reading!