Search code examples
sassas-macro

How do I use a value that is returned from a Macro Variable in another macro


I have simplified this a lot so it can be repeated

%macro macro_one(dt2);
  %let var1 = &dt2;
%mend;

Then I have another macro and I want to use the output from macro one in macro 2

%macro macro_print(dt2);
  /*call macro 1*/
  %macro_one(&dt2);
  %put &var1;
%mend;

/call macro/

%macro_print('purple');

It should print purple in the logs but I get an error I get an error though - i suspect I need to assign the macro variable from macro one when I call macro two.


Solution

  • First, I suspect you have a typo between your code and here. Proper way to define a macro is:

    %macro blah(x);
    <do stuff>
    %mend;
    

    not:

    %macro_blah(x);
    <do stuff>
    %mend;
    

    The macro is created in %macro_one and defaults to a local scope. You can fix this by declaring it %global.

    %macro macro_one(dt2);
       %global var1;
       %let var1=&dt2;
    %mend;
    

    Also, use %put not put in %macro_two.