David Glickman February 2016

Exit with error code from SPSS syntax

I am using a batch file which calls an SPSS production job which runs many syntax files.

In the syntax files I want to able to check some variables, and if certain conditions are not met then I want to stop the production job, exit SPSS and return an error code to the batch file.

The batch file needs to stop running the next commands based on the error code returned. I know how to do this in the batch file already.

The most basic solution could be if the error code is not 0 then stop, and the error text will be output to a separate text file from within the syntax. A bonus would be a different error code which I could then match to where in the syntax that code is thrown.

What is the best way to achieve this in the SPSS syntax and or production file?

Answers


JKP February 2016

One way to do this would be to execute Statistics as an external mode Python job. Then you could interrogate any results, catch exceptions, and set exit codes and messages however, you like. Here is an example:

jobs.py:

Python jobs

import sys
sys.path.append(r"""c:/spss23/python/lib/site-packages""")
import spss

try:
spss.Submit("""INSERT FILE="c:/temp/syntax1.sps".""") except:
print "syntax1.spss failed" exit(code=1)

try:
spss.Submit("""INSERT FILE="c:/temp/syntax2.sps".""")
except:
print "syntax1.spss failed"
exit(code=2)

Then the bat file would do
python c:/myjobs/jobs.py
print %ERRORLEVEL%

or similar. The job would need to save the output in appropriate format using OMS or shell redirection. (The blocks after try and except should be indented.)


JKP February 2016

In external mode, you could use code like this or you could interrogate items in the Viewer.

import spss, spssdata

curs = spssdata.Spssdata("variable2")  
for case in curs:  
    if case[0] == 6:  
        exit(99)  
curs.CClose  

Post Status

Asked in February 2016
Viewed 2,766 times
Voted 13
Answered 2 times

Search




Leave an answer