Hello Forum -
I need to trap any potential libname errors that occur inside a macro, and make them available to the remainder of my SAS program (so that I can output results of connectivity test.). Below is my code. How can I modify it to accomplish this? I have seen this post, but it did not help: https://communities.sas.com/t5/SAS-Procedures/LIBNAME-error-trapping/m-p/238132.
Please, and Thank you in advance.
%MACRO test_connection();
%DO I = 1 %TO &IDS;
%IF "&&type&I." EQ "DB2" /* success*/
%THEN
%DO;
LIBNAME DB2TST DB2 DATAsrc="&&datasrc&i." USER="&&user&i." PASSWORD="&&pwd&i." SCHEMA="&&schema&i." access=readonly;
/* Trap error here.
What code can trap an error, if it occurs, and make it available to the rest of the SAS program? */
%END;
%END;
%RUN;
%MEND test_connection;
%test_connection();
Check the automatic macro variable SYSLIBRC.
And for relational databases SYSDBMSG and SYSDBRC.
429 libname xx oracle '||||'; ERROR: Incorrect syntax for this LIBNAME statement. This engine does not accept a physical name. ERROR: Error in the LIBNAME statement. 430 %put &=syslibrc ; SYSLIBRC=5780013 431 %put &=sysdbmsg; SYSDBMSG=ORACLE: ERROR: Incorrect syntax for this LIBNAME statement. This engine does not accept a physical name. 432 %put &sysdbrc; -1
Set macro variable SYSCC to 0 before you issue the libname, and check it after. A non-zero value indicates a problem.
Check the automatic macro variable SYSLIBRC.
And for relational databases SYSDBMSG and SYSDBRC.
429 libname xx oracle '||||'; ERROR: Incorrect syntax for this LIBNAME statement. This engine does not accept a physical name. ERROR: Error in the LIBNAME statement. 430 %put &=syslibrc ; SYSLIBRC=5780013 431 %put &=sysdbmsg; SYSDBMSG=ORACLE: ERROR: Incorrect syntax for this LIBNAME statement. This engine does not accept a physical name. 432 %put &sysdbrc; -1
The same way you would put any macro variable value into a dataset variable.
For example just expanding the value.
numvar = &digitstring;
charvar= "&charstring";
Or using function call.
numvar=symgetn('digitstring');
charvar=symget('charstring');
If you have the list of values in a data set you might be better off ditching the macro a see if you can just the LIBNAME() function in a single data step to test all of the different values are once. That would be a lot simpler than trying get macro code and macro variables to work. Something like this:
data want;
set have ;
rc = libname(......);
run;
Check the documentation on the LIBNAME() function to work out the details. You could probable use the SYMGET() function to return the values of the automatic macro variables after each LIBNAME() function call.
These are the coding changes I made, but I my libstat&I isn't being assigned anything...what could possible sytax corrections be for this line:
%let libstat&I = syslibrc;
x=symget("libstat"|| cat(I)); - x has nothing in it.
%MACRO test_connection();
%global libstat&I;
%DO I = 1 %TO &IDS;
%IF "&&type&I." EQ "DB2" /* success*/
%THEN
%DO;
LIBNAME DB2TST DB2 DATAsrc="&&datasrc&i." USER="&&user&i." PASSWORD="&&pwd&i." SCHEMA="&&schema&i." access=readonly;
/* Trap error here.
What code can trap an error, if it occurs, and make it available to the rest of the SAS program? */
%put &=syslibrc;
%let libstat&I = syslibrc;
%END;
%END;
%RUN;
%MEND test_connection;
%test_connection();
data work.fail;
do i=1 to &ids;
x=symget("libstat"|| cat(I));
if x ^=0 then output;
end;
run;
Proc export data=work.fail;
outfile="/mydir/fail.xlsx"
dbms=XLSX replace;
run;
You made LOCAL macro variables and then tried to read them after they were already gone because the macro had finished running.
Ok - So I moved my global var definition downwhere I am doing the libstat&I assignment, and now it works. Thank you!
%MACRO test_connection();
%DO I = 1 %TO &IDS;
%IF "&&type&I." EQ "DB2" /* success*/
%THEN
%DO;
LIBNAME DB2TST DB2 DATAsrc="&&datasrc&i." USER="&&user&i." PASSWORD="&&pwd&i." SCHEMA="&&schema&i." access=readonly;
/* Trap error here.
What code can trap an error, if it occurs, and make it available to the rest of the SAS program? */
%put &=syslibrc;
%global libstat&I;
%let libstat&I = syslibrc;
%END;
%END;
%RUN;
%MEND test_connection;
%test_connection();
data work.fail;
do i=1 to &ids;
x=symget("libstat"|| cat(I));
if x ^=0 then output;
end;
run;
Proc export data=work.fail;
outfile="/mydir/fail.xlsx"
dbms=XLSX replace;
run;
Sounds good.
It is still going to be a LOOOOOOOT easier to skip the macro code.
LIBNAME DB2TST DB2 DATAsrc="&&datasrc&i." USER="&&user&i." PASSWORD="&&pwd&i." SCHEMA="&&schema&i." access=readonly;
From the names of the macro variables it looks like you started with a DATASET that had variables name DATASRC, USER , PWD and SCHEMA. So just use that dataset directly to generate the LIBNAME() function call and then save the return code.
You can do all of that, including the looping, in a DATA step using the LIBNAME function and avoid a macro loop altogether.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.