BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

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 ?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
BillSawyer
SAS Employee

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

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
alepage
Barite | Level 11
libname DATA "&path1./data";
libname TEMP "&path1./temp";
Ksharp
Super User
I think you are using thest two libname statements in many times in your code.
You should clear them out before reassigning them. Like:

LIBNAME DATA "&path2./&subfolder./data";
LIBNAME BROK spde "&path2./&subfolder./temp/master";
.....other code......
LIBNAME _ALL_ CLEAR;
.......other code.......
LIBNAME DATA "&path1./data";
LIBNAME BROK spde "&path1./master";
.....other code......
LIBNAME _ALL_ CLEAR;


Check your code carefully and inserting "LIBNAME _ALL_ CLEAR;" at right place;
alepage
Barite | Level 11

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

PaigeMiller
Diamond | Level 26

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;

  

--
Paige Miller
Tom
Super User Tom
Super User

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

BillSawyer
SAS Employee

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

sas-innovate-white.png

Missed SAS Innovate in Orlando?

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.

 

Register now

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 785 views
  • 1 like
  • 5 in conversation