2011-10-18 85 views
1

是否有人知道如何通過sas宏程序創建宏變量以獲得特定的星期幾?從前一週通過sas宏獲得特定日期

我想在每次運行sas宏時獲得前一週的星期三日期。

例如:

今天:星期二十月18,2011 - >如果我運行宏今天我想獲得: 「週三2011年10月12日」

,如果我運行週一宏,我仍然希望得到的「週三二○一一年十月一十二日」

感謝,

回答

3

如果你想調用一個宏來返回最近的星期三的日期。 (另外,如果你想只是存儲在一個宏VAR日......去掉「和平日;」語句)

%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)); 
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現在包含最近的星期三的日期。

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 
+1

正如信息,如果週三是一週中所需的一天,你總是可以找到上週三通過了以下內容:lastwed = intnx(「周」 ,今天-1,'m'); – adam

相關問題