楼主: Imasasor
12390 11

[问答] 求个sas日志出现error后自动报错的宏程序 [推广有奖]

  • 1关注
  • 64粉丝

VIP

已卖:215份资源

学科带头人

33%

还不是VIP/贵宾

-

TA的文库  其他...

超哥喜欢的文章

威望
1
论坛币
47033 个
通用积分
3.1376
学术水平
238 点
热心指数
246 点
信用等级
231 点
经验
37102 点
帖子
849
精华
3
在线时间
2235 小时
注册时间
2012-7-4
最后登录
2024-10-10

初级学术勋章 初级热心勋章 初级信用勋章 中级热心勋章 中级学术勋章

楼主
Imasasor 发表于 2013-2-21 20:54:21 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
经常会遇到这样一些事情,做了一个很大的宏程序,中间有很多data步,很多循环,跑了很长时间,导致日志几十页长。
跑完了之后,首先要看日志有没有错误,拉了几十页慢慢找红色的error,找到错误再慢慢调试代码。

有没有什么方法,可以实现以下功能.
1: 程序跑完后,日志如果有红色的error,自动出来ods界面(或其它弹出界面),提示错误,甚至显示出错误日志位置上下一定区域的日志。
2:有的时候,还想看到warning情况,可否也类似上述error情况给出提示。
3:  因为跑一个程序要好久,可否控制日志出现error后,终止sas执行过程,随后的程序都不再执行。




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Error 宏程序 err warning data步 程序 error

已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
webgu + 60 + 60 + 1 + 1 + 1 好问题。

总评分: 经验 + 60  论坛币 + 60  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

本帖被以下文库推荐

欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

沙发
tangliang0905 发表于 2013-2-22 01:55:48
1.可以将log文件printto到txt文件中,然后在文件中用SCAN,或者index寻找“ERROR”,这样可以找到所有的error message

2.不必将warning专门列出来,有时候warning只是一些类似格式上的错误,不影响下步sas的运行

3.在marco中,我只是用了一步语句“%if &SYSERR > 6 %then %goto STOPLOG;”。至于为什么&syserr是大于6,这只是我自己工作经验总结,然后把STOPLOG放在最后一步,这样就可以跳出macro了。

一点愚见,大家可以讨论。


已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Imasasor + 80 + 100 + 2 + 2 + 1 分析的有道理

总评分: 经验 + 80  论坛币 + 100  学术水平 + 2  热心指数 + 2  信用等级 + 1   查看全部评分

藤椅
可~乐 发表于 2013-2-22 22:23:15
这是之前一些做法,你看看是否有帮助。。。
  1. /*输出错误状态和错误信息*/
  2. %macro error_handle(information);
  3. %global status scripts_info;

  4. /*自动宏变量syserr为0表示运行成功,无error和warning,为4表示成功运行但是有warning,自动宏变量syserrortext为日志中错误信息
  5. status表示运行状态,scripts_info表示程序中那一步出错*/
  6. %if &syserr^=0 and &syserr^=4 %then %do;
  7.         %put 'error'::&syserrortext.;
  8.         %let status=ERROR;
  9.         %let scripts_info=&information;
  10. %end;
  11. %else %do;
  12.         %let status=OK;
  13.         %let scripts_info=程序执行成功;       
  14. %end;

  15. %mend;

  16. /*生成文件并写入简要日志*/
  17. %macro create_file();
  18. data _null_;
  19. file "d:\&name..jk";
  20. put "&name." @;        /*name为程序的名称*/
  21. put "|" @;
  22. put "&date." @;        /*date为当天运行日期*/
  23. put "|" @;
  24. put "&begin_time." @; /*begin_time为程序开始运行时间*/
  25. put "|" @;
  26. put "&end_time." @;        /*end_time为程序运行结束时间*/
  27. put "|" @;
  28. put "&status." @; /*status为程序运行状态,OK或者ERROR*/
  29. put "|" @;
  30. put "&scripts_info." @;        /*scripts_info为详细信息*/
  31. put "|" ;
  32. run;

  33. /*若成功运行,则把文件修改为.ok文件*/
  34. %if %quote(&status.)=%quote(OK) %then %do;
  35.         data _null_;
  36.                 _rc=rename("d:\&name..jk","d:\&name..ok",'file');
  37.         run;
  38. %end;

  39. /*若失败,则把文件修改为.error文件*/
  40. %else %do;
  41.         data _null_;
  42.                 _rc=rename("d:\&name..jk","d:\&name..error",'file');
  43.         run;

  44. %end;

  45. %mend;

  46. /*以下是一个例子,当生成test数据集时出错,然后就跳到mend_end后面的位置,所以就不执行建立数据集test1的步骤,
  47. 然后生成error文件*/
  48. %macro test;

  49. %let name=test;

  50. %let date=%sysfunc(putn(%sysfunc(date()),yymmdd10.));

  51. %let begin_time=%sysfunc(datetime());

  52. proc printto log="d:\&name..txt";
  53. run;

  54. data test;
  55.         'x+1';
  56. run;

  57. %error_handle(生成数据集&name.出错);
  58. %if &syserr^=0 and &syserr^=4 %then %do;%goto mend_end; %end;

  59. data test1;
  60.         set sashelp.class;
  61. run;

  62. %error_handle(生成数据集test1出错);
  63. %if &syserr^=0 and &syserr^=4 %then %do;%goto mend_end; %end;

  64. %mend_end:

  65. %let end_time=%sysfunc(datetime());

  66. %create_file();
  67. %mend;

  68. %test;
  69. proc printto;
  70. run;
复制代码
已有 3 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
米兰_卡卡 + 1 + 1 + 1 观点有启发
Imasasor + 100 + 100 + 4 + 4 + 4 精彩帖子
webgu + 60 + 60 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 160  论坛币 + 160  学术水平 + 6  热心指数 + 6  信用等级 + 6   查看全部评分

板凳
bobguy 发表于 2013-2-23 10:10:22
tangliang0905 发表于 2013-2-22 01:55
1.可以将log文件printto到txt文件中,然后在文件中用SCAN,或者index寻找“ERROR”,这样可以找到所有的err ...
Good tips:

The errors are usually caused by wrong syntax, or specify a non-exist variable, or say compiling errors.

One alway use run cancel; statement to check the syntax first. See below.

340  data a;
341    set sashelp.class;
342    where age2=0;
ERROR: Variable age2 is not on file SASHELP.CLASS.
343  run cancel;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.A may be incomplete.  When this step was stopped there were 0
         observations and 5 variables.
WARNING: Data set WORK.A was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


344
345  proc means data=sashelp.class;
346  var _all_;
ERROR: Variable Name in list does not match type prescribed for this list.
ERROR: Variable Sex in list does not match type prescribed for this list.
347  run cancel;

WARNING: The procedure was not executed at the user's request.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
webgu + 1 + 1 + 1 观点有启发

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

报纸
webgu 发表于 2013-2-23 11:43:24
playmore原来贴一个PDF.

%macro RunQuit();
  ;run;quit;
  %if &syserr. ne 0 %then %do;
     %abort cancel;
         %end;
%mend;


data tmp;
   set sashelp.class;
   where age2=0;
%runquit;
data tmp2;
  x=1;
%runquit ;
SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

地板
Imasasor 发表于 2013-2-23 17:01:53
webgu 发表于 2013-2-23 11:43
playmore原来贴一个PDF.

%macro RunQuit();
试了一下,这个不错,可以达到目的,不过syserr=4的时候表示waring,如果出现waring不让停止的话,还要加上ne 4。另外, 不知道怎么把这个宏变成永久的宏,以后使用的时候,直接调用,不写宏代码了。
欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

7
webgu 发表于 2013-2-23 17:29:05




把你的宏存一个SAS文件,假使命名为 runquit.sas 放在一个文件夹。
用时加个Header code.
filename  mymacros   'd:\myacro'; /*mymacro folder path*/

options
  mautosource sasautos=(sasautos mymacros)
;

data _null_;
     set sashelp.class;
   if age2=0;
%runquit;
SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

8
playmore 发表于 2013-2-23 19:10:53
其实在Log窗口直接按ctrl+F然后搜WARN或ERROR就可以了
但是另一个最严重的问题是你在Log里看到了ERROR,但有时很难定位发生错误的地方,尤其在宏套宏的时候
我现在变通的方法是,
首先,所有的宏,其中的临时表或临时宏变量全部用宏的名称首字母作前缀,方便查找
其次,在较长的宏中分步,每步put出一行标题,便于定位
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
webgu + 1 + 1 + 1 Good Practice

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

9
Bridgenc 发表于 2013-2-23 21:39:20
Here is what I use:

%macro lastmsg;
%* Check last warning message;
%put;
%if %bquote(&syswarningtext) eq %then
%put No warnings generated so far in this SAS session;
%else %do;
%put Last warning message generated in this SAS session:;
%put &syswarningtext;
%end;
%put;
%* Check last error message;
%if %bquote(&syserrortext) eq %then
%put No error messages generated so far in this SAS session;
%else %do;
%put Last error message generated in this SAS session:;
%put &syserrortext;
%put;
%end;
%put;
%mend lastmsg;

%lastmsg
已有 1 人评分经验 论坛币 收起 理由
Imasasor + 100 + 100 观点有启发

总评分: 经验 + 100  论坛币 + 100   查看全部评分

10
hs4601 发表于 2013-12-15 16:15:24

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 16:34