Help using Base SAS procedures

LIBNAME error trapping?

Reply
Frequent Contributor
Posts: 90

LIBNAME error trapping?

[ Edited ]

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.

Super User
Posts: 3,105

Re: LIBNAME error trapping?

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

 

 

Frequent Contributor
Posts: 90

Re: LIBNAME error trapping?

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.

Super User
Posts: 3,105

Re: LIBNAME error trapping?

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
Super User
Posts: 6,499

Re: LIBNAME error trapping?

[ Edited ]

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.

 

Ask a Question
Discussion stats
  • 4 replies
  • 407 views
  • 4 likes
  • 3 in conversation