Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAS macro error: More positional parameters found than defined

Tags:

macros

sas

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.

    ------------------------------------------------------------------------          
like image 226
MrsDP Avatar asked May 20 '26 10:05

MrsDP


1 Answers

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;
like image 89
Tom Avatar answered May 24 '26 03:05

Tom