BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

All, 
   I am trying to find an example of how to perform error handling in SAS with a construct similar to try/catch. I understand Base SAS does not offer the try/catch construct. However, I am wondering if there is a simple way to mimic this? I have an internal api that I call with a custom made macro - which works fine, however I would like to build some error handling capability where I can say some thing like this: 

 

Try 
   %MyApiCall(); 
Catch 
    %writetolog(); 

   I am more familiar with JMP ( a SAS product) and here is how similar construct is achieved in JMP :

Try( 
	If( Random Uniform() < 0.5, 
		1, 
		Throw() 
	  ), 
	"thrown" 
   );
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Ah, you want to use the automatic macro variables in some of these cases. 

 

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n1wrevo4roqsnxn1fbd9yezxvv9k.htm&docse...

 

For example, SYSERR will contain a different value if a proc errors out, versus if it completes successfully. In the example below, you get 3000 if it fails and 0 if it is successful. Then you can use that macro variable in the conditional logic to execute your code conditionally based on the macro variables values.

 

proc means data=sashelp.class;
class sex;
var wieght;
run;

%put &syserr;

proc means data=sashelp.class;
class sex;
var weight;
run;

%put &syserr;

Log:

 

208  proc means data=sashelp.class;
209  class sex;
210  var wieght;
ERROR: Variable WIEGHT not found.
211  run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds

212
213  %put &syserr;
3000
214


215  proc means data=sashelp.class;
216  class sex;
217  var weight;
218  run;

NOTE: Writing HTML Body file: sashtml.htm
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.62 seconds
      cpu time            0.20 seconds


219
220  %put &syserr;
0

View solution in original post

4 REPLIES 4
Reeza
Super User

There isn't enough detail in your question to provide a specific answer.

 

You'd handle it with SAS macro logic most likely. 

 

Here's a contrived example, where there's conditional execution of a procedure:

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p011imau3tm4jen1us2a45cyenz9.htm&docse...

 

Or it could be as simple as conditional logic within a data step. 


@UdayGuntupalli wrote:

All, 
   I am trying to find an example of how to perform error handling in SAS with a construct similar to try/catch. I understand Base SAS does not offer the try/catch construct. However, I am wondering if there is a simple way to mimic this? I have an internal api that I call with a custom made macro - which works fine, however I would like to build some error handling capability where I can say some thing like this: 

 

Try 
   %MyApiCall(); 
Catch 
    %writetolog(); 

   I am more familiar with JMP ( a SAS product) and here is how similar construct is achieved in JMP :

Try( 
	If( Random Uniform() < 0.5, 
		1, 
		Throw() 
	  ), 
	"thrown" 
   );

 

UdayGuntupalli
Quartz | Level 8

@Reeza , 

 

     Here is an example: 

 

Proc HTTP
		URL = &QAUrl.
		Method = "get"
	  	Out = Resp; 
Run;


* Assign Libname for parsing ;
Libname in json "&WorkDir./Test.txt";

   In an ideal world, if everything goes well, the above would work fine and I would parse the data into a dataset using something like the following: 

Data TestDetrails;
		set in.root; 
Run; 

   However, when something goes wrong, the error occurs when I try to assign a libname. So, I don't know what is the logical condition to test for here ? For e.g. I don't have a way of figuring out if a 400 code got returned in response to my incorrect api call or if my Test.txt file is empty. I hope that adds a little more color. 

 

   I definitely follow and understand how I could use a logical test for error handling, here is an example, but I don't know what an appropriate logical test would be when you can have different errors rather than a definitive logical condition like a dataset existence ? 

 

%checkds(Work.ErrorLog); *Check if Data set exists;
%If &ExistTest = 1 %Then
	%Do; 
		%Let OutputFilePath = &OutputFilePath&FileSeperator&MyDate&SpaceSeperator&MyTime&MyFile; 
		%Put &OutputFilePath; 
		Proc Export Data = Work.errorlog
			 Outfile = "&OutputFilePath."
			 Dbms = csv
			 Replace;
		Run;
	%End; 
	
Data _null_;
	Stop; 
Run; 

 

Reeza
Super User

Ah, you want to use the automatic macro variables in some of these cases. 

 

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n1wrevo4roqsnxn1fbd9yezxvv9k.htm&docse...

 

For example, SYSERR will contain a different value if a proc errors out, versus if it completes successfully. In the example below, you get 3000 if it fails and 0 if it is successful. Then you can use that macro variable in the conditional logic to execute your code conditionally based on the macro variables values.

 

proc means data=sashelp.class;
class sex;
var wieght;
run;

%put &syserr;

proc means data=sashelp.class;
class sex;
var weight;
run;

%put &syserr;

Log:

 

208  proc means data=sashelp.class;
209  class sex;
210  var wieght;
ERROR: Variable WIEGHT not found.
211  run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds

212
213  %put &syserr;
3000
214


215  proc means data=sashelp.class;
216  class sex;
217  var weight;
218  run;

NOTE: Writing HTML Body file: sashtml.htm
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: PROCEDURE MEANS used (Total process time):
      real time           0.62 seconds
      cpu time            0.20 seconds


219
220  %put &syserr;
0
rlandbeck
Calcite | Level 5

It's been months, but I figured I'd drop this here anyway.

 

If you've narrowed the source of any problems down to the libname statement, use the libname function instead, and check the return code.

Documentation with examples:

https://bit.ly/397oFMr

 

HTH,

Rebekah

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 20289 views
  • 6 likes
  • 3 in conversation