Hello,
Some time we use an include statement like the one below to declare a series of libnames like below:
%INCLUDE "/&path./&subfolder./include/defined_lib_file.sas";
and the defined_lib_file.sas contains libname statements like:
LIBNAME DATA "&path2./&subfolder./data";
LIBNAME BROK spde "&path2./&subfolder./temp/master";
and after executing the %include statement, we see this following error into the log file:
ERROR: Unable to clear or re-assign the library DATA because it is still in use.
ERROR: Error in the LIBNAME statement.
how can we solve this issue ?
You could use the libref function to check whether the library DATA is assigned or not then program around it to avoid the problem.
libname data "c:\users\sasbqs\users\bill";
*libname data clear;
/*libref function returns 0 if the libref DATA is assigned, and returns a non-zero if not assigned*/
data test;
keep mylibref;
mylibref = libref("data");
run;
Regards,
Bill
Perhaps you have opened a SAS data set in the DATA library, and it is still open. If so, just close the data set(s) in DATA that are open.
If that's not it, show us the %INCLUDE code
libname DATA "&path1./data";
libname TEMP "&path1./temp";
Do we have a SAS function that we can use to check if the libname exist and if so we can clear it before re-assign it
You don't need to know what LIBNAMEs exist, the command from @Ksharp will clear whichever ones exist. Of course, I consider that command dangerous in the sense that it will clear LIBNAMEs that you do NOT want to clear.
But even that command fails if, as I said earlier, you have an open data set in one of the libraries, then the command will not clear the LIBNAME. So do you find yourself in this situation? Can we see the contents of the %INCLUDE file as I requested?
You can query to find out what LIBNAMEs exist
proc sql noprint;
select distinct libname into :libnames separated by ' ' from sashelp.vlibnam;
quit;
%put &=libnames;
@alepage wrote:
Do we have a SAS function that we can use to check if the libname exist and if so we can clear it before re-assign it
You can check the dictionary "table" DICTIONARY.LIBNAMES, which you can also access via the view SASHELP.VLIBNAM.
You can use the LIBNAME() function with only the name. It will return zero when the libname is assigned.
There is no need to clear the libref before reassigning it, other than to protect yourself from coding errors that might prevent the new assignment from running an hence leave the libref pointing to the old location.
One thing to check to see why you could not reassign the libref is to make sure it is not being used by some SAS option, like FMTSEARCH, that is keeping a file open.
You could use the libref function to check whether the library DATA is assigned or not then program around it to avoid the problem.
libname data "c:\users\sasbqs\users\bill";
*libname data clear;
/*libref function returns 0 if the libref DATA is assigned, and returns a non-zero if not assigned*/
data test;
keep mylibref;
mylibref = libref("data");
run;
Regards,
Bill
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.