DATA Step, Macro, Functions and more

can we call a macro in a do loop?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

can we call a macro in a do loop?

please help me with this issue and suggest me possible alternatives

can we call a macro in a do loop?

fo ex-

data _null_;

do i=1 to 4

%monthly(&month.);

end;

run;


Accepted Solutions
Solution
‎12-06-2013 10:49 AM
Super User
Posts: 10,516

Re: can we call a macro in a do loop?

It depends on what the macro does. Since your example doesn't change &month, why would you call it 4 times? Also your Do statement needs a ; at the end.

If you want to use a value in a dataset with a macro call you should look up CALL EXECUTE.

If you want to use a macro loop you will need to call it within an macro as the %do %end constructs will not work in open code (i.e. non-macro code ). Something like:

%macro dummy;

%do month = 1 %to 4;

     %monthly(&month);

%end;

%mend;

%dummy;

Remember that the macro language basically just generates text that is interpretted as SAS code.

View solution in original post


All Replies
Solution
‎12-06-2013 10:49 AM
Super User
Posts: 10,516

Re: can we call a macro in a do loop?

It depends on what the macro does. Since your example doesn't change &month, why would you call it 4 times? Also your Do statement needs a ; at the end.

If you want to use a value in a dataset with a macro call you should look up CALL EXECUTE.

If you want to use a macro loop you will need to call it within an macro as the %do %end constructs will not work in open code (i.e. non-macro code ). Something like:

%macro dummy;

%do month = 1 %to 4;

     %monthly(&month);

%end;

%mend;

%dummy;

Remember that the macro language basically just generates text that is interpretted as SAS code.

Occasional Contributor
Posts: 12

Re: can we call a macro in a do loop?

hey thanks.

Trusted Advisor
Posts: 1,621

Re: can we call a macro in a do loop?

Yes of course you can place a macro in a do-loop inside a data step, if you really want to. First make sure you pay attention to 's advice regarding missing semi-colon

If the macro contains only data step code (and not a new DATA step or a call to a PROC) then it should work.

This very trivial macro ought to do something, assuming the argument month is integer, you won't get an error -- not saying this is a good thing to put in your macro, just that it is an example of a macro that works inside of a do-loop

%macro monthly(month);

calc=&month+i;

%mend;

Of course, as stated by , you don't really need to do this with a macro (and in fact, you don't even need CALL EXECUTE for this trivial case), and so you really ought to be 100% sure you need a macro inside the do loop; many times data step code can do the same thing.

Occasional Contributor
Posts: 12

Re: can we call a macro in a do loop?

thanku

going forward i will keep in view syntactical errors do not happen.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 263 views
  • 3 likes
  • 3 in conversation