Date: Sun, 5 Feb 2006 16:46:43 -0600
Reply-To: Jiann-Shiun Huang <Jiann-Shiun.Huang@AMERUS.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jiann-Shiun Huang <Jiann-Shiun.Huang@AMERUS.COM>
Subject: Re: macro quoting functions %str and %bquote
Content-Type: text/plain; charset=US-ASCII
I think I found the answer to the question that I asked in a previous
email on the following code:
%put >>>%bquote(")<<< ;
From "SAS Macro Language Reference" I found that "...%BQUOTE
function... (It does not mask parentheses or quotation marks that are
not produced by resolution)..." Since the double quotation mark in the
code is not produced by resolution and hence is not masked by %bquote
function. This explains why the following ERROR appears in log
ERROR: Macro keyword MACRO appears as text. A semicolon or other
delimiter may be missing.
J S Huang
>>> Ian Whitlock <iw1junk@COMCAST.NET> 2/5/2006 2:41:52 PM >>>
Subject: macro quoting functions %str and %bquote
Summary: Short macro quoting lesson emphasizing macro compile time
versus open code execution time using %STR, %NRSTR and
Respondent: Ian Whitlock
Jiann-Shiun Huang <jiann-shiun.huang@AMERUS.COM> asks for examples
illustrating differences between %STR, %NRSTR and %BQUOTE.
One big difference, although documented, is when the initial quoting
place. %STR and %NRSTR are compile time directives while %BQUOTE is
execution time function. An important point not documented, is the
difference between open macro code and that compiled by the macro
Here is a batch log that illustrates some features of the functions and
to think about them. Study the comments and results.
NOTE: SAS initialization used:
real time 0.42 seconds
cpu time 0.41 seconds
1 %macro q ;
3 %mend q ;
5 /* single quote mark quoted at
6 compile time is ok */
7 %put >>%str(%")<< ;
9 /* single quote mark quoted at execution
10 time is ok in open code */
11 %put >>>%bquote(")<<< ;
13 /* but not in macro */
14 options mcompilenote = all ;
15 %macro check ;
16 %put >>>%bquote(")<<< ;
17 %mend check ; /* in quote */
18 %put ---- ;
ERROR: Macro keyword PUT appears as text. A semicolon or other
delimiter may be missing.
19 %mend check ;
NOTE: The macro CHECK completed compilation without errors.
3 instructions 32 bytes.
20 data w ; x = 1 ; run ;
NOTE: SAS set option OBS=0 and will continue to check statements.
This may cause NOTE: No observations in data set.
NOTE: The data set WORK.W has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds
22 /* single quote mark generated at execution
23 time is quoted at execution time - ok */
24 %put >>>%bquote(%q)<<< ;
26 /* the unquoted quote mark is not there
27 becasue the invocatin is hidden */
The SAS System
Sunday, February 5, 2006
28 %put >>>%nrstr(%q)<<< ;
30 /* the unquoted quote mark generated by %q
31 is balanced with a closing quote mark */
32 %put >>>%str(%q)"<<< ;
34 /* error - the single quote mark is
35 neither balanced or quoted
37 %put >>>%str(%q)<<< ;
38 data w ; x = 1 ; run ;
>>>"abc <<< ; data w ; x = 1 ; run ;
ERROR: Errors printed on page 1.
NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
NOTE: The SAS System used:
real time 0.55 seconds
cpu time 0.46 seconds
Note that the line
makes sense since %STR acts at compile time and %UNQUOTE acts at
time. We need to get the single quote mark past the compiler, but we
an unquoted result for later testing.
Two apparent SAS bugs appear in the log. First the note after line 19
indicates the macro CHECK compiled all right while the error in the
macro indicates that it is not all right. Moreover, SAS set obs to 0
shown by the note following line 20. Second, line 37 has the
of a quoted expression but no end quote and it is not caught SAS. The
curious box on the line following line 38 may or may not have
to do with the problem.
Hope this helps you study macro quoting. One word of caution. Many
are macro quoted by macro programmers that never need quoting. Hence
programs become harder to read and sometimes cause needless bugs in
programs. It is a good rule to always ask yourself, what are you
and who are you hiding it from.
%STR should hide symbols from the macro compiler and later at
execution time, but it does not symbols generated at
time during execution time
%NRSTR hides macro triggers, i.e. macro variable resolution
and macro execution at compile time and later at execution
%BQUOTE hides generated symbols as macro execution time, but not
at macro compile time
Details, of which symbols in particular are hidden, are left to the