## can we call a macro in a do loop?

Solved
Occasional Contributor
Posts: 12

# can we call a macro in a do loop?

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: 13,583

## 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.

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

## 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

hey thanks.

Posts: 3,058

## 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.

--
Paige Miller
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 and locked.