Hi everyone,
I wrote below macro and it works fine as long as I have the counter n as a hard-coded number like 20.
but I would like this macro to be more flexible and I want to insert INTCK function to automatically count the number of iterations from last month to the end of next year.
So in the 2nd macro, I inserted the INTCK function following the examples in this forum but it produces an error.
My data is monthly and INTCK function works fine itself but not within a loop.
I appreciate if anyone could point out how to fix this problem.
ORIGINAL
%macro lag;
%do n=1 %to 20;
data data_horizon&n;
set data_horizon0;
A_L=lag&n(A);
%end;
run;
%mend;
%lag
FLEXIBLE
%macro lag;
%do n=1 %to %sysfunc(intck('month', mdy(month(today())-1,01,year(today())), mdy(12,01,year(today())+1)));
data data_horizon&n;
set data_horizon0;
A_L=lag&n(A);
%end;
run;
%mend;
%lag
I don't think you put quotes around month inside of %sysfunc
You probably also need to nest %sysfunc calls, as explained in the SAS documentation, which means it probably would be easier to do something like
data _null_;
call symputx('niter',intck('month', mdy(month(today())-1,01,year(today())), mdy(12,01,year(today())+1))));
run;
%do n=1 %to &niter;
I don't think you put quotes around month inside of %sysfunc
You probably also need to nest %sysfunc calls, as explained in the SAS documentation, which means it probably would be easier to do something like
data _null_;
call symputx('niter',intck('month', mdy(month(today())-1,01,year(today())), mdy(12,01,year(today())+1))));
run;
%do n=1 %to &niter;
Hi, thanks for your quick response!
It worked perfectly and yes, I will review the SAS documentation once again.
Hatsumi
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.