首页 > 解决方案 > 使用 python 调用 SAS 脚本 - 子进程

问题描述

我正在尝试使用 python 和子进程调用 sas 脚本。这是我的代码:

    proc = Popen(self.cmd, shell=True, stdout=PIPE, stderr=STDOUT)
    proc.wait()
    standard_output, standard_error = proc.communicate()
    if proc.returncode == 0:
        log.info("Successfully executed execute_shell_command")
    elif proc.returncode == 1:
        self.status_message = "return code 1 from" + " ".join(self.cmd) + "error msg: " + str(standard_error)
        self.flag = 1
        raise ValueError(self.status_message)
    elif proc.returncode > 1:
        self.status_message = "Error occurred while executing command on shell :" + " ".join(self.cmd) + ' ' + standard_error
        self.flag = 1
        raise ValueError(self.status_message)

    self.cmd  = [sas_path,'-config',sas_config_path,'-sysin',sas_code_path]

我不包括 SAS 的 autoexec_path,因为我没有找到任何 autoexec 文件。如果我有 autoexec 文件,

self.cmd = [sas_path,'-config',sas_config_path,'-autoexec',autoexec_path,'-sysin',sas_code_path]

问题是SAS代码执行成功,但proc.returncode不等于0。因此,我的python代码不知道代码运行成功。
有什么我做错了吗?

标签: pythonsassubprocesspopen

解决方案


您可能在日志中有警告。

来自文档“SAS® 9.4 Companion for Windows, Fifth Edition”

返回代码和完成状态

SAS 作业完成的返回码在 Windows 批处理变量 ERRORLEVEL 中返回。

ERRORLEVEL 变量的值

在此处输入图像描述


推荐阅读