发生错误时的 SAS 电子邮件 [英] SAS Email If Errors Occur

查看:68
本文介绍了发生错误时的 SAS 电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何代码/宏可以合并到我的 sas 程序中,当我的 sas 代码在运行时发生错误时,它会立即向我发送电子邮件?

is there any code/macro I can incorporate into my sas program that will email me as soon as an error occurs in my sas code while it is running?

该电子邮件是否也可能包含发生的错误?

Also is it possible for this email to contain the error that happened?

推荐答案

是.... 没有...

Yes.... and no...

这是可能的 - 但没有好的方法可以做到.您必须在每个程序之后检查以查看是否发生错误.基本上,您会在整个代码中插入一行代码来执行数十次(或数百次)测试.

This is possible - but there's no nice way to do it. You would have to check after each procedure to see if an error occurred. Basically you would insert a line of code to perform the test dozens (or hundreds) of times throughout your code.

我经常发现在整个程序运行后执行测试就足够了.如果中途确实发生了错误,那么 SAS 通常无论如何都会进入语法检查模式,这样它就不会在发生错误后执行任何代码.

I often find that it's sufficient to perform a test after the entire program has run. If an error did occur halfway through then SAS will usually enter syntax check mode anyway so it won't execute any code after the error.

这种方法也有它自己的一系列问题.从包含错误信息的 SYS 宏变量开始,只存储最近错误的信息.我们可以检查日志,但问题在于我们当前正在运行的程序仍在使用该日志,并检查哪些阻止我们使用 SAS 打开它以从中读取.

This approach also comes with it's own separate set of problems. To start with the SYS macro variables that contain information about errors only store info for the most recent error. We could check the log, but the problem with this is that the log is still in use by the program we are currently running and to examine which prevents us from using SAS to open it to read from it.

我解决这个问题的方法是两次调用 SAS.第一次运行程序并将日志保存到指定文件中.第二次运行将检查刚刚创建的日志文件并在满足特定条件时发送电子邮件的程序(例如以 ERROR: 开头的行.

The way I get around this is to call SAS twice. The first time to run the program and save the log to a specified file. The second time to run a program that will check the logfile that was just created and to send an email if specific criteria are met (such as the line begins with ERROR:.

一些细节...当您第二次启动sas时,您可以使用SYSPARM参数传入您要检查的日志文件的名称.您可以使用类似这样的代码解析日志文件(我建议您根据自己的情况自定义条件):

Some details... when you launch sas the 2nd time, you can use the SYSPARM parameter to pass in the name of the logfile you want to check. You can parse the logfile with code something like this (I suggest you customize the conditions to your own situation):

**
** 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 中发送电子邮件,但如果不知道,我建议您先在谷歌上搜索它,如果您仍然无法使用它,请回来并发布一个单独的问题.

It's not clear from your question whether you know how to send an email in SAS, but if not I suggest googling it first and if you still can't get it working then come back and post a separate question.

EDITS:您可以在调用 SAS 时使用 -LOG "myfile.log" 参数指定 SAS 应将日志文件保存到的位置.

EDITS: You can specify where SAS should save the logfile to by using the -LOG "myfile.log" parameter when you call SAS.

这篇关于发生错误时的 SAS 电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆