是否有人知道如何使用sas宏进程创建宏变量以获取特定的星期几?
我希望每次运行sas宏时都能获得上周的星期三日期。
例如:
今天:2011年10月18日星期二 - >如果我今天运行宏,我想得到: “2011年10月12日星期三”
如果我在周一运行宏,我仍然希望得到“2011年10月12日星期三”
谢谢,
答案 0 :(得分:4)
您可以使用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
现在包含最近一个星期三的日期。
答案 1 :(得分:3)
如果要调用宏来返回最近一个星期三的日期。 (另外,如果您希望将日期存储在宏变量中,请删除“& 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));
答案 2 :(得分:2)
我将您的问题解释为“如何让SAS返回上周上周三的日期,而不管当周的哪一天?”
如果这是你想要的结果,则需要两次intnx
次呼叫:一次呼叫回到上周日(lastwk=intnx('week', today, -1);
),然后再拨打第二次呼叫,继续前进到上周三(lastwed=intnx('week.4', lastwk , 1);
)。
更简单的方法可能是回到上周的开头,只需在结果中添加3,但该代码看起来更像是黑客而非故意偏移。
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