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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

11 REPLIES 11
derrick
Calcite | Level 5

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.

aland1
Calcite | Level 5

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

Astounding
PROC Star

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.

derrick
Calcite | Level 5

How would I use the %BQUOTE function?

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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

....

derrick
Calcite | Level 5

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

derrick
Calcite | Level 5

dequote doesn't work.

Peter_C
Rhodochrosite | Level 12

%include might be the way to go

It is resolved at macro run time.

It must be worth trying.

peterC

derrick
Calcite | Level 5

This doesn't work.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 1300 views
  • 0 likes
  • 5 in conversation