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

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();
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

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
Smurfy
Fluorite | Level 6
Thank you! But how to I get it into a variable and available to the rest of the SAS program (not just a put to the log)? I ultimately need to output failures to an excel sheet...
Tom
Super User Tom
Super User

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.

 

Smurfy
Fluorite | Level 6

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;
Tom
Super User Tom
Super User

You made LOCAL macro variables and then tried to read them after they were already gone because the macro had finished running.

Smurfy
Fluorite | Level 6

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;
Tom
Super User Tom
Super User

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.

Smurfy
Fluorite | Level 6
Can't - I am doing a mass connection test. I need the macro to loop thru the input file that contains many database types, userids, passwords, schemas, etc.
SASKiwi
PROC Star

You can do all of that, including the looping, in a DATA step using the LIBNAME function and avoid a macro loop altogether.

SAS Innovate 2025: Register Now

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!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1598 views
  • 5 likes
  • 4 in conversation