Search code examples
sassas-macro

Why does the %str in `%str(%inner_macro())` not work?


%str should pass a string as one parameter to a sas macro, even if it contains commas, but that apparently does not work if the argument of %str is in it self the result of a macro? I get ERROR: More positional parameters found than defined.

Example

This is what the error means

    15         %macro outer_macro(left, right);
    16          %put NOTE: outer_macro: left is &left;
    17          %put NOTE: outer_macro: right is &right;
    18         %mend;
    19         %outer_macro(left, right);
    NOTE: outer_macro: left is left
    NOTE: outer_macro: right is right
    20         %outer_macro(left, midle, right);
    ERROR: More positional parameters found than defined.

This is how %str resolves it in normal situations.

    21         %outer_macro(left, %str(midle, right));
    NOTE: outer_macro: left is left
    NOTE: outer_macro: right is midle, right

It is possible to construct an argument of a macro with an inner macro

    23         %macro blank_macro(left, right);
    24          %put NOTE: blank_macro: left is &left;
    25          %put NOTE: blank_macro: right is &right;
    26          &left. &right.
    27         %mend;
    28         %outer_macro(links, %blank_macro(midden, rechts));
    NOTE: blank_macro: left is midden
    NOTE: blank_macro: right is rechts
    NOTE: outer_macro: left is links
    NOTE: outer_macro: right is midden rechts

But if the inner macro inserts a comma, you get the original error

    30         %macro comma_macro(left, right);
    31          %put NOTE: comma_macro: left is &left;
    32          %put NOTE: comma_macro: right is &right;
    33          &left., &right.
    34         %mend;
    35         %outer_macro(left, %comma_macro(midle, right));
    NOTE: comma_macro: left is midle
    NOTE: comma_macro: right is right
    ERROR: More positional parameters found than defined.

I would expect %str to resolve this, but it does not. Why?

    36         %outer_macro(left, %str(%comma_macro(midle, right)));
    NOTE: comma_macro: left is midle, right
    NOTE: comma_macro: right is
    ERROR: More positional parameters found than defined.

Context

For your information: I need this because I wrote a macro to list a few fields I should query from a database and I need to pass my sql to the database via another macro.

%run_SQL(select key, %list_fields(some_arguments), from something);

Solution

  • Quote the output from %COMMA_MACRO

    33          %macro comma_macro(left, right);
    34            %put NOTE: comma_macro: left is &left;
    35            %put NOTE: comma_macro: right is &right;
    36            %nrbquote(&left., &right.)
    37            %mend;
    38         %macro outer_macro(left, right);
    39            %put NOTE: outer_macro: left is &left;
    40            %put NOTE: outer_macro: right is &right;
    41            %mend;
    42         %outer_macro(left, %comma_macro(midle, right));
    NOTE: comma_macro: left is midle
    NOTE: comma_macro: right is right
    NOTE: outer_macro: left is left
    NOTE: outer_macro: right is midle, right