DATA Step, Macro, Functions and more

repeating macro code inside a macro definition

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

repeating macro code inside a macro definition

I have a macro that writes a large number of IML functions.  A large part of these functions are the same across all functions.  I want to be able to edit that part that all functions share in one place rather than going through each function to make the change.  Essentially I would like to create a macro variable that contains these statements (that include macro statement) and then use that variable in each of the macro functions.  Is there a macro quoting function that I can use that will allow me to do this?

%MACRO write_functions;

%LET common="%DO;

                               some shared code

                         %END;"

START func1

     &common

     some unique code

FINISH func1;

START func2

     &common

     some unique code

FINISH func2;

%MEND write_functions;


Accepted Solutions
Solution
‎05-17-2013 09:49 AM
Super User
Super User
Posts: 7,083

Re: repeating macro code inside a macro definition

Looks like defining a macro is the way to go if what you need to have in "common" include macro logic.

Not sure WHY you would need to include MACRO logic for this application.

If the code is just normal SAS statements without macro logic then you can use DEQUOTE() or %INCLUDE.

%MACRO write_functions;

%MACRO common;

      some shared code

%MEND common;

START func1

     %common

     some unique code

FINISH func1;

START func2

     %common

     some unique code

FINISH func2;

%MEND write_functions;

View solution in original post


All Replies
Contributor
Posts: 26

Re: repeating macro code inside a macro definition

To put it another way I do not want the contents of &common to resolve when I define it but I want it to resolve when I use it.

Occasional Contributor
Posts: 7

Re: repeating macro code inside a macro definition

if &common is a parameter to the macro, you can do what you need to do. but i don't know why you wouldn't make it a separate macro that gets called. this may be useful.

HMS Analytical Software: A Modern Software Design Principle Applied To SAS Macro Programming: The In...

Super User
Posts: 5,518

Re: repeating macro code inside a macro definition

Only 2 macro quoting functions operate at compile time:  %str and %nrstr

All the others operate at run time.  The one you are looking for is probably %bquote:

%bquote(&common)

There's an outside chance it should be %nrbquote, but that depends on what would be contained in &COMMON.

Good luck.

Contributor
Posts: 26

Re: repeating macro code inside a macro definition

Posted in reply to Astounding

How would I use the %BQUOTE function?

Super User
Posts: 5,518

Re: repeating macro code inside a macro definition

To use %Bquote, just replace any reference to &COMMON with %BQUOTE(&COMMON),  However, taking a closer look at your application, that won't be enough.  You added double quotes as part of the value of &COMMON and they are fairly certain to cause a syntax error later when the double quotes get incorporated into your macro code.

I change my vote to go with the recommendation from aland1 about making a separate macro.  Instead of creating &COMMON, define %COMMON as holding the shared code.  That would make it easy to get rid of the double quotes.

%macro COMMON;

   shared code

%mend COMMON;

Then refer to %COMMON any place you are currently referring to &COMMON.

Super User
Super User
Posts: 7,083

Re: repeating macro code inside a macro definition

First try using %NRSTR() to hid the macro commands:   %nrstr(%do) .... %nrstr(%to) ... %nrstr(%end)

Did you try just using regular single quotes and then removing them?

%let common = '%do .... ' ;

...

%sysfunc(dequote(&common))

....

Contributor
Posts: 26

Re: repeating macro code inside a macro definition

%NRSTR hides the macro commands, since my code is multiple lines do I also need to hide the ";"?

I cannot see to find a resource that explains these functions clearly.

Contributor
Posts: 26

Re: repeating macro code inside a macro definition

dequote doesn't work.

Valued Guide
Posts: 2,177

Re: repeating macro code inside a macro definition

%include might be the way to go

It is resolved at macro run time.

It must be worth trying.

peterC

Contributor
Posts: 26

Re: repeating macro code inside a macro definition

This doesn't work.

Solution
‎05-17-2013 09:49 AM
Super User
Super User
Posts: 7,083

Re: repeating macro code inside a macro definition

Looks like defining a macro is the way to go if what you need to have in "common" include macro logic.

Not sure WHY you would need to include MACRO logic for this application.

If the code is just normal SAS statements without macro logic then you can use DEQUOTE() or %INCLUDE.

%MACRO write_functions;

%MACRO common;

      some shared code

%MEND common;

START func1

     %common

     some unique code

FINISH func1;

START func2

     %common

     some unique code

FINISH func2;

%MEND write_functions;

🔒 This topic is solved and locked.

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

Discussion stats
  • 11 replies
  • 397 views
  • 0 likes
  • 5 in conversation