Search code examples
pythonspss

SPSS-Python Script stops with an Error when spss commands inside spss.Submit() would create a warning


Let's assume I have two lists of variables

list a: a1 a2 a3
list b: b1 b2 b3

which I want to process in a way like this:

TEMPORARY.
SELECT IF a1=b1.
FREQUENCY someVar.


TEMPORARY.
SELECT IF a2=b2.
FREQUENCY someVar.


TEMPORARY.
SELECT IF a2=b2.
FREQUENCY someVar.

I tried tried to do this within a python loop:

BEGIN PROGRAM.
import spss

la = ['a1', 'a2', 'a3']
lb = ['b1', 'b2', 'b3']

for a, b in zip(la, lb):
    spss.Submit('''
TEMPORARY.
SELECT IF %s=%s.
FREQUENCY someVar.
    ''' % (a, b))
END PROGRAM.

So far so good. This works except when the SELECT IF command would create an empty dataset. Outside the Python program block this would cause the following warning message in the output viewer:

No cases were input to this procedure. Either there are none in the working data file or all of them have been filtered out. Execution of this command stops.

But inside a Python block it causes an Error and the python script to stop.

Traceback (most recent call last):
File "", line 7, in
File "C:\PROGRA~1\ibm\SPSS\STATIS~1\23\Python\Lib\site-packages\spss\spss.py", line 1527, in Submit raise SpssError,error spss.errMsg.SpssError: [errLevel 3] Serious error.

Is there a way to run this loop (which might produce temporary empty data sets and therefore warnings) inside of python?


Solution

  • Yes, if you wrap the problematic function inside a try-except construct:

    for a, b in zip(la, lb):
        try:
            spss.Submit('''
    TEMPORARY.
    SELECT IF %s=%s.
    FREQUENCY someVar.
            ''' % (a, b))
        except:
            pass