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
- some Python code is included in SPSS syntax;
- SPSS passes this Python code on to Python;
- Python may then look up information in SPSS such as variable names in the dataset or tables in the output window;
- finally, Python can use this information to modify our data, output or syntax either directly or by creating and running SPSS syntax.
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.
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 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
1 2 3 4 5 6 7 8 9 10 11 12 13
| *Look up all table titles with SPSS Python Scripting.|
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
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
1 2 3 4 5 6 7 8
| *Lowercase all variable names.|
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
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!