使用宏变量名称调用宏

时间:2018-08-30 14:07:06

标签: callback sas metaprogramming pass-by-reference sas-macro

在SAS中,是否可以使用值为宏名称的宏变量来调用对宏的调用?类似于以下内容:

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

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

%MEND;
%TEST(testName,testVar1);

对理查德的回答。我尝试遵循您的解决方案,但仍使用以下代码获得“未解析宏YearMonthString的表调用”:

%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 个答案:

答案 0 :(得分:2)

可以!构造为% & 宏符号。这是一个演示:

%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