Search code examples
sassas-dis

Improvement on %NRQUOTE 'removal'


I have the following piece of code that works, but I'd like to know if anyone can come up with a better way of 'removing' the %nrquote. I have had to add a %SUBSTR function, which works, but I'm keen to know if there are any other suggestions, and if anyone can help explain why the code doesn't work without the %let statement within the mvar macro definition.

/* Automatically generated by DI Studio - cannot change */
%let _where_clause = %nrquote(name = %'Henry%');
%let _mac1 = %nrquote(lemk);
%let _variable = weight;
%let _input0 = sashelp.class;
/* End of auto-generated code */

options mprint;

%macro mvar;
    %if &_where_clause ^= %then %do;
        /* Re-assign the _where_clause variable to 'remove' %nrquote */
        %let _where_clause = %substr(&_where_clause,1);
        where &_where_clause
    %end;
%mend mvar;

proc sql;
    select &_variable into :&_mac1
    from &_input0
    %mvar
    ;
quit;

Without the %let statement, the code fails with this error:

NOTE: Line generated by the macro variable "_WHERE_CLAUSE".
1     name = 'Henry'
             -
             22
MPRINT(MVAR):   where name = '
NOTE: Line generated by the macro variable "_WHERE_CLAUSE".
1     name = 'Henry'
             -
             200
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,
              a numeric constant, a datetime constant, a missing value, (, *, +, -, ALL, ANY,
              BTRIM, CALCULATED, CASE, INPUT, PUT, SELECT, SOME, SUBSTRING, TRANSLATE, USER.

ERROR 200-322: The symbol is not recognized and will be ignored.

114          ;
MPRINT(MVAR):  Henry'

Solution

  • You need %UNQUOTE which is what is happening with %LET, it is un-quoting the quoted quotes.

    Change 
    where &_where_clause
    to 
    where %unquote(&_where_clause)