BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
naziya
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
ballardw
Super User

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.

naziya
Calcite | Level 5

hey thanks.

PaigeMiller
Diamond | Level 26

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
naziya
Calcite | Level 5

thanku

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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