Getting ERROR :Unable to clear or re-assign the library DATA1 because it is still in use in SAS

Getting ERROR :Unable to clear or re-assign the library DATA1 because it is still in use in SAS

这是我 运行 的代码,但我不确定为什么会出现该错误。

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 库名 lib1 '/home/userid';

错误:无法清除或重新分配库 LIB1,因为它仍在使用中。 错误:LIBNAME 语句错误。

尝试 运行在新的会话中使用它。还要确保如果您在查看器中打开它然后将其关闭。确保没有其他用户或进程正在使用它。

有时代码会在某些时候出错并阻止 运行ning 中的 close() 语句。发生这种情况时,有必要手动 运行 close()。在开发过程中,有时只清除您打开的任何文件句柄会很方便。

您可以使用如下所示的宏来执行此操作:

%macro close_all_dsid;
  %local i rc;
  %do i=1 %to 1000;
    %let rc=%sysfunc(close(&i));
  %end;
%mend;
%close_all_dsid;

编辑: 我还注意到您在代码中遗漏了 'meat'。如果您在 open()close() 语句之间调用一些其他宏,您可能会覆盖 dsid 的值,因为您没有将宏变量声明为局部变量。我建议您至少声明以下内容:

%local dsid rc;

另一种解释是,有可能在某些时候您 运行 多个 open() 语句而没有每次都 运行 宁相应的 close()。如果您不小心连续 运行 2 open() 语句,第一次出现时 dsid 的值将被赋值 1。第二次它会被赋值 2 . 如果你然后 运行 a close() '2' 将关闭,但 '1' 仍将打开。无论您然后 运行 一个 open()close() 多少次,“1”都不会关闭,除非您手动 运行 close(1) 这实际上是我的上面的代码片段正在做。

我今天在尝试重新分配库时遇到了同样的错误。回顾我的日志,我看到了以下内容(在 proc 追加之后):

NOTE: BASE file MYLIB.MYDS.DATA set to record level control because there is at 
least one other open of this file. The append process may take longer to complete.

运行 Robert 的宏 (close_all_dsid) 修复了它。 OS 在存储过程服务器会话中是 Windows 2008r2 运行 SAS 9.2。追加成功。如果/当我想通了就会回来!

您可以终止并重新启动会话,然后它将正常工作。