I am checking if a Library is assigned or not as per the code (at the end of this message)
As you can see, there are no statements assigning any libraries, but still when I check if the library was assigned, it shows assigned.
In the job I haven't assigned any of the libraries (RAMSN, CDC3, CDC4, CDC5, PUB, CLBP2), I deliberately removed the assignment statements to see if the logic works or not, it's not working. Not sure what's wrong.
Please check the log file attached for the same - "Libcheck-log.txt". I removed my ID details and replaced it with xxxx in the log, rest all is intact as it was generated.
SAS JOB -
%include '/home/xxxx/temp/cred.sas';
%include '/home/xxxx/temp/ldap.sas';
Libname mylib '/home/xxxx/temp';
%Global RAMSNEXISTS CDC3EXISTS CDC4EXISTS CDC5EXISTS PUBEXISTS CLBP2EXISTS;
Options source source2 notes symbolgen mlogic mprint ;
%Macro RamsCheck(RAMSN);
%Let RAMSNEXISTS=0;
%IF %SYSFUNC(LIBREF(RAMSN))=0 %THEN %Let RAMSNEXISTS=1;
%Mend;
%Macro CDC3Check(CDC3);
%Let CDC3EXISTS=0;
%IF %SYSFUNC(LIBREF(CDC3))=0 %THEN %Let CDC3EXISTS=1;
%Mend;
%Macro CDC4Check(CDC4);
%Let CDC4EXISTS=0;
%IF %SYSFUNC(LIBREF(CDC4))=0 %THEN %Let CDC4EXISTS=1;
%Mend;
%Macro CDC5Check(CDC5);
%Let CDC5EXISTS=0;
%IF %SYSFUNC(LIBREF(CDC5))=0 %THEN %Let CDC5EXISTS=1;
%Mend;
%Macro PUBCheck(PUB);
%Let PUBEXISTS=0;
%IF %SYSFUNC(LIBREF(PUB))=0 %THEN %Let PUBEXISTS=1;
%Mend;
%Macro CLBP2Check(CLBP2);
%Let CLBP2EXISTS=0;
%IF %SYSFUNC(LIBREF(CLBP2))=0 %THEN %Let CLBP2EXISTS=1;
%Mend;
LIBNAME RAMSN CLEAR;
%RamsCheck(RAMSN);
LIBNAME CDC3 CLEAR;
%CDC3Check(CDC3);
LIBNAME CDC4 CLEAR;
%CDC4Check(CDC4);
LIBNAME CDC5 CLEAR;
%CDC5Check(CDC5);
LIBNAME PUB CLEAR;
%PUBCheck(PUB);
LIBNAME CLBP2 CLEAR;
%CLBP2Check(CLBP2);
%Put &RAMSNEXISTS.;
%Put VALUE IS %SYSFUNC(LIBREF(RAMSN));
%Put &CDC3EXISTS.;
%Put VALUE IS %SYSFUNC(LIBREF(CDC3));
%Put &CDC4EXISTS.;
%Put VALUE IS %SYSFUNC(LIBREF(CDC4));
%Put &CDC5EXISTS.;
%Put VALUE IS %SYSFUNC(LIBREF(CDC5));
%Put &PUBEXISTS.;
%Put VALUE IS %SYSFUNC(LIBREF(PUB));
%Put &CLBP2EXISTS.;
%Put VALUE IS %SYSFUNC(LIBREF(CLBP2));
Query DICTIONARY.LIBNAMES (SQL procedure) or SASHELP.VLIBNAM (data step, other procedures) instead when you need to check for a number of libraries.
You create macro variables inside your macros without declaring them global (%GLOBAL statement), so they are defined in the local symbol table and vanish as soon as the macro stops executing.
Since in this macro
%macro RamsCheck(RAMSN);
%let RAMSNEXISTS=0;
%if %SYSFUNC(LIBREF(RAMSN))=0 %then %Let RAMSNEXISTS=1;
%mend;
you haven't et RAMSNEXISTS to either LOCAL or GLOBAL it will default to local if there is not already a macro variable with that name. So to use the macro make sure to create the target macro variable before running the macro.
LIBNAME RAMSN CLEAR;
%let RAMSNEXISTS=Before macro call;
%RamsCheck(RAMSN);
%put &=RAMSNEXISTS;
It might be simpler to create a single macro to do the test. Similar to this macro for using the FILEREF() function. https://github.com/sasutils/macros/blob/master/fileref.sas
Then your logic can instead be:
%let RAMSNEXISTS=%libref(RAMSN);
%let CDC3EXISTS=%libref(CDC3);
...
What result do you want when the libref is defined, but it is pointing at something that does not exist?
7 libname xxx 'c:\downloads';
NOTE: Libref XXX was successfully assigned as follows:
Engine: V9
Physical Name: c:\downloads
8 %put %sysfunc(libref(xxx));
0
9 libname xxx clear;
NOTE: Libref XXX has been deassigned.
10 %put %sysfunc(libref(xxx));
70006
11 libname xxx 'c:\no such\directory';
NOTE: Library XXX does not exist.
12 %put %sysfunc(libref(xxx));
-70008
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.