0
votes

Is it possible in SAS to invoke a call to a macro using a macro variable whose value is the macro's name? Something like the following:

%MACRO TEST (macroName, var1, var2, var3, var4, var5);

    %put LOG: %&macroName(&var1);

%MEND;
%TEST(testName,testVar1);

In response to Richard's answer. I tried following your solution, but am still getting an "apparent invocation of macro YearMonthString not resolved" using the following code:

%MACRO YearMonthString(nYear,nMonth);
/* Builds a string in the format YYYYMM using passed nYear and nMonth */

    %local returnVal;

    /*Build the string */
    %let returnVal = &nYear.%AddLeadingZerosToMakeNumXLength(2,&nMonth);

    /*Write to LOG */
    %put MACRO LOG: YearMonthString(nYear= &nYear, nMonth= &nMonth) will return &returnVal;

    /* return returnVal */
    &returnVal

%MEND;
%MACRO TEST (macroName, var1, var2, var3, var4, var5);

    %local loopCount;   

    %let loopCount = 1;

    %do i=1 %to 13;
        %&macroName.(&var1,&loopCount);
        %let loopCount = %eval(&loopCount+1);
    %end;

%MEND;
%TEST(YearMonthString,2018,8,,,);
1
Are you sure you have a macro named YearMonthString defined? There is no %macro statement to define YearMonthString in your posted code. - Tom
You might want to restart your SAS session to begin fresh. The macro emits a value via &returnVal and the macro invocation is followed by a semi-colon, which will also enter the SAS 'source code' flow, and thus the returnval will be 'interpreted' as a statement, of which there is none. Are you trying to generate a macro value that contains a space separated list of items that is a 13-month list of months starting from some given month ? - Richard

1 Answers

2
votes

Yes you can! The construct is % &macro-symbol. Here is a demonstration:

%macro one();
  %put &SYSMACRONAME;
%mend;

%macro two();
  %put &SYSMACRONAME;
%mend;

%macro dispatch (routine);
  %&routine.()
%mend;

%dispatch (one)
%dispatch (two)

---- LOG ----
13   %dispatch (one)
ONE
14   %dispatch (two)
TWO