Invoke a Macro Using A Macro Variable Name

2019-08-25 10:04发布

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条回答
太酷不给撩
2楼-- · 2019-08-25 10:17

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
查看更多
登录 后发表回答