Posted By

sastechies on 10/12/10


Tagged

errors search macros and report warnings logs sas


Versions (?)

SAS Macros to Search and Report Errors and Warnings from your SAS Logs


 / Published in: SAS
 

URL: http://sastechies.blogspot.com/2010/10/sas-macros-to-search-and-report-errors.html

  1. %macro recsinds(table,macvar);
  2. /*----------------------------------------------------------------------
  3.   this macro can be used to get the count of observations in a dataset
  4.   into a macrovariable that you give.
  5.  
  6.   usage: %recsinds(sashelp.class,macvar);
  7.  
  8.   &macvar is the observation count of your dataset
  9.  ----------------------------------------------------------------------*/
  10.  
  11. %global &macvar;
  12. %local handle rc;
  13. %let handle = %sysfunc(open(&table)); /* open the table */
  14. %if &handle %then
  15. %do;
  16. /* get the observation count into the macvar macro variable */
  17. %let &macvar = %sysfunc(attrn(&handle, nlobs));
  18. %let rc = %sysfunc(close(&handle)); /* close the dataset */
  19. %end;
  20. /* write the record count to the log */
  21. %put recsinds &table: &&&macvar. &macvar=&&&macvar.;
  22.  
  23. %mend recsinds;
  24.  
  25. %macro SearchErrorsWarnings(strg,loc,dsx,mm=1);
  26. * mm=# of lines to display before and after target lines. mm=0,1,2 ...etc.;
  27.  
  28. filename dd "&loc.\&dsx.";
  29. %global nnn; *nnn=# of records in dsx;
  30.  
  31. data aa (keep= filen arrow linum xx);
  32. infile dd length=ln end=last;
  33. length llprted pthru 8 arrow $3 xx $120 filen $400;
  34. retain llprted pthru 0;
  35. *LLPRTed:line # of Last Line Printed; * PThru: to be line # of the last of: * the 2mm+1 lines to be displayed;
  36. length %do jj=1 %to &mm; x&jj %end; XT $ 120;
  37. retain filen XT ' ' %do jj=1 %to &mm; x&jj %end;;;
  38.  
  39. filen="&dsx.";
  40.  
  41. * These store the mm lines that are; * to be printed prior to a Find; * Next save these mm lines;
  42. %do jj=%eval(&mm-1) %to 1 %by -1;
  43. x%eval(&jj+1)=x&jj;;
  44. %end;
  45.  
  46. x1=XT;
  47. input XT $varying120. ln;
  48. XT=upcase(XT);
  49. if index (XT,"THE SAS SYSTEM")>0 then delete;
  50. if index (XT,upcase("&strg"))>0 then
  51. do;
  52. filen="========================================";linum=.; arrow='====='; xx='========================================================================================';
  53. output;
  54. filen="&dsx."; arrow='';
  55.  
  56. * Found one!; * Insert a blank line between finds; * provided the scopes of two finds ; * do not overlap;
  57. if (_n_ > pthru + &mm + 1) & pthru > 0
  58. then
  59. do;
  60. filen="========================================";linum=.; arrow='====='; xx='========================================================================================';
  61. output;
  62. filen="&dsx."; arrow='';
  63. end;
  64. *Output mm lines preceding the find;
  65. %do jj=&mm %to 1 %by -1;
  66. if &jj<_n_ - pthru then
  67. do;
  68. xx=x&jj;
  69. linum=_n_ - &jj;
  70. output;
  71. end;
  72. %end;
  73. linum=_n_;
  74. xx=XT;
  75. if &mm > 0 then arrow="-->";
  76. llprted=_n_;
  77. output; * Output the Found line;
  78.  
  79. * Compute pthru, the line # of the ; * last line of the scope so that the; * next mm lines can be printed;
  80. pthru=_n_+&mm;
  81. arrow=' ';
  82. end;
  83. else
  84. do;
  85. if _n_<=pthru then
  86. do;
  87. linum=_n_;
  88. xx=XT;
  89. output;
  90. * Outputting the next mm lines;
  91. end;
  92. end;
  93. if last then call symput('nnn',left(put(_n_,5.)));
  94. run;
  95.  
  96. proc append base=allout data=aa; run;
  97. %mend SearchErrorsWarnings;
  98.  
  99. %macro ReportErrorsWarnings(searchstr,searchloc,searchfiles,outfile,lines2display);
  100.  
  101. proc sql;
  102. drop table allout;
  103. drop table aa;
  104. drop table new;
  105. quit;
  106.  
  107. %let dircmd0=%nrbquote(%str(dir /b /s %")%str(&searchloc.\&searchfiles.)%str(%"));
  108. FILENAME rootloc pipe "&dircmd0";
  109.  
  110. /*data _null_; run;*/
  111. data new;
  112. length file $ 1000;
  113. infile rootloc;
  114. input file;
  115. cnt=count(file,"\");
  116. file=translate(file,' ','\');
  117. file=scan(file,cnt+1,' ');
  118. call symput(compress('filen'||_N_),file);
  119. run;
  120.  
  121. %recsinds(new,nobs);
  122. %if &nobs gt 0 %then %do;
  123. %do k=1 %to &nobs;
  124. %SearchErrorsWarnings(&searchstr.,&searchloc.,&&filen&k.,mm=&lines2display.);
  125. %end;
  126.  
  127. data allout;
  128. retain filen linum arrow xx;
  129. set allout;
  130. run;
  131.  
  132. data _null_;
  133. file "&outfile." lrecl=32767;
  134. set allout;
  135. if _n_ =1 then
  136. do;
  137. put "FILENAME" '09'x "LINE #" '09'x "ARROW" '09'x "MESSAGE";
  138. put "*****************************************************************************************************";
  139. end;
  140. put filen '09'x linum '09'x arrow '09'x xx '09'x;
  141. run;
  142. %end;
  143. %else
  144. %do;
  145. %put ==================;
  146. %put NO LOGS TO PROCESS;
  147. %put ==================;
  148. %end;
  149.  
  150. %mend ReportErrorsWarnings;
  151.  
  152.  
  153. %let LogLoc=C:\SASDATA;
  154. %let cyle_runtime=10OCT2010102001;
  155.  
  156. proc printto log="&LogLoc.\example1_&cyle_runtime..log"; run;
  157.  
  158. DATA example1;
  159. INPUT ID $ 1 SBP 2-4 DBP 5-7 GENDER $ 8 AGE 9-10 WT 11-13;
  160. DATALINES;
  161. 1120 80M15115
  162. 2130 70F25180
  163. 3140100M89170
  164. 4120 80F30150
  165. 5125 80F20110
  166. ;
  167. RUN;
  168. PROC MENS;
  169. RUN;
  170. proc printto; run;
  171.  
  172. proc printto log="&LogLoc.\example2_&cyle_runtime..log"; run;
  173. DATA CDS2;
  174. INPUT @1 CATEGORY $9. @10 NUMBER 3.;
  175. DATALINES;
  176. JAZZ 252
  177. POP 49
  178. CLASSICAL 59
  179. RAP 21
  180. GOSPEL 44
  181. JAZZ 21
  182. ;
  183. run;
  184. ODS RTF;
  185. PROC FREQ DATA=CDS ORDER=FREQ; WEIGHT NUMBER;
  186. TITLE3 'READ IN SUMMARIZED DATA';
  187. TABLES CATEGORY;
  188. RUN;
  189. proc printto; run;
  190.  
  191. %ReportErrorsWarnings(ERROR:,&LogLoc.,*_&cyle_runtime..log,&LogLoc.\error_&cyle_runtime..txt,2);

Report this snippet  

You need to login to post a comment.