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;
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;
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.
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.
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.
How would I use the %BQUOTE function?
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.
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))
....
%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.
dequote doesn't work.
%include might be the way to go
It is resolved at macro run time.
It must be worth trying.
peterC
This doesn't work.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.