Search notes:

SAS programming - macro function: %str

%str should be used
* quote special tokens such as semicolons, percent signs and ampersands;
%let  s=%str(semicolon; %percent; &ampersand);
%put &s;
Github repository about-SAS, path: /macro-processor/functions/str.sas
%nrstr is like %str except that is also masks % and &.

Open code vs in macro execution

/* Ian Whitlock: A Serious Look Macro Quoting 

   http://www2.sas.com/proceedings/sugi28/011-28.pdf

*/

%let v_semicolon = %str(;);

%put v_semicolon >&v_semicolon<; /* semicolon >;< */


/*   The following put is executed in
     OPEN CODE and throws the following error:

     ERROR 180-322: Statement is not valid or
           it is used out of proper order.

     This is because the line is interpreted as
     %put %upcase(semicolon >;<);
*/
%put %upcase(v_semicolon >&v_semicolon<);

/*  However, with %qupcase instead of %upcase, the
    put can be made: */
%put %qupcase(v_semicolon >&v_semicolon<);



%macro m_semicolon;
  %local l_semicolon;
  %let l_semicolon = %str(;);

  %put %upcase(l_semicolon >&l_semicolon<);
%mend m_semicolon;

/* This works! Apparently because the %put
   is executed in CLOSED CODE.*/
%m_semicolon;
Github repository about-SAS, path: /macro-processor/functions/str/open-code-vs-in-macro.sas

Mnemonics

%macro oregon_trail(state);
  /* 
     Use str because NE and OR are mnemonics with special meaning to the macro processor

     Found in http://www2.sas.com/proceedings/forum2007/152-2007.pdf (Ex_1) 
  */

  %if &state eq %str(NE) or
      &state eq %str(OR) or
      &state eq      MO  or
      &state eq      KS  or
      &state eq      WY  or
      &state eq      ID
  %then
    %put The Oregon Trail ran through &state;
  %else
    %put The Oregon Trail did not run through &state;
%mend oregon_trail;

%oregon_trail(NY);
%oregon_trail(%str(OR));
%oregon_trail(%quote(NE));
%oregon_trail(WY);
Github repository about-SAS, path: /macro-processor/functions/str/mnemonics.sas

See also

macro quoting
macro functions

Index