Invoke a Macro Using A Macro Variable Name

冷暖自知 提交于 2019-12-25 02:16:05

问题


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:


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


来源:https://stackoverflow.com/questions/52099077/invoke-a-macro-using-a-macro-variable-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!