Question

Does any one know how to create macro variable with sas macro process to get specific day of week ?

I would like to get Wednesday date of the previous week every time I run the sas macro.

For example :

Today : Tuesday Oct 18,2011--> if I run the macro today I would like to get : "Wednesday Oct 12, 2011"

and If I run the macro on Monday , I still want to get the "Wednesday Oct 12, 2011"

Thanks,

Was it helpful?

Solution

If you want to call a macro to return the date of the most recent Wednesday. (Also, if you want the date just stored in a macro var...remove the "&weekday;" statement.)

%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));

OTHER TIPS

You can accomplish this with intnx. Here's an example (not a macro, but it gives you the idea):

First, generate some data:

data dates;
 do d=0 to 13;
  date = '15oct2011'd + d;
  output;
 end;
 format date date8.;
run;

This will give you the most-recent Wednesday. Note that 'week.4' is the instruction for weeks starting on Wednesday.

data dates;
 set dates;
 wed=intnx('week.4',date,0,'beginning');
 format wed date8.;
run;

The variable wed now contains the date of the most recent Wednesday.

i interpreted your question as "How to have SAS return the date for the Wednesday of last week regardless of the current day of week?"

if that's your desired outcome, then two intnx calls are needed: one call to move back to Sunday of last week (lastwk=intnx('week', today, -1);) and then a second call to move ahead to Wednesday of last week (lastwed=intnx('week.4', lastwk , 1);).

a simpler way might be to go back to the beginning of last week and simply add 3 to the result, but that code appears more like a hack than an intentional offset.

code

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;

log

      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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top