DATA Step, Macro, Functions and more

Using INTCK function in the loop

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

Using INTCK function in the loop

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


Accepted Solutions
Solution
‎05-22-2014 04:21 PM
Trusted Advisor
Posts: 1,607

Re: Using INTCK function in the loop

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;

View solution in original post


All Replies
Solution
‎05-22-2014 04:21 PM
Trusted Advisor
Posts: 1,607

Re: Using INTCK function in the loop

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;

Contributor
Posts: 30

Re: Using INTCK function in the loop

Hi, thanks for your quick response!

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

Hatsumi

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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