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

I am trying to create a macro within i am calling two other macros separately when passing different values to the parameter.

something like that-

1) %macro A;

      code

     %mend A;

2) %macro B;

       code;

    %mend B;

3) %macro AB (var=);

       code;

     %mend AB;

how do i write the code in macro AB that if i try to call it by passing (var=A) it will call macro A and if its (var=B) it will call macro B.

how can i do - %macro AB (var=);

                        %&var;

                       %mend;

                   %AB(var=A);

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

I suppose what you want is:

 

%macro A(var=);
      code
%mend A;

%macro B(var=);
       code;
%mend B; %macro AB; %A(var=A); %B(var=B); %mend AB;

 

 

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

I suppose what you want is:

 

%macro A(var=);
      code
%mend A;

%macro B(var=);
       code;
%mend B; %macro AB; %A(var=A); %B(var=B); %mend AB;

 

 

SAS_USER10
Calcite | Level 5

Thanks Sir,  it worked. 

But i have a question, it may sound silly but what if we have 30-50 or more macros to call inside a macro.

can we do it without typing all the macros like %A(var=A), %B(var=B)......... 

I mean how can we make code more generic in nature? or even is it possible to write a generic code for it?

Tom
Super User Tom
Super User

@SAS_USER10 wrote:

Thanks Sir,  it worked. 

But i have a question, it may sound silly but what if we have 30-50 or more macros to call inside a macro.

can we do it without typing all the macros like %A(var=A), %B(var=B)......... 

I mean how can we make code more generic in nature? or even is it possible to write a generic code for it?


Hypothetical questions are hard to answer. 

 

In general if you have a complex code generation problem then you will probably find it to be much easier to use metadata (data about data) to drive the process.  Then you can use a DATA step to generate the code from the metadata.  You can reduce the complexity of the code generation step by creating macros encapsulate some steps.

 

Say you had a list of "runs" you wanted to make. And each run had one or more macro calls it wanted to generate. Then you might use a step like this to generate code from that dataset with that information.

 

filename code temp;
data _null_;
  set metadata ;
  by run_id;
  file code ;
  if first.run_id then put '%init_session(' run_id= ')';
  put '%' macro_name '(' parameter_values ')';
  if last.run_id then put '%term_session(' run_id= ')';
run;
%include code / source2;

Now you can debug the code generation step by examining the program that is being generated.

SAS_USER10
Calcite | Level 5

Thanks Tom for the nice explanation. 

Down here is what i actually need to do.....

%macro dm_attrib;
attrib usubjid length = $20 label = "Unique subject identifier"....
%mend;

 

%macro vs_attrib;
%mend;

%macro attrib(domain=);
%&domain._attrib;
%mend;

%attrib(domain=dm);

 

create macros dm_attrib and vs_attrib, now create a new macro attrib in a way that when i pass the value dm to parameter domain 

it will invoke macro dm_attrib and when i pass vs it will call vs_attrib.

 

ScottBass
Rhodochrosite | Level 12
%macro foo;
   %put &sysmacroname;
%mend;
%macro bar;
   %put &sysmacroname;
%mend;
%macro blah;
   %put &sysmacroname;
%mend;
%macro baz;
   %put &sysmacroname;
%mend;
%macro code;
   %&word
%mend;
%loop(foo bar blah baz)

See https://github.com/scottbass/SAS/tree/master/Macro for the %loop macro.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2887 views
  • 0 likes
  • 4 in conversation