这是我正在运行的代码,我不确定为什么会出现该错误。
options symbolgen mlogic;
libname lib11 '/home/userid';
%macro SFTPLoop(ds);
%global numobs;
%let dsid = %sysfunc(open(&ds));
%if &dsid %then %do;
%let NumObs= %sysfunc(AttrN(&dsid,NObs));
%If &NumObs>0 %THEN %DO;
%do %while (%sysfunc(fetch(&dsid)) = 0);
%end;
%end;
%else %put ERROR:Data set &dset has 0 rows in it.;
%let rc = %sysfunc(close(&dsid));
%end;
%else %put ERROR:open for data set &dset failed - %sysfunc(sysmsg()).;
%mend SFTPLoop;
%SFTPLoop(lib1.data);
16 libname lib1 '/home/userid';
错误:无法清除或重新分配库 LIB1,因为它仍在使用中。 错误:LIBNAME 语句错误。
最佳答案
尝试在新 session 中运行它。还要确保如果您在查看器中打开它,则将其关闭。确保没有其他用户或进程正在使用它。
有时代码会在某个时刻出错并阻止 close()
语句运行。发生这种情况时,需要手动运行close()
。有时在开发时,清除已打开的任何文件句柄很方便。
您可以使用如下宏来执行此操作:
%macro close_all_dsid;
%local i rc;
%do i=1 %to 1000;
%let rc=%sysfunc(close(&i));
%end;
%mend;
%close_all_dsid;
编辑:我还注意到您在代码中遗漏了“内容”。如果您在 open()
和 close()
语句之间调用一些其他宏,则可能会覆盖您的 dsid
值。没有将您的宏变量声明为本地变量。我建议您至少声明以下内容:
%local dsid rc;
另一种解释是,您可能在某个时刻运行了多个 open()
语句,而没有每次都运行相应的 close()
语句。如果您不小心连续运行了 2 个 open()
语句,则第一次出现时,dsid
的值将被赋值为 1。第二次它将被赋值值为 2。如果您随后运行 close()
,“2”将被关闭,但“1”仍将打开。无论您运行 open()
和 close()
多少次,“1”都永远不会关闭,除非您手动运行 close(1)
这实际上就是我上面的代码片段正在做的事情。
关于sas - 出现错误 :Unable to clear or re-assign the library DATA1 because it is still in use in SAS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31036350/