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"
);
Ah, you want to use the automatic macro variables in some of these cases.
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
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:
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" );
@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;
Ah, you want to use the automatic macro variables in some of these cases.
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
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:
HTH,
Rebekah
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.