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.
I suggest you check out the LIBNAME function which will allow you to trap return codes and error messages:
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.
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));
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.
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.