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.
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;
Did you try the code you posted? It should work.
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;
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?
@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.
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.
%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.
Sure, Thanks! I appreciate
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.