Quick Overview SPSS Python Modules

Basic Python can't do anything in SPSS. However, we can add extra functionality to it that makes things work. Such functionality resides in modules that we import. This basic idea is similar to installing apps on a smartphone.

Overview Main SPSS Python Modules

NameMeaningMain Uses
spssSPSS FunctionsHave Python run the SPSS syntax it creates.
Look up variable properties, variable count, case count, system settings and more.
spssauxSPSS Auxiliary FunctionsGet and set variable properties including value labels.
Retrieve variable names by expanding SPSS TO and ALL keywords.
spssdataSPSS Data ValuesLook up all data values in one or many variables.
Append new variables/cases to dataset.
osOperating SystemLook up contents of folders.
Create, edit or delete files or folders.
SpssClientSPSS ClientAccess Output, Data Editor and Syntax windows.
Batch modify SPSS Output tables or Charts.

SPSS Python Modules - What & Why?

Python has a modular structure: there's the basic program with quite some functions built into it. And then there's a ton of modules, add-on files that extend Python's capabilities.

Like so, there's a couple of modules for having Python interact with SPSS. All modules discussed in this lesson are included in the SPSS Python Essentials so you don't need to download them.

For recent SPSS versions, they're found in a folder like C:\Program Files\IBM\SPSS Statistics\Python3\Lib\site-packages as shown below.

SPSS Python Modules In Folder 28

Note that the site-packages folder is also where you can create your own SPSS Python module(s) as we'll do in SPSS - Cloning Variables with Python.

The remainder of this tutorial walks you through the main SPSS Python modules and their functions. We also created a handy overview of these in this Googlesheet (read-only).

SPSS Python Functions Googlesheet

Python spss Module

We'll use the spss module mostly for looking up SPSS dictionary information and having Python run SPSS syntax. The SPSS module is extensively documented in the SPSS Python Reference Guide (download here).

spssGetSettingspss.GetSetting('tnumbers')Get system setting from SET command. Similar to spssaux.GetSHOW.
spssGetSplitVariableNamesspss.GetSplitVariableNames()Return Python list of split variable names.
spssGetVariableCountspss.GetVariableCount()Get number of variables in active dataset.
spssGetVariableFormatspss.GetVariableFormat(0)Get variable format (such as F4.2, A20, date11) by Python index.
spssGetVariableLabelspss.GetVariableLabel(0)Get variable label by Python index.
spssGetVariableMeasurementLevelspss.GetVariableMeasurementLevel(0)Get measurement level by Python index.
spssGetVariableNamespss.GetVariableName(0)Get variable name by Python index.
spssGetVariableTypespss.GetVariableType(0)Get variable type (string or numeric) by Python index.
spssSubmitspss.Submit("FREQUENCIES ALL.")Run SPSS syntax held in Python string.
spssGetWeightVarspss.GetWeightVar()Get name of active WEIGHT variable -if any.

Python spssaux Module

spssaux is short for SPSS auxiliary functions and has quite some overlap with the spss module. Two important things that we'll do with spssaux are

The table below lists these and some other main uses of spssaux.

spssauxcreateDatasetOutputspssaux.CreateDatasetOutput(...)Create dataset holding SPSS output, similarly to SPSS OMS.
spssauxGetVariableNamesListspssaux.GetVariableNamesList()Return Python list of all variable names in active dataset.
Of limited use because most variable properties are retrieved
by index instead of name.
spssauxVariableDictspssaux.VariableDict(caseless = True)Create Python dict containing all variable names and indices.
Expands SPSS TO and ALL keywords and retrieves variable properties.
spssauxGetSHOWspssaux.GetSHOW('weight')Retrieves system setting displayed by SHOW.
Has some more options than spss.GetSetting.
spssauxGetValueLabelsspssaux.GetValueLabels(0)Returns Python dict object holding value labels by index.
spssauxGetVariableValuesspssaux.GetVariableValues(6)Returns Python list holding distinct data values by index.
Does not work with SPLIT FILE on.

Python spssdata Module

The spssdata module can look up data values in SPSS for one or several variables. We'll do so with spssdata.Spssdata as shown below (using bank.sav).


begin program python3.
import spssdata
with spssdata.Spssdata(['dob','educ','marit']) as allData:
    for case in allData:
end program.

Part of the resulting output is shown below.

Python SPSSdata SPSSdata Output Example

By default, SPSS date variables are not converted to Python datetime objects. For more on this, see SPSS - Extract ISO Weeks from Date Variable.

Missing values in SPSS result in Python NoneType objects.

Python os Module

The Python os (short for operating system - probably MS Windows for most readers) module allows us to look up the contents of folders and move files to different folder and/or rename them. The table below lists some functions.

oslistdiros.listdir(r'd:\data')Return Python list of all files and folders in some directory.
ospath.existsos.path.exists(r'd:\data\myfile.sav')Check if path exists.
ospath.joinos.path.join(dir,'myfile.sav')Create path from components such as (sub)folders and file name.
osremoveos.remove(r'd:\data\myfile.sav')Delete file.
osrenameos.rename(r'd:\temp',r'd:\tmp')Change file/folder name or location.
oswalkos.walk(r'd:\data')Search folder and all subfolders.

Note that we often use raw strings for escaping the backslashes found in Windows paths. We'll use os.listdir in some future tutorials.

Python SpssClient Module

The SpssClient module works quite differently than the other SPSS Python modules. We mostly use it for (batch) processing SPSS output items such as tables and charts.

Most of what we used to do with the SpssClient module has been added to OUTPUT MODIFY for recent SPSS versions. This works way easier, faster and more reliably.

On top of that, the SpssClient module is rather challenging. We therefore decided not to cover it any further during this course.

Thanks for reading!

Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.