BookmarkSubscribeRSS Feed
Lapis Lazuli | Level 10

Hello all,

I am working with a lot of metadata, opening thousands of different db files: access, sas, dbf, and more.  I wanted to add a feature that traps my errors not in the log file but in a new data set or on the master list of my list of files indicateing I had an error opening/connecting the LIBNAME. Does anyone have any starter code on this?  Any help would be greatly apresheated for a new SAS guy (April/May2015).   TIA. -KJ



ERROR: Connect: Could not find file 'D:\MyPath\MyMdb.MDB'.
ERROR: Error in the LIBNAME statement.

Opal | Level 21

I suggest you check out the LIBNAME function which will allow you to trap return codes and error messages:



Lapis Lazuli | Level 10

Is this remotly close?

for my program:

     LIBNAME MYMDB ACCESS PATH="D:\MyDir\MyFile.MDB" access=readonly;

works and I can get my metadata.


when I try to addapt it to this web page sample:

%let mylib=c:\projects\May2015;
%if %sysfunc(libname(new,&mylib)) %then
%put %sysfunc(sysmsg());

I come up with this:


%let myfile='D:\MyDir\MyFile.MDB';
%if %sysfunc(libname('MYMDB', 'ACCESS', &myfile, 'readonly')) %then
%put %sysfunc(sysmsg());


and get this error:

ERROR: The %IF statement is not valid in open code. 


I am not sure what to try, I have tryied adding and removing quotes on the arguments.

Opal | Level 21

To use a %IF statement it must be inside a macro. Try this:

%macro Assign_Libname (MyFile = );

%if %sysfunc(libname('MYMDB', 'ACCESS', "&myfile", 'readonly')) %then
%put %sysfunc(sysmsg());

%mend Assign_Libname;

%Assign_Libname (MyFile = %str(D:\MyDir\MyFile.MDB)); 


Super User Tom
Super User

Do it in a data step instead of using macro code. Especially if you want to create metadata.

data data_sources;
  infile cards truncover;
  length engine libref $8 rc 8 path msg $200 ;
  input engine $ path $200. ;
  if rc then msg=sysmsg();
  else msg='SUCCESS';
  rc=libname(libref,' ');
base c:\downloads
base c:\notfound
xlsx c:\downloads\DSPI.xlsx


287   data _null_; set ; put (_all_) (=/); run;

engine=base libref=L0000001 rc=0 path=c:\downloads msg=SUCCESS engine=base libref=L0000002 rc=-70008 path=c:\notfound msg=NOTE: Library L0000002 does not exist. engine=xlsx libref=L0000003 rc=0 path=c:\downloads\DSPI.xlsx msg=SUCCESS NOTE: There were 3 observations read from the data set WORK.DATA_SOURCES.




Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg



Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 3 in conversation