BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hatsumi
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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;

--
Paige Miller
hatsumi
Obsidian | Level 7

Hi, thanks for your quick response!

It worked perfectly and yes, I will review the SAS documentation once again.

Hatsumi

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3513 views
  • 1 like
  • 2 in conversation