尝试遍历多个数据集的 SAS 错误

SAS Error trying to loop through multiple datasets

我正在尝试 运行 一些代码,这些代码有望连接数月或数年的数据。我试图找出一个字段何时填充了一个值。 IE。我的数据集中有字段 XYZ,它在 2016 年 11 月填充了值 A。如果我 运行 我的代码从 1 月到 12 月,我想要一个新字段填充 SAS 遇到非空值的日期在那个领域。

这是我的代码:

    options mprint symbolgen source mlogic merror syntaxcheck ;

%macro append_monthly(iStart_date=, iEnd_date=);

  %local tmp_date i;
  %let tmp_date = %sysfunc(intnx(month,&iStart_date,0,beginning)) ;

  %do %while (&tmp_date le &iEnd_date);

    %let i = %sysfunc(sum(&tmp_date),yymmn4.);
    %put &i.;

    %let tmp_date = %sysfunc(intnx(month,&tmp_date,1,beginning)) ;

    libname note "my.qualifiers.fords.note&i." disp=shr;

data new ;
set note.file ;

%if ln_note_crbur_date_delinq ne '' %then spc_cmt_date = &i.;

run;

  %end;


%mend;
%append_monthly(iStart_date=%sysfunc(mdy(5,1,2016)),  iEnd_date=%sysfunc(mdy(10,1,2016)) );

LIBNAME _ALL_ CLEAR;

这是来自错误日志的示例:

SYMBOLGEN:  Macro variable TMP_DATE resolves to 20606
SYMBOLGEN:  Macro variable IEND_DATE resolves to 20728
MLOGIC(APPEND_MONTHLY):  %DO %WHILE(&tmp_date le &iEnd_date) condition is TRUE; loop will iterate again.
MLOGIC(APPEND_MONTHLY):  %LET (variable name is I)
SYMBOLGEN:  Macro variable TMP_DATE resolves to 20606
MLOGIC(APPEND_MONTHLY):  %PUT &i.
SYMBOLGEN:  Macro variable I resolves to 1606
1606
MLOGIC(APPEND_MONTHLY):  %LET (variable name is TMP_DATE)
SYMBOLGEN:  Macro variable TMP_DATE resolves to 20606
MPRINT(APPEND_MONTHLY):   spc_cmt_date = 1605 run;
SYMBOLGEN:  Macro variable I resolves to 1606
MPRINT(APPEND_MONTHLY):   libname note "my.qualifiers.fords.note1606" disp=shr;
ERROR: Unable to clear or re-assign the library NOTE because it is still in use.
ERROR: Error in the LIBNAME statement.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.NEW may be incomplete.  When this step was stopped there were 0 observations and 622 variables.
WARNING: Data set WORK.NEW was not replaced because this step was stopped.
NOTE: The DATA statement used 0.01 CPU seconds and 49483K.

NOTE: The address space has used a maximum of 4292K below the line and 240388K above the line.

我不明白为什么这不起作用。也许这可以使用 Proc append 来工作。

基本上,我只希望我的输出带有一个字段,该字段 returns 是 YYMM 形式的日期,表示字段 ln_note_crbur_date_delinq 为非空白时的日期。

如有任何帮助,我们将不胜感激

我猜你的错误原因是在下一个 libname 语句尝试重新分配之前,你的源文件上的句柄没有被清除。

一个简单的解决方法是每次都使用不同的别名 (libref),如下所示:

libname note&i "my.qualifiers.fords.note&i." disp=shr;

然后像这样调整你的数据步骤:

data new ;
  set note&i..file ;

下一部分似乎是宏逻辑和数据步骤之间的混淆。只需删除 % 符号,如下所示:

if ln_note_crbur_date_delinq ne '' then spc_cmt_date = &i.;

最后在%end前加一个proc append如下:

proc append base=work.final data=new; run;

如果work.final不存在,将以与new相同的格式创建。

编辑:

根据评论中的讨论,这里是修改后的方法:

%macro append_monthly(iStart_date=, iEnd_date=);
  %local tmp_date i set_statement;
  %let tmp_date = %sysfunc(intnx(month,&iStart_date,0,beginning)) ;
  %do %while (&tmp_date le &iEnd_date);
    %let i = %sysfunc(sum(&tmp_date),yymmn4.);
    %let tmp_date = %sysfunc(intnx(month,&tmp_date,1,beginning)) ;
    %let set_statement=&set_statement &i..file;
    libname note&i "my.qualifiers.fords.note&i." disp=shr;
  %end;
  data new ;
    set &set_statement;
    if ln_note_crbur_date_delinq ne '' then spc_cmt_date = &i.;
  run;
%mend;
%append_monthly(iStart_date=%sysfunc(mdy(5,1,2016)),  iEnd_date=%sysfunc(mdy(10,1,2016)) );

LIBNAME _ALL_ CLEAR;