Hi,
i have one main program called main.sas. it has code as below;
option mprint mlogic symbogen;
%let path=dummy;
%include "&dummy/test1.sas;
%include "&dummy/test2.sas;
%include "&dummy/test3.sas;
%include "&dummy/test4.sas;
%include "&dummy/test5.sas;
these five subsequent program genarates work tables from sas librabries and test5 will have the final output dataset. the dataset will be exported as .csv file.
i want like if any one of (test1,test2,test3,test4,test5) fails the main program should stop/abort immediately generating and error message as "main.sas program failed and not executed. Program execution fails in test1/test2/test3/test4/test5.sas either of these 5 programs."
ex: "main.sas program failed and not executed. Program execution fails in test3.sas"
Could anyone help me in achieving the solution for the above. Thanks in advance!!
/*lets assume the following setup*/
filename f "R:\folder with my sas codes\";
data _null_;
file f(code1.sas);
put "data test;" / "x = 17; output;" / "x = 42; output;" / "run;";
file f(code2.sas);
put "proc sort data = testNOdata;" / "by x;" / "run;";
file f(code3.sas);
put "proc transpose data = test out = transposed_data;" / "var x;" / "run;";
run;
and then:
/* test if they work */
%macro myMacro();
%let syscc=0;
%include f(code1.sas);
%if &syscc. > 0 %then %do; %Put ERROR: in code!; %abort; %end;
%include f(code2.sas);
%if &syscc. > 0 %then %do; %Put ERROR: in code!; %abort; %end;
%include f(code3.sas);
%if &syscc. > 0 %then %do; %Put ERROR: in code!; %abort; %end;
%mend myMacro;
%myMacro()
Just adding the ERRORABEND option to your OPTIONS statement will give you the desired behaviour although you won't be able to add log messages as SAS aborts immediately. Please bear in mind it will also close your EG session so you really need to run this program in batch mode with this option.
options mprint mlogic symbogen errorabend;
There is no option in SAS to stop all current processing but keep the session open. There is another option SYNTAXCHECK, that will stop processing any data that will leave your session open.
/*lets assume the following setup*/
filename f "R:\folder with my sas codes\";
data _null_;
file f(code1.sas);
put "data test;" / "x = 17; output;" / "x = 42; output;" / "run;";
file f(code2.sas);
put "proc sort data = testNOdata;" / "by x;" / "run;";
file f(code3.sas);
put "proc transpose data = test out = transposed_data;" / "var x;" / "run;";
run;
and then:
/* test if they work */
%macro myMacro();
%let syscc=0;
%include f(code1.sas);
%if &syscc. > 0 %then %do; %Put ERROR: in code!; %abort; %end;
%include f(code2.sas);
%if &syscc. > 0 %then %do; %Put ERROR: in code!; %abort; %end;
%include f(code3.sas);
%if &syscc. > 0 %then %do; %Put ERROR: in code!; %abort; %end;
%mend myMacro;
%myMacro()
Hi,
i have tried to add two statements one is %let and other is x command in the do loop to execute when include fails and abort the whole main program as below. But the %let statement and x command is not working. Please provide your inputs on the same. Thanks!!
%macro myMacro();
%let syscc=0;
%include "&source_path/subbu/sub1.sas";
%if &syscc. > 0 %then
%do;
%Put ERROR: main.sas program failed. Program execution fails in sub1.sas!;
%LET error_string=%str('main.sas program failed. Program execution fails in sub1.sas!');
x "sh &saspath/dummy.sh &error_string";
%abort;
%end;
%include "&source_path/subbu/sub2.sas";
%if &syscc. > 0 %then
%do;
%Put ERROR: main.sas program failed. Program execution fails in sub2.sas!;
%LET error_string=%str('main.sas program failed. Program execution fails in sub2.sas!');
x "sh &saspath/dummy.sh &error_string";
%abort;
%end;
%include "&source_path/subbu/sub3.sas";
%if &syscc. > 0 %then
%do;
%Put ERROR: main.sas program failed. Program execution fails in sub3.sas!;
%LET error_string=%str('main.sas program failed. Program execution fails in sub3.sas!');
x "sh &saspath/dummy.sh &error_string";
%abort;
%end;
%mend mymacro;
%mymacro;
Could you explain what is the reason for that additional statements? It's nor quite clear for me what you try to get.
And, please, us the "insert SAS code" option (running man icon above message box) to paste sas code. It's hard to read without formating.
Bart
You've already gotten some great advice. Look into the system options for error handling, and consider using return codes to detect failures.
I would encourage you to think through your definition of failure. Is an error a failure? Is a warning a failure? Is a bad note a failure? For one program, I actually scan the log after key steps, and if I don't judge the log to be clean, I treat that as a failure.
One benefit of using the return code approach is it allows you to add your own logic to create a failure if there is an unexpected data value, or some other indication of a problem.
Troy Hughes has some great papers on error handling using both the return code approach and log scanning, e.g.
https://support.sas.com/resources/papers/proceedings15/3387-2015.pdf
https://www.lexjansen.com/sesug/2017/PSA-209.pdf
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: