Basic Specification for a Python Program Block
The basic specification for a Python program block is BEGIN PROGRAM PYTHON3
(the
keyword PYTHON3
can be omitted) followed by one or more Python statements, followed
by END PROGRAM.
Note: The Python function sys.exit()
is
not supported for use within a program block.
- The first program block in a session should start with the Python
function
import spss
, which imports thespss
module, providing access to the functions in the Python Integration Package for IBM® SPSS® Statistics. See the topic Python Functions and Classes for more information. - Subsequent program blocks in the same session do not require
import spss
, and it is silently ignored if the module has already been imported.
Example
DATA LIST FREE /var1.
BEGIN DATA
1
END DATA.
DATASET NAME File1.
BEGIN PROGRAM PYTHON3.
import spss
File1N=spss.GetVariableCount()
END PROGRAM.
DATA LIST FREE /var1 var2 var3.
BEGIN DATA
1 2 3
END DATA.
DATASET NAME File2.
BEGIN PROGRAM PYTHON.
File2N=spss.GetVariableCount()
if File2N > File1N:
message="File2 has more variables than File1."
elif File1N > File2N:
message="File1 has more variables than File2."
else:
message="Both files have the same number of variables."
print message
END PROGRAM.
- The first program block contains the
import spss
statement. This statement is not required in the second program block. - The first program block defines a programmatic variable, File1N, with a value set to the number of variables in the active dataset.
- Prior to the second program block, a different dataset becomes the active dataset, and the second program block defines a programmatic variable, File2N, with a value set to the number of variables in that dataset.
- Since the value of File1N persists from the first program block, the two variable counts can be compared in the second program block.
Syntax Rules
- Within a program block, only statements recognized by the specified programming language are allowed.
- Command syntax generated within a program block must follow interactive syntax rules. See the topic Running Commands for more information.
- Within a program block, each line should not exceed 251 bytes (although syntax generated by those lines can be longer).
- With the IBM SPSS Statistics Batch Facility (available only with IBM SPSS Statistics Server), use the -i switch when submitting command files that contain program blocks. All command syntax (not just the program blocks) in the file must adhere to interactive syntax rules.
Within a program block, the programming language is in control, and the syntax rules for that programming language apply. Command syntax generated from within program blocks must always follow interactive syntax rules. For most practical purposes this means command strings you build in a programming block must contain a period (.) at the end of each command.
Scope and Limitations
- Programmatic variables created in a program block cannot be used outside of program blocks.
- Program blocks cannot be contained within
DEFINE-!ENDDEFINE
macro definitions. - Program blocks can be contained in command syntax files run via the
INSERT
command, with the defaultSYNTAX=INTERACTIVE
setting. - Program blocks cannot be contained within command syntax files run
via the
INCLUDE
command.
- Python variables specified in a given program block persist to subsequent program blocks.
- Python programs (.py, .pyc) utilizing the
spss
module cannot be run as autoscripts, nor are they intended to be run from Utilities>Run Script.
More information about Python programs and Python scripts is available from the topic Scripting with the Python Programming Language.