SPSS tutorials website header logo

SPSS tutorials


SPSS with Python III – How Does It Work?

So how does Python for SPSS actually work? First off, a distinction is often made between SPSS Python programmability and SPSS Python scripting. They work similarly in that

Python Program Blocks

Right. So SPSS syntax may hold some Python code and it needs to be enclosed by the BEGIN PROGRAM. and END PROGRAM. commands. Whatever lines are between those are passed on to Python. The entire specification is known as a Python program block.

SPSS Python Program block in SPSS syntax Window

However, the way this code operates differs between Python programmability and scripting. So let's take a closer look.

SPSS Python Scripting

The three main windows we use in SPSS are

Each window, in turn, may hold several other objects. For instance, the Data Editor holds variables and cases which -in turn- hold data values. Like so, the entire program is organized according to a tree structure known as the object hierarchy.

SPSS Python Scripting - SPSS Object Hierarchy

SPSS Python scripting uses the SpssClient module to access and navigate through this object hierarchy. While doing so, it can look up and modify information on different objects.
Although Python scripting is highly challenging compared to Python programmability, we'll include a short example anyway. Perhaps ignore the actual code and just look at the Python-comments because these illustrate how we walk through the contents of an output window.

SPSS Python Scripting - Look Up All Table Titles

*Look up all table titles with SPSS Python Scripting.

begin program.
import SpssClient
oDoc = SpssClient.GetDesignatedOutputDoc() #Access active output window
oItems = oDoc.GetOutputItems() #Look up all items in output window
for index in range(oItems.Size()): #Loop through indices of output items
    oItem = oItems.GetItemAt(index) #Access output item
    if oItem.GetType() == SpssClient.OutputItemType.PIVOT: #Continue only if output item is pivot table
        pTable = oItem.GetSpecificType() #Access pivot table
        print pTable.GetTitleText() #Print table title to output window
end program.

Note: this syntax will only print some table titles if you actually have some tables in the active output window.

SPSS Python Programmability

Python programmability -way easier than Python scripting- does not use the SpssClient module or the SPSS object hierarchy. Instead, it retrieves data properties directly and often -not always- modifies them by creating and running SPSS syntax. A simple example is shown below.

Python Programmability - Lowercase All Variable Names

*Lowercase all variable names.

begin program.
import spss
for ind in range(spss.GetVariableCount()): #Loop through variable indices
    varNam = spss.GetVariableName(ind) #Look up each variable name
    spss.Submit('RENAME VARIABLES %s = %s.'%(varNam,varNam.lower())) #Rename variable by lowercase name
end program.

In this example, SPSS passes syntax to Python. Python looks up each variable name and then creates and runs an SPSS RENAME VARIABLES command. So here we have SPSS syntax holding Python code that -in turn- holds SPSS syntax.

Programmability or Scripting?

Whether to use Python programmability or scripting depends on what you want to accomplish. If it can be done by SPSS syntax -mostly editing data or running procedures- then creating and running this syntax with Python programmability is usually the easiest way to go. It doesn't really matter if the required syntax is complex or huge: Python will handle it for us. Like so, our SPSS Clone Variables Tool uses Python programmability.
However, SPSS syntax can't modify tables and charts in your output window. Well, ok, except for OUTPUT MODIFY introduced in SPSS version 22. Since programmability can't edit output items either, Python scripting is the way to go here. Like so, our SPSS - Set Chart Sizes Tool uses Python scripting.

I guess that's about it. Although Python for SPSS tends to be rather technical, I hope this tutorial gave you at least some idea of how it works. For a more thorough understanding of Python for SPSS: just do it.

Thanks for reading!

Previous tutorial: SPSS with Python II – Installing and Testing

Next tutorial: SPSS with Python IV – How to Use It?

Let me know what you think!

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

This tutorial has 6 comments