DATA Step, Macro, Functions and more

SAS Date loop Monday to Sunday

Reply
Occasional Contributor
Posts: 14

SAS Date loop Monday to Sunday

Hi

 

I would like to know if anyone can help me with a macro loop I am having trouble with

I am trying to run the same macro for the weeks running Monday to Sunday for the past 2 years

/*2015*/

%line(29dec2014,04jan2015);%line(05jan2015,11jan2015);%line(12jan2015,18jan2015);%line(19jan2015,25jan2015);%line(26feb2015,01mar2015);%line(02mar2015,08mar2015);

%line(09mar2015,15mar2015);%line(16mar2015,22mar2015);%line(23mar2015,29mar2015);%line(30mar2015,05apr2015);%line(06apr2015,12apr2015);%line(13apr2015,19apr2015);

 

 

I am repeating the same lines several times but I am sure I can write a weekly macro loop to give the start and end dates so something that looks like

 

%macro new (Start, End);

 

 

.....i=1 to 60

       j=1 to 60

   %line(&i.,&j.)

 

%mend;

 

Thanks

Priya.

Super User
Posts: 6,964

Re: SAS Date loop Monday to Sunday

Call it from a data step:

data _null_;
date1 = '29dec2014'd;
do until (date1 > '13apr2015'd);
  call execute('%line('!!put(date1,date9.)!!','!!put(date1+6,date9.)!!');');
  date1 + 7;
end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 63

Re: SAS Date loop Monday to Sunday

Priya,

That should not be so difficult. I assume you want a macro to be called like %do_lines(01JAN2015,31DEC2016) - the parameters being starting date and ending date. I think this will work:

%macro do_lines(StartDate,EndDate);
  %local Monday Sunday;
  %do Monday=%sysfunc(intnx(WEEK.2,"&StartDate"d,0)) %to %sysfunc(inputn(&EndDate,date9.)) %by 7;
    %let Sunday=%eval(&monday+6);
    %line(%sysfunc(putn(&Monday,DATE9.)),%sysfunc(putn(&Sunday,DATE9.)));
    %end;
%mend;

Regards,

Søren

 

 

Occasional Contributor
Posts: 14

Re: SAS Date loop Monday to Sunday

Thankyou - This worked very well.

Ask a Question
Discussion stats
  • 3 replies
  • 122 views
  • 0 likes
  • 3 in conversation