简体   繁体   English

SAS循环的开始日期和结束日期

[英]Sas loop for start date and end date

I have a code here, where I take a start date and end date from the user and execute a macro for those 2 dates. 我在这里有一个代码,在这里我从用户那里获取开始日期和结束日期,并为那两个日期执行一个宏。

%let mon_last=31MAR2019;
%let mon_first=01JAN2019;
%macro call();
data _null_;
array datelist {2} "&mon_first"d "&mon_last"d;
%do i=1 %to 2;
    %let yrmon1=%sysfunc(inputn(datelist{i},mmddyy10.), date9.));
    %let yrmon=%sysfunc(putn(&yrmon1,date9.),yymmn6.);
    %let yr=%sysfunc(year(&yrmon1),4);
    %let mon=%sysfunc(month(&yrmon1),z2);
    %datapull(&yrmon., &yr., &mon.);
%end;
%mend;
%call();

But I end up getting the following error whichever way I try: 但是无论尝试哪种方式,我最终都会遇到以下错误:

WARNING: Argument 1 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set 
      to a missing value.
ERROR: The function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function has too few arguments.

The macro processor considers everything as a string. 宏处理器将所有内容视为字符串。 You cannot convert the string datelist{i} into a date value. 您无法将字符串datelist{i}转换为日期值。

Looks like you want to have a macro that can take as input a list of strings that are in a format that can be converted into date values and use those to call another macro. 看起来您想拥有一个宏,该宏可以将一系列可以转换为日期值的格式的字符串作为输入,并使用它们来调用另一个宏。

%macro call(date_list);
%local i yrmon yr mon;
%do i=1 %to %sysfunc(countw(&date_list));
    %let yrmon=%sysfunc(inputn(%scan(&date_list,&i),date11.),yymmn6.);
    %let yr=%substr(&yrmon,1,4);
    %let mon=%substr(&yrmon,5);
    %datapull(&yrmon., &yr., &mon.);
%end;
%mend;
%call(31MAR2019 01JAN2019);

If instead you wanted to process every month from start to end then you want a different macro with different inputs. 相反,如果您想从头到尾每个月进行处理,那么您想要一个具有不同输入的不同宏。 In that case you just need two inputs that each can only have one value. 在这种情况下,您只需要两个输入,每个输入只能有一个值。

This time let's code it so that the burden of supplying valid date values falls onto the caller of the macro instead of accepting character strings that need to be converted into dates. 这次让我们对其进行编码,以便提供有效日期值的负担落在宏的调用方上,而不是接受需要转换为日期的字符串。

%macro call(start,end);
%local i yrmon yr mon;
%do i=0 %to %sysfunc(intck(month,&start,&end));
    %let yrmon=%sysfunc(intnx(month,&start,&i),yymmn6.);
    %let yr=%substr(&yrmon,1,4);
    %let mon=%substr(&yrmon,5);
    %datapull(&yrmon., &yr., &mon.);
%end;
%mend;
%call("01JAN2019"d,"31MAR2019"d);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM