是否有任何代码/宏可以合并到我的sas程序中,当我的sas代码在运行时发生错误时会立即给我发电子邮件?
此电子邮件是否也可能包含发生的错误?
答案 0 :(得分:2)
是......而且没有......
这是可能的 - 但是没有好办法。您必须在每个过程后检查是否发生错误。基本上,您将插入一行代码以在整个代码中执行数十(或数百)次测试。
我经常发现在整个程序运行后执行测试就足够了。如果在中途发生错误,那么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=( test@test.test )
from=("myemail@test.test")
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中发送电子邮件,但如果不是,我建议先使用Google搜索,如果您仍然无法使其正常工作,请返回并发布单独的问题。
编辑:您可以在致电SAS时使用-LOG "myfile.log"
参数指定SAS应将日志文件保存到的位置。
答案 1 :(得分:0)
这篇简明的文章给出了简单的SAS代码,其中显示了如何检查错误并在错误情况下发送电子邮件。正如Rob已经提到的,你应该在每个程序之后检查错误代码。
https://heuristically.wordpress.com/2012/02/09/return-codes-errors-sas/
此代码不会发送日志,但您可以通过定义一个宏变量来发送一般错误,其中包含“无法查询SQL数据库”等简要说明。在我们的组织中,我们在批处理作业中运行大约50-100(?)个SAS程序,我们使用日期戳保存每个日志,例如 2012-03-05 13:05 daily job.log