SAS尝试循环遍历多个数据集时出错

时间:2017-01-04 15:17:58

标签: date sas syntax-error

我试图运行一些代码,这些代码有望连接数月或数年的数据。我试图找出一个字段填充值的时间。即在我的数据集中有字段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。

基本上,我只希望我的输出带有一个字段,当字段ln_note_crbur_date_delinq为非空时,该字段以YYMM的形式返回日期。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

我猜你的错误原因是在下一个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.;

最后,在proc append之前添加%end,如下所示:

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;