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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3263 views
  • 1 like
  • 2 in conversation