BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
subhani4
Obsidian | Level 7

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!!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15
/*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()

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

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. 

yabwon
Onyx | Level 15
/*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()

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



subhani4
Obsidian | Level 7
Thanks alot. It works!! 🙂 🙂 😄
subhani4
Obsidian | Level 7

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;

@yabwon 

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Quentin
Super User

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

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 748 views
  • 3 likes
  • 4 in conversation