DATA Step, Macro, Functions and more

how to call macro under a loop

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

how to call macro under a loop

Hi All,

 

I have a SAS macro, which only has one parameter related to date, and I tried to call it several times per its date. In order  not to copy and paste several times, I tried to write another macro to call it.However, I got an error and I could not run through it. Below is a little more details.

 

SAS macro whose parameter is date like this:

%macro actdat(date1);

;;;;;;;

%mend;

 

Simple way to call that macro is like this:

%actdat(0107);

%actdat(0207);

;;;;

 

What I wrote another macro to call it like this:

%macro alldate;

array dtloop {2} $ ('0107' '0207');

%do i=1 %to 2;

%let a=dtloop{i};

%actdat(symget(&a.));

%end;

%mend;

 

%alldate;

 

 

Any idea what's wrong with this? And how to correct this?

 

Thank you.


Accepted Solutions
Solution
‎05-26-2017 10:06 AM
Super User
Super User
Posts: 7,413

Re: how to call macro under a loop

It seems to me your just layering macro on top of macro for no purpose here.  Nothing of the information you have provided would indicate something not possible in base SAS.  There are many ways to approach a problem, for instance rather than dates as parameters you could pass in a dataset with all the dates to be worked on, this effectively means one call.  Or you can do it in a datastep, no macro needed at all, or you could transpose/normalise your data etc.  As you can see many methods.  99.9% of the time you should either re-modelling the data, or figuring out use of Base SAS functions will result in simpler, easier to maintain code.

View solution in original post


All Replies
PROC Star
Posts: 7,364

Re: how to call macro under a loop

You could try (not tested):

%macro alldate;

  data _null_;
    array dtloop {2} $ ('0107' '0207');
    length forexec $80;
    do i=1 to 2;
       forexec=catt('%actdat(',dtloop(i),')');
       call execute(forexec);
    end;
  run;
%mend;
 
%alldate;

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 16

Re: how to call macro under a loop

Hi ART297, 

 

it works, thanks so much.

Super User
Posts: 17,905

Re: how to call macro under a loop

Don't use a macro to call another macro. Use Call Execute. 

 

@art297 's answer is correct, except I'd strip the macro entirely.

 

  data _null_;
    array dtloop {2} $ ('0107' '0207');
    length forexec $80;
    do i=1 to 2;
       forexec=catt('%actdat(',dtloop(i),')');
       call execute(forexec);
    end;
run;
Trusted Advisor
Posts: 1,399

Re: how to call macro under a loop

You may also try:

%macro alldates(dates);
   %let n = %sysfunc(countw(&dates));
    %do i=1 %to &n;
          %let a = %scan(&dates , &i);
           %actdat(&a.);
    %end;
%mend;
 
%alldates(0107 0207);
Solution
‎05-26-2017 10:06 AM
Super User
Super User
Posts: 7,413

Re: how to call macro under a loop

It seems to me your just layering macro on top of macro for no purpose here.  Nothing of the information you have provided would indicate something not possible in base SAS.  There are many ways to approach a problem, for instance rather than dates as parameters you could pass in a dataset with all the dates to be worked on, this effectively means one call.  Or you can do it in a datastep, no macro needed at all, or you could transpose/normalise your data etc.  As you can see many methods.  99.9% of the time you should either re-modelling the data, or figuring out use of Base SAS functions will result in simpler, easier to maintain code.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 177 views
  • 1 like
  • 5 in conversation