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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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