Search code examples
macrossasreturnreturn-valuesas-macro

SAS Macro with Return value with more text afterwards


Is it possible to return a value from a SAS macro, and continue the current SAS line after returning the value?

e.g. Desired output to SAS (without quotes):

"set test.hello_2018_2020_2028;"

I've tried the following:

%MACRO returnFunc(passVar);
    %local testReturn;
    %let testReturn = %eval(&passVar +1);
    &testReturn
    %return;
%MEND returnFunc;

%MACRO test;
    %local var1;
    %local varPassed;
    %local anotherVar;

    %let var1 = 2018;
    %let varPassed = 2019;
    %let anotherVar = 2028;

    set test.hello_&var1._%returnFunc(&varPassed)_&anotherVar;
%MEND test;

However I get errors like the following:

  • File test.hello_2018_2020.DATA does not exist

  • File WORK._2028 Does not exist

So the macro returns the value fine, however it starts trying to make another set statement instead of adding _&anotherVar to the set statement


Solution

  • Yes %UNQUOTE and remove the . after the macro call.

    111  %MACRO returnFunc(passVar);
    112      %local testReturn;
    113      %let testReturn = %eval(&passVar +1);
    114      &testReturn
    115      %return;
    116  %MEND returnFunc;
    117
    118  %MACRO test;
    119      %local var1;
    120      %local varPassed;
    121      %local anotherVar;
    122
    123      %let var1 = 2018;
    124      %let varPassed = 2019;
    125      %let anotherVar = 2028;
    126
    127      set %unquote(test.hello_&var1._%returnFunc(&varPassed)_&anotherVar);
    128  %MEND test;
    129  options mprint=1;
    130  data _null_;
    131     %test;
    MPRINT(TEST):   set
    MPRINT(TEST):   test.hello_2018_2020_2028;
    ERROR: Libref TEST is not assigned.
    132     run;