Introduction to Python Programs
The Python® Integration Package for IBM® SPSS® Statistics allows you to create Python programs that control the flow of command syntax jobs, read and write data, and create custom procedures that generate their own pivot table output. This feature requires the IBM SPSS Statistics - Integration Plug-in for Python, which is installed by default with your IBM SPSS Statistics product.
A companion interface is available for creating Python scripts that operate on the IBM SPSS Statistics user interface and manipulate output objects. See the topic Introduction to Python Scripts for more information.
Python programming features described here are available inside BEGIN
PROGRAM-END PROGRAM
program blocks in command syntax. A program
block provides access to all the functionality of the Python programming
language, including the functions specific to IBM SPSS Statistics and
provided in the Python Integration Package for IBM SPSS Statistics.
You can use program blocks to combine the programmability features
of Python with all the capabilities of IBM SPSS Statistics by
building strings of command syntax that are then executed by IBM SPSS Statistics.
You can also run IBM SPSS Statistics from an external Python process, such as a Python IDE or the Python interpreter. See the topic Running IBM SPSS Statistics from an External Python Process for more information.
Within a program block, Python is in control, and it knows nothing about IBM SPSS Statistics commands. When the Python Integration Package for IBM SPSS Statistics is loaded, Python knows about the functions provided in the package, but standard IBM SPSS Statistics commands are basically invalid within a program block. For example:
BEGIN PROGRAM PYTHON3.
FREQUENCIES VARIABLES=var1, var2, var3.
END PROGRAM.
will generate an error, because FREQUENCIES
is
not recognized by Python. But since the goal of a program block is
typically to generate some command syntax that IBM SPSS Statistics can
understand, there must be a way to specify command syntax within a
program block. This is done by expressing syntax commands, or parts
of commands, as character strings, as in:
spss.Submit("FREQUENCIES VARIABLES=var1, var2, var3.")
The real power of program blocks comes from the ability to dynamically build strings of command syntax, as in:
BEGIN PROGRAM PYTHON3.
import spss
string1="DESCRIPTIVES VARIABLES="
N=spss.GetVariableCount()
scaleVarList=[]
for i in range(N):
if spss.GetVariableMeasurementLevel(i)=='scale':
scaleVarList.append(spss.GetVariableName(i))
string2="."
spss.Submit([string1, ' '.join(scaleVarList), string2])
END PROGRAM.
-
spss.GetVariableCount
returns the number of variables in the active dataset. -
if spss.GetVariableMeasurementLevel(i)=="scale"
is true only for variables with a scale measurement level. -
scaleVarList.append(spss.GetVariableName(i))
builds a list of variable names that includes only those variables with a scale measurement level. -
spss.Submit
submits aDESCRIPTIVES
command to IBM SPSS Statistics that looks something like this:
DESCRIPTIVES VARIABLES=
scalevar1 scalevar2 scalevar3...etc.
.