Thursday, February 23, 2012

Macro Quoting : A quick and easy reference


Macro Quoting : A quick reference

Objective : This post deals with quoting of macro variables and their use. The final objective of this post is that after reading this you will be able to decide where and why to use a particular quoting function.
Macros are considered to be a tough topic in SAS and within Macros these quoting functions are considered abstruse. But I will say that this topic is not as difficult as it seems and you just need an objective approach to make a correct decision on which function to use.

Theory:  The major problem with most programmers is that they just start using hit and trial on quoting functions whenever they feel the need of quoting anything. But only thing you need is an objective strategy.

So first let us discuss why macro quoting is used.

WHY???

I’ll give the answer to this why with an example. Let us take a SAS statement.

%let var=hello;
%put &var;

LOG : hello

Now I want a semicolon after ‘hello’ to be as part of the value of macro variable var. So I write the following code :

%let var=hello;;
%put &var;

LOG : hello

Hey..!! what happened where is my other semi-colon.

What happened here is, SAS considered the first semi-colon as statement end, assigned the value ‘hello’ to var and treated the second semi-colon as empty SAS statement.

So what we need here??? Answer anyone…

Right!! Quoting.

%let var=%str(hello;);
%put &var;

LOG : hello;

Here…now my log danced on my tune.

So macro quoting is used to hide the special meaning of special characters in SAS.

So before saying anything..Quote it..then you’ll never have to say “I did not mean that”.

Next question arises…

WHAT ARE SPECIAL CHARATERS IN SAS???

Special characters are the characters holding a specific meaning in SAS. For e.g. GE is just a set of 2 alphabets but in SAS GE mean "Greater than or equal to".

Some of the special characters are:
blank ; % & ‘ “ ( ) + - = * / < > ^ | , ~ -- # GE LE EQ OR AND GT LT NE IN

Macro quoting should be used to mask these special characters in a SAS program when they are intended to be used as plain text.

WHAT MACRO QUOTING FUNCTIONS ARE AVAILAIBLE???

The available functions are:

  Ø  %STR
  Ø  %NRSTR
  Ø  %BQUOTE
  Ø  %NRBQUOTE
  Ø  %QUOTE
  Ø  %NRQUOTE
  Ø  %SUPERQ
  Ø  %UNQUOTE

I’ll discuss their use one by one.

Here one thing to remember is that the %STR and %NRSTR are compile time functions and rest except %SUPERQ are execution time functions.

What is this compile time and execution time???

Macro statements are compiled by macro processor and then executed We need to control when the text string is to be quoted. Some quoting functions remove the meaning during compilation while others remove it during macro execution. When meaning is removed during compilation (%STR and %NRSTR) the resultant text is passed to the processor, but not the function call. The other quoting functions are resolved during the execution of the macro. For these functions the entire call to the function and its text is passed to the macro processor where it is resolved.
In Short,Compile time functions mask the special meaning while compilation of code but not while executing the code. But execution time functions mask the special meaning at execution as well.

The tokens to be masked can be grouped in 3 categories:

Cat1: All special characters except the ones in Cat2 and Cat3
blank ; + - = * / < > ^ | , ~ -- # GE LE EQ OR AND GT LT NE IN ‘’ “”

Cat2: Characters that occur in pair.
‘ “ ( )

Cat3: Macro triggers
& %

Different functions are required to mask each of these categories of characters.

%STR: Masks Cat1 characters at compile time.

Note: A point to note here is that %STR will mask cat2 tokens also if they appear in pairs but not when they appear alone.

%NRSTR: Masks Cat1 and Cat3 characters at compile time.

Note: All function starting with NR do the same job as their counterpart without-NR functions plus one thing extra that is masking macro triggers also.

%QUOTE: This is a execution time function which masks Cat1 characters at execution time.

%NRQUOTE: Masks Cat1 and Cat3 characters at execution time.

%BQUOTE: Also called “blind Quote” for its extra ability to mask Cat2 or unmatched pair tokens. Masks Cat1 and Cat2 characters at execution time.

%NRBQUOTE: Masks Cat1, Cat2 and Cat3 characters at execution time.
Now you see why so many functions, each have a work of its own. Everyonez got unique style..!!  J

%UNQUOTE: Works as a cop for the above spoilt youth. This function removes any masking done by each of the above functions

Oh!! I almost forgot %SUPERQ.

%SUPERQ: This execution time quoting function is a separate monster and works in a way different from all the above functions.

This function requires a macro variable name as its argument and is used when you want only first resolution of the macro variable and don’t want anything in its value to resolve further.
It is different form %NRBQUOTE in that it will never try to resolve further the macro expression if either an & or % sign are present in the macro expression after the first resolution.

Conclusion : So if you are reading this it means that you have read the post (Can also mean that you have just scrolled to see the climax… in that case go up and read the complete post) and next time you require quoting anywhere you’ll know which function will do your job best and the outputs will be predictable.
More scuh tips will follow soon. So keep reading guyz.

Will be back with some more SAS magic. Till then Goodbye.

References :
SAS 9.2 macro language reference by SAS Institute.
http://www.ats.ucla.edu/stat/sas/library/nesug99/ad088.pdf

Saurabh Singh  Chauhan
(er.chauhansaurabh@gmail.com)         
Note: Comments and suggestions are always welcome

Disclaimer :
SAS® and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc.in the USA and other countries. ® Indicates USA registration.
Other brand and product names are registered trademarks or trademarks of their respective companies. 
The contents of this post are the works of the author(s)and do not necessarily represent the opinions,recommendations, or practices of any organization whatsoever.

No comments:

Post a Comment