prior code is OK.
42 libname TEST '/sas/sasmain/mydept/Test Report';
NOTE: Library TEST does not exist.
43 run;
44
I'd like to skip the next datastep if the TEST library is not found, or perhaps use a macro variable for the phrase "successfully", depending on the return code of the libname statement (above). Is there a return code to test ?
data _null_;
file outbox
cc="bossman.last@compny.com"
subject="Report process completed - &sysuserid " ;
put " ";
put "&sysdate The Test/Report process successfully completed by: &sysuserid ";
put " " ;
run;
To skip a step need a macro programing:
%macro chklib(path);
%if %sysfunc(fileexist(&path)) %then %do;
libname TEST "&path";
data _null_;
file outbox
cc="bossman.last@compny.com"
subject="Report process completed - &sysuserid " ;
put " ";
put "&sysdate The Test/Report process successfully completed by: &sysuserid ";
put " " ;
run;
%end;
%mend chklib;
%chklib(/sas/sasmain/mydept/Test Report);
You can use the LIBREF function in a macro variable as in
%let abc=%sysfunc(libref(test));
If macro variable &ABC is 0 then the library exists, otherwise it does not exist.
You can then create a branch around this data step inside a macro, or if you have SAS V.94M5 or later, you can use it in a simple %IF - %THEN block, even outside a macro.
To skip a step need a macro programing:
%macro chklib(path);
%if %sysfunc(fileexist(&path)) %then %do;
libname TEST "&path";
data _null_;
file outbox
cc="bossman.last@compny.com"
subject="Report process completed - &sysuserid " ;
put " ";
put "&sysdate The Test/Report process successfully completed by: &sysuserid ";
put " " ;
run;
%end;
%mend chklib;
%chklib(/sas/sasmain/mydept/Test Report);
With all due respect, @Shmuel , I think your very close but off by a teensy little detail. The question was if the libref exists, not whether or not the folder exists. The folder can exist but libref not assigned, and I would think the original poster would want that to fail, at least as far as I understand the question. So the LIBREF function is needed, not FILEEXIST.
@PaigeMiller , I could have a misunderstanding of the query.
@GailVanCourt asked for
prior code is OK. 42 libname TEST '/sas/sasmain/mydept/Test Report'; NOTE: Library TEST does not exist. 43 run; 44 I'd like to skip the next datastep if the TEST library is not found,
I can't regenerate such NOTE but could think of two different issues:
1) path does not exist
2) libref was not assigned
In case 1 I would expect an ERROR message, not a NOTE.
So my answer focused on the requirement - how to check and skip a datastep.
@PaigeMiller - can you explain in what situation, assignment of a libref creates the NOTE: Library <name> does not exist, as shown above?
Finally I have regenerated the issue as shown in next log:
73 libname test "/folders/myfolders/none";
NOTE: Library TEST does not exist.
74
75 %let abc=%sysfunc(libref(test));
76 %put ABC=&abc;
ABC=-70008
77
78 %macro chklib(path);
79 %if not %sysfunc(fileexist(&path))
80 %then %put NOT EXIST;
81 %else %put PATH EXISTS;
82 %mend;
83 %chklib(/folders/myfolders/flat);
PATH EXISTS
84 %chklib(/folders/myfolders/none);
NOT EXIST
Here is some code that illustrates a few things about the LIBNAME statement, the LIBREF function, and how SAS keeps certain things in the background:
Before the code is run, a subdirectory xxx exists in /folders/myfolders, but not a subdirectory yyy.
libname test1 '/folders/myfolders/xxx';
libname test2 '/folders/myfolders/yyy';
%macro chklib(lr);
%if not %sysfunc(libref(&lr))
%then %put EXIST;
%else %put not EXISTS;
%mend;
%chklib(test1)
%chklib(test2)
%put %sysfunc(dcreate(yyy,/folders/myfolders));
%chklib(test2);
Log:
73 libname test1 '/folders/myfolders/xxx'; NOTE: Libref TEST1 was successfully assigned as follows: Engine: V9 Physical Name: /folders/myfolders/xxx 74 libname test2 '/folders/myfolders/yyy'; NOTE: Bibliothek TEST2 existiert nicht. 75 76 %macro chklib(lr); 77 %if not %sysfunc(libref(&lr)) 78 %then %put EXIST; 79 %else %put not EXISTS; 80 %mend; 81 82 %chklib(test1) EXIST 83 %chklib(test2) not EXISTS 84 85 %put %sysfunc(dcreate(yyy,/folders/myfolders)); /folders/myfolders/yyy 86 87 %chklib(test2); EXIST
So, although the library can't be assigned correctly (which is also correctly reported by the LIBREF function, which returns a 0 if the libref exists, non-zero otherwise), the definition attempt is kept, and when the physical path is created, the LIBREF function reports the libref as defined, although no new LIBNAME statement was executed.
The result of the LIBREF() function is tri-level, not binary.
0 means libref was defined and file/directory was found.
negative means libref was defined but file/directory was not found.
positive means libref does not exist.
72 libname test1 'c:\downloads\'; NOTE: Libref TEST1 was successfully assigned as follows: Engine: V9 Physical Name: c:\downloads 73 libname test2 'c:\no such directory\'; NOTE: Library TEST2 does not exist. 74 75 %put TEST1=%sysfunc(libref(TEST1)); TEST1=0 76 %put TEST2=%sysfunc(libref(TEST2)); TEST2=-70008 77 %put TEST3=%sysfunc(libref(TEST3)); TEST3=70006
Thank you again. I was wondering about the ABC=-70008, in Shmuels' earlier post.
Keeping it (as) Simple (as possible)
Thank You. I was pleasantly surprised by all the responses.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.