简体   繁体   中英

SAS macro error: More positional parameters found than defined

Given the following SAS data set and macro, could someone please provide an explanation as to why the first 3 calls to the macro execute without errors but the 4th call produces the error message as listed below. If I don't use a default value for "code" (change the “code=A11” to “code”) in the macro definition, then all 4 calls execute without error. Why is this? I have tried exiting the SAS session and restarting but it didn't help.

New to SAS. Thank you. Much Appreciated. :)

    ------------------- begin code block -----------------------------------


    /* data set definition */

    data dat5;
        infile datalines truncover;
        input Year Prod_Cd $ Sales;
        datalines;
        2001 A11 100
        2001 B12 200
        2002 C13 300
        ;
    run;

    /* macro definition */
    %macro sel(code=A11); 
        proc sql;
            select * from dat5
            where Prod_Cd = "&code";
        quit;
    %mend;

    /* calls to the macro */

    %sel();             /* 1st call */
    %sel(code=A11);     /* 2nd call */
    %sel(code=A12);     /* 3rd call */
    %sel(A11);          /* 4th call */


    --------------------end code block--------------------------------------

    /* SAS log message for the 4th call to the macro */


    128  %macro sel(code=A11);
    129      proc sql;
    130          select * from dat5
    131          where Prod_Cd = "&code";
    132      quit;
    133  %mend;
    134
    135  %sel(A11);
    MLOGIC(SEL):  Beginning execution.
    ERROR: More positional parameters found than defined.
    MLOGIC(SEL):  Parameter CODE has value A11
    MLOGIC(SEL):  Ending execution.

    ------------------------------------------------------------------------          

When you define a macro you can specify whether or not the parameter can be called by position (that is without naming the parameter in the macro call). So called positional parameters are defined in the %MACRO statement without an = after the name. You can define both positional and named parameters, but the positional parameters must appear first. Note that you can specify values for any parameter by name in the macro call, but only parameters defined as positional can be called by position.

So if you want to specify a value for the CODE parameter in the macro call without including the name then you need to define the macro like this:

%macro sel(code);

Then all of the calls in your example will work, but the first one will set CODE to an empty string instead of A11 . If you want to have a default value for parameters that are defined as positional you will need to add the logic to the macro itself. Such as this:

%macro sel(code);
%if not %length(&code) %then %let code=A11;
proc sql;
  select * from dat5
    where Prod_Cd = "&code"
  ;
quit;
%mend;

This is expected behaviour:

%sel();             /* 1st call - no parameters specified - SAS uses default values from macro definition */
%sel(code=A11);     /* 2nd call - keyword parameter used correctly */
%sel(code=A12);     /* 3rd call - keyword parameter used correctly */
%sel(A11);          /* 4th call - No parameter name or =, so SAS interprets this as a positional parameter, but none are defined within the macro, hence the error*/

SAS® 9.4 Macro Language: Reference, Fifth Edition, %MACRO Statement

When you define a macro the parameters list is specified as a comma separated list of positional items ( name ) followed by keyword items ( name=default-value ).

When the macro is invoked, the arguments, specified as a comma separated list of values, are mapped to the parameters.

  • Positional arguments must occur in the same order as in the macro definition.
  • Keyword arguments can occur in any order after the positional arguments.
    • Positional arguments can also be specified as name=value. This is a good convention for ensuring readability in macro heavy code.
  • Any positional arguments that are skipped will be passed as an empty string.
  • Any unspecified keyword parameter is assigned the default value.
  • Error if the argument list tries to specify a parameter more than once (as can occur if you incorrectly mix positional and keyword or repeat a keyword)

Using A11 value as a positional argument causes an error because the parameter list for macro %sel does not have any positional parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM