Wednesday, April 13, 2011

Quoting functions in MACRO

You may know %str and %nrstr, which are compilation phase quoting functions. But do you know other quoting functions in execution phase?

Execution Functions
There are functions cause the macro facility to treat items as text during macro execution as well. %QUOTE and %NRQUOTE operate at execution time and remove meaning of most characters in the result of the macro call. %BQUOTE and %NRBQUOTE should be used if the value contains unmatched quotes or parentheses.

Examples:

%macro getst(state,employee);
%if quote(&state) = %str(NE) %then
%do;
%let longst=Nebraska;
%end;
%put hello &employee from &longst;
%mend;
%getstate(NE,O’brien)


Functions to Prevent Resolution

%SUPERQ is a function that will prevent starting any resolution of macro variables. This is most needed in windowing operations such as SAS/AF®, %WINDOW, or SAS/IntrNet® screens, or after CALL SYMPUT if there is any chance the input value could contain an
ampersand or percent sign. Other functions do not work as well in this case, and would issue warnings and recursion messages.

Example:

data _null_;
call symput('mv1','Bob&Fred %macro report');
run;
%let mv2=%superq(mv1);
%put mv2=&mv2;
The Result:
mv2=Bob&Fred %macro report


Unquoting

The effects of quoting can be removed if desired by the %UNQUOTE function. In the example below var2 is not resolved but using %UNQUOTE later allows the resolution to take place.
Example:

%let mv1= hallo;
%let mv2=%nrstr(&mv1);
%let mv3=%unquote(&mv2);
%put mv1=&mv1 mv2=&mv2 mv3=&mv3;


The Result:

mv1=hallo mv2=&mv1 mv3=hallo;

No comments: