有谁知道如何使用 sas 宏过程创建宏变量以获得特定的星期几?
每次运行 sas 宏时,我都想获得前一周的星期三日期。
例如 :
今天:2011 年 10 月 18 日,星期二--> 如果我今天运行宏,我想得到:“2011 年 10 月 12 日,星期三”
如果我在 Monday 运行宏,我仍然想获得“Wednesday Oct 12, 2011”
谢谢,
您可以使用intnx
. 这是一个示例(不是宏,但它为您提供了想法):
首先,生成一些数据:
data dates;
do d=0 to 13;
date = '15oct2011'd + d;
output;
end;
format date date8.;
run;
这将为您提供最近的星期三。请注意,“week.4”是从星期三开始的几周的说明。
data dates;
set dates;
wed=intnx('week.4',date,0,'beginning');
format wed date8.;
run;
该变量wed
现在包含最近的星期三的日期。
如果你想调用一个宏来返回最近的星期三的日期。(此外,如果您希望将日期仅存储在宏变量中...删除“&weekday;”语句。)
%Macro Get_Weekday(date);
%Let weekday=%sysfunc(putn(
%sysfunc(intnx(week.4,&date,0,beginning)),weekdate.));
&weekday;
%Mend Get_Weekday;
%Put Today is %sysfunc(putn(%sysfunc(today()),weekdate.))
and the most recent Wednesday is %Get_Weekday(%sysfunc(today()));
%Put If Today was %sysfunc(putn(%eval(%sysfunc(today())-1),weekdate.))
then the most recent Wednesday would be
%Get_Weekday(%eval(%sysfunc(today())-1));
我将您的问题解释为“如何让 SAS 返回上周三的日期,而不管当前是一周中的哪一天?”
如果这是您想要的结果,则intnx
需要两次调用:一次调用返回上周的星期日 ( lastwk=intnx('week', today, -1);
),然后第二次调用向前移动到上周的星期三 ( lastwed=intnx('week.4', lastwk , 1);
)。
一种更简单的方法可能是回到上周的开头并简单地将 3 添加到结果中,但该代码看起来更像是一个 hack,而不是有意的偏移量。
data _null_;
do i = -10 to 10;
today="&SYSDATE9"d + i;
lastwk=intnx('week', today, -1);
lastwed=intnx('week.4', lastwk , 1);
put today weekdate. '-->' lastwed weekdate.-l;
end;
run;
Sunday, October 9, 2011-->Wednesday, October 5, 2011
Monday, October 10, 2011-->Wednesday, October 5, 2011
Tuesday, October 11, 2011-->Wednesday, October 5, 2011
Wednesday, October 12, 2011-->Wednesday, October 5, 2011
Thursday, October 13, 2011-->Wednesday, October 5, 2011
Friday, October 14, 2011-->Wednesday, October 5, 2011
Saturday, October 15, 2011-->Wednesday, October 5, 2011
Sunday, October 16, 2011-->Wednesday, October 12, 2011
Monday, October 17, 2011-->Wednesday, October 12, 2011
Tuesday, October 18, 2011-->Wednesday, October 12, 2011
Wednesday, October 19, 2011-->Wednesday, October 12, 2011
Thursday, October 20, 2011-->Wednesday, October 12, 2011
Friday, October 21, 2011-->Wednesday, October 12, 2011
Saturday, October 22, 2011-->Wednesday, October 12, 2011
Sunday, October 23, 2011-->Wednesday, October 19, 2011
Monday, October 24, 2011-->Wednesday, October 19, 2011
Tuesday, October 25, 2011-->Wednesday, October 19, 2011
Wednesday, October 26, 2011-->Wednesday, October 19, 2011
Thursday, October 27, 2011-->Wednesday, October 19, 2011
Friday, October 28, 2011-->Wednesday, October 19, 2011
Saturday, October 29, 2011-->Wednesday, October 19, 2011