SPSS Python

SPSS can call an external Python process for complex programming.


Setup

Running Python from SPSS

Recent versions of SPSS come with a vendored distribution of Python, simplifying the setup.

Set the location of Python distrutions in Edit > Options > File locations. Depending on the version of SPSS, options for Python 2 and/or Python 3 may be available. Recent versions only allow one configuration, meant for Python 3.

Running SPSS from Python

import sys
sys.path.insert(0,r'<SPSS_HOME>\Python3\Lib\site-packages')


Usage

The spss module offers a basic interface to SPSS.

begin program python3.
import spss

varlist = []
for i in range(spss.GetVariableCount()):
    if spss.GetVariableMeasurementLevel(i) == 'scale':
        varlist.append(spss.GetVariableName(i))

spss.Submit(["DESCRIPTIVES VARIABLES=", " ".join(varlist), "."])
end program.

The spssdata module is a simple interface to data.

begin program python3.
import spssdata

with spssdata.Spssdata(['foo','bar','baz']) as d:
    for r in d:
        print(r)
end program.

The spssaux module offers some higher-level interfaces.

begin program python3.
import spss, spssaux

for v in spssaux.GetVariableNamesList():
    spss.Submit(["RENAME VARIABLES (", v, "=", v.lower(), ")."])
end program.

To call SPSS from a Python process, try:

import spss, SpssClient

SpssClient.StartClient()

spss.Submit("SHOW UNICODE.")

SpssClient.Exit()
SpssClient.StopClient()

When calling SPSS from a Python process, it can be helpful to establish a data step so that manipulations to data objects persist.

import spss, SpssClient

SpssClient.StartClient()
spss.StartDataStep()

dataset = spss.Dataset()
dataset.varlist.append('foo',0)

var = dataset.varlist['foo']
var.format = (5,2,0)

col = dataset.varlist['foo'].index
n = len(dataset.cases)
for row in range(n):
    dataset.cases[row, col] = 1.23

spss.EndDataStep()
SpssClient.Exit()
SpssClient.StopClient()


CategoryRicottone

SPSS/Python (last edited 2023-06-15 16:07:49 by DominicRicottone)