BookmarkSubscribeRSS Feed
kjohnsonm
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.

4 REPLIES 4
SASKiwi
PROC Star

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

 

http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#p1bq8nyxm7y...

 

 

kjohnsonm
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.

SASKiwi
PROC Star

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)); 

 

Tom
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. ;
  libref='L'||put(_n_,Z7.);
  rc=libname(libref,path,engine,'access=readonly');
  if rc then msg=sysmsg();
  else msg='SUCCESS';
  output;
  rc=libname(libref,' ');
cards;
base c:\downloads
base c:\notfound
xlsx c:\downloads\DSPI.xlsx
;;;;

results:

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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2946 views
  • 6 likes
  • 3 in conversation