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;
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.
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.
hey thanks.
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.
thanku
going forward i will keep in view syntactical errors do not happen.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.