Search notes:

SAS: Macro quoting

Macro quoting is necessary to indicate to the macro processor that a special character is to interpreted as text rather than as a symbol of the macro language.
When text is quoted, the macro processor masks the special character with a so called delta characters: a prefix and a suffix delta character enclose the special character. Additionally, the prefix delta character indicates the type of the quoting mechanism used.

Quoting functions

%str, %nrstr
%bquote, %nrbquote
%superq,
%quote, %nrquote
nr stands for not resolved (or no rescan?). These functions address the same characters like the functions without nr. expcept that they keep the macro processor from rescanning the result of the function. This is useful when & or % need to be maintained.
b functions can quote unbalanced parentheses and unmatched quotation marks.
Quoting functions can be categorized by

Examples

%str vs %nrstr

%let mv      =%nrstr(Acme&Co);

%let mv_str  =%str  (&mv    );
%let mv_nrstr=%nrstr(&mv    );

%put &mv_str.;    /* Acme&Co */
%put &mv_nrstr.;  /* &mv     */

Compile time vs execution time

str% and %nrstr are executed during macro-compilition.
The other macro quoting functions are executed when the compiled macro executes.

Using %nrstr to quote macro statements

/* %nrstr allows to quote macro statements: */
%let quotedStatements=%nrstr(
  %put macroVarOne = &macroVarOne;
  %put macroVarTwo = &macroVarTwo;
);

/* &quotedStatements now contains the quoted statements: */
%put &quotedStatements; /*  %put macroVarOne = &macroVarOne;   %put macroVarTwo = &macroVarTwo; */

/* Assign some values to the macro variables referenced in
   the quoted statements: */
%let macroVarOne = foo;
%let macroVarTwo = baz;

/* With unquote, the statements will be executed: */
%unquote(&quotedStatements)
/*
macroVarOne = foo
macroVarTwo = baz
*/

Percent sign and ampersands

In order to produce a percent sign with %str or %nrstr, two percent signs must be used
%put Percent sign: %str     (%%), ampersand: %str     (&);   /* Percent sign: %, ampersand: &  */
%put Percent sign: %quote   (% ), ampersand: %quote   (&);   /* Percent sign: % , ampersand: & */
%put Percent sign: %bquote  (% ), ampersand: %bquote  (&);   /* Percent sign: % , ampersand: & */
%put Percent sign: %nrstr   (%%), ampersand: %nrstr   (&);   /* Percent sign: %, ampersand: &  */
%put Percent sign: %nrquote (% ), ampersand: %nrquote (&);   /* Percent sign: % , ampersand: & */
%put Percent sign: %nrbquote(% ), ampersand: %nrbquote(&);   /* Percent sign: % , ampersand: & */

TODO

options nomprint;
/*
 
    Problems to be solved:
       Does a semicolon (;) belong to a macro statement or to an ordinary SAS statement?

    str:      The most basic quoting function.
              It hides symbols that could be interpreted by the macro compiler at
              macro compile time. (Such symbols are: semicolon, space ...)
    bquote
    left
    nrstr
    qleft
    nrbquote
    superq

    Macro quoting:
      Applied during compilation: operate against text in code
        - %str
        - %nrstr
      Applied during execution of macro element: operate against resolved values

*/

%let text                  = twenty-two;
%let text_with_comma       = Foo, bar, baz;
%let text_with_paranthesis = Hello (World);
%let text_with_quotes      = He said "hasta la vista";
%let text_with_ampersand   = %bquote(Acme&Co.); /* Use bquote to prevent "Apparent symbolic reference Co not resolved." */
%let text_with_percent     = 42%is not enough;
%let text_with_comment     = this is a /* comment */;

%put         &text_with_quotes;
%put %bquote(&text_with_quotes);


%macro macro_with_one_parameter(param);
  %put macro with one param says: &param;
%mend  macro_with_one_parameter;

   %macro_with_one_parameter(        &text                  );
   %macro_with_one_parameter(        &text_with_paranthesis );
   %macro_with_one_parameter(        &text_with_quotes      );
   %macro_with_one_parameter(        &text_with_ampersand   );
   %macro_with_one_parameter(        &text_with_percent     );
   %macro_with_one_parameter(        &text_with_comment     );
   %macro_with_one_parameter(%bquote(&text_with_comma       )); /* Use bquote to prevent "ERROR: More positional parameters found than defined." */

/* ---------------------------------------------- */

%let var = foo;

%let put_var_str      = %str     (data _null_; put "&var"; run;);
%let put_var_nrstr    = %nrstr   (data _null_; put "&var"; run;);
%let put_var_quote    = %quote   (data _null_; put "&var"; run;);
%let put_var_nrquote  = %nrquote (data _null_; put "&var"; run;);
%let put_var_bquote   = %bquote  (data _null_; put "&var"; run;);
%let put_var_nrbquote = %nrbquote(data _null_; put "&var"; run;);
%let put_var_superq   = %superq  (data _null_; put "&var"; run;);

%put &put_var_str;
%put &put_var_nrstr;
%put &put_var_quote;
%put &put_var_nrquote;
%put &put_var_bquote;
%put &put_var_nrbquote;


%let var = bar;

&put_var_str;
&put_var_nrstr;
&put_var_quote;
&put_var_nrquote;
&put_var_bquote;
&put_var_nrbquote;

%unquote(&put_var_bquote);
Github repository about-SAS, path: /macro-processor/todo.quoting.sas

See also

macros

Index