%let date=%sysfunc(intnx(month,&start,&i,b),date9.);
%put &date;
%end;
%mend date_loop;
%date_loop(01jul2015,01feb2016)
Program Description
Begin the macro definition with two parameters.
%macro date_loop(start,end);
Use the %LET statement to change the value of the macro variable named &START.
The INPUTN function is used so that the ANYDTDTE informat can be used on the
value that is passed to &START. Using the ANYDTDTE informat causes the value of
&START to be a SAS formatted date.
%let start=%sysfunc(inputn(&start,anydtdte9.));
Use the %LET statement to change the value of the macro variable named &END. The
INPUTN function is used so that the ANYDTDTE informat can be used on the value
that is passed to &END. Using the ANYDTDTE informat causes the value of &END to
be a SAS formatted date.
%let end=%sysfunc(inputn(&end,anydtdte9.));
Use the %LET statement to create a macro variable named &DIF. The INTCK function
is used to return the number of months between &START and &END. The MONTH
function is used as the first argument to retrieve the month interval. When using this
function within the macro facility, quotation marks are not used around MONTH, like
you would in the DATA step.
%let dif=%sysfunc(intck(month,&start,&end));
The %DO statement is used to loop through the number of months (&DIF) between
&START and &END.
%do i=0 %to &dif;
Use the %LET statement to create a macro variable named &DATE. The INTNX
function is used to increment the &START date by MONTH. The fourth argument, B,
specifies the alignment. The
B argument specifies that the returned date or datetime
value is aligned to the beginning of the interval. The second argument to %SYSFUNC is
the format DATE9, which is applied to the value that is returned from the function
INTNX. The %PUT statement is used to write the date value to the log.
%let date=%sysfunc(intnx(month,&start,&i,b),date9.);
%put &date;
End the %DO block.
%end;
End the macro.
%mend date_loop;
Invoke the macro by passing the starting date and the ending date parameters.
%date_loop(01jul2015,01feb2016)
468 Appendix 5 • SAS Macro Examples