是否有任何代码/宏可以合并到我的 SAS 程序中,一旦我的 SAS 代码在运行时发生错误,它就会向我发送电子邮件?
另外,这封电子邮件是否可能包含所发生的错误?
是的……又不是……
这是可能的——但没有好的方法来做到这一点。您必须在每个过程之后检查是否发生错误。基本上,您将插入一行代码来在整个代码中执行数十(或数百)次测试。
我经常发现在整个程序运行之后执行测试就足够了。如果中途发生错误,那么 SAS 通常会进入语法检查模式,因此在错误发生后不会执行任何代码。
这种方法也有其自身的一系列问题。首先,包含错误信息的 SYS 宏变量仅存储最近错误的信息。我们可以检查日志,但这样做的问题是日志仍在被我们当前运行的程序使用,并且要检查这会阻止我们使用 SAS 打开它进行读取。
我解决这个问题的方法是给 SAS 打电话两次。第一次运行程序并将日志保存到指定文件中。第二次运行程序,该程序将检查刚刚创建的日志文件,并在满足特定条件时发送电子邮件(例如以以下内容开头的行)ERROR:
.
一些细节...当您第二次启动 sas 时,您可以使用SYSPARM
参数传入要检查的日志文件的名称。您可以使用类似以下的代码解析日志文件(我建议您根据自己的情况自定义条件):
**
** READ IN LOGFILE. CHECK FOR PROBLEMS
*;
data problems log;
length line $1000;
infile "&logfile";
input;
logfile = "&logfile";
line_no = _n_;
line = _infile_;
problem = 0;
if
(
line =: "ERROR:"
or line =: "WARNING:"
or line =: "NOTE: Numeric values have been converted to character values"
or line =: "NOTE: Character values have been converted to numeric values"
or line =: "NOTE: Missing values were generated as a result of performing an operation on missing values"
or line =: "NOTE: MERGE statement has more than one data set with repeats of BY values"
or line =: "NOTE: Invalid (or missing) arguments to the INTNX function have caused the function to return"
or line =: "INFO: Character variables have defaulted to a length of 200"
or line =: "NOTE: Invalid"
)
and not
(
line =: "WARNING: Your system is scheduled to expire"
or line =: "WARNING: The Base Product product with which Session Manager is associated"
or line =: "WARNING: will be expiring soon, and is currently in warning mode to indicate"
or line =: "WARNING: this upcoming expiration. Please run PROC SETINIT to obtain more"
or line =: "WARNING: information on your warning period."
or line =: "WARNING: This CREATE TABLE statement recursively references the target table. A consequence"
or line =: "WARNING: Unable to copy SASUSER registry to WORK registry. Because of this, you will not see registry customizations during this"
or line =: "WARNING: Estimates did not improve after a ridge was encountered in the objective function."
or line =: "WARNING: Estimates may not have converged."
or line =: "ERROR: A lock is not available for"
or line =: "ERROR: Errors printed on page"
or (line =: "WARNING: Apparent symbolic reference TODT not resolved." and "%upcase(&jobname)" eq "DIAL800.REPORTING_API")
)
then do;
problem = 1;
output problems;
end;
output log;
run;
filename mymail email content_type="text/html"
to=( [email protected] /cdn-cgi/l/email-protection )
from=("[email protected] /cdn-cgi/l/email-protection")
subject="mysubject"
attach="&logfile";
data _null_;
length divider $200;
file mymail;
set problems end=eof;
divider = repeat('=',80);
if _n_ eq 1 then do;
put '<font style="font-family:courier new;font-size:9pt"><br>';
put divider "<br>";
put "SUMMARY OF PROBLEMS: &logfile <br>";
put divider "<br><br>";
end;
put line_no 5. ": " line "<br>";
if eof then do;
put divider "<br>";
put "END OF SUMMARY <br>";
put divider "<br><br>";
end;
run;
从您的问题中尚不清楚您是否知道如何在 SAS 中发送电子邮件,但如果不知道,我建议先用谷歌搜索它,如果您仍然无法使其正常工作,请回来并发布一个单独的问题。
编辑:您可以使用以下命令指定 SAS 应将日志文件保存到的位置-LOG "myfile.log"
调用 SAS 时的参数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)