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
Ian:
I think I found the answer to the question that I asked in a previous
email on the following code:
%macro check;
%put >>>%bquote(")<<< ;
%mend check;
%check;
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
window.
ERROR: Macro keyword MACRO appears as text. A semicolon or other
delimiter may be missing.
J S Huang
1-515-557-3987
fax 1-515-557-2422
>>> 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
%BQUOTE.
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
takes
place. %STR and %NRSTR are compile time directives while %BQUOTE is
an
execution time function. An important point not documented, is the
difference between open macro code and that compiled by the macro
facility.
Here is a batch log that illustrates some features of the functions and
how
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 ;
2 %unquote(%str(%"abc))
3 %mend q ;
4
5 /* single quote mark quoted at
6 compile time is ok */
7 %put >>%str(%")<< ;
>>"<<
8
9 /* single quote mark quoted at execution
10 time is ok in open code */
11 %put >>>%bquote(")<<< ;
>>>"<<<
12
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
21
22 /* single quote mark generated at execution
23 time is quoted at execution time - ok */
24 %put >>>%bquote(%q)<<< ;
>>>"abc<<<
25
26 /* the unquoted quote mark is not there
27 becasue the invocatin is hidden */
The SAS System
15:31
Sunday, February 5, 2006
28 %put >>>%nrstr(%q)<<< ;
>>>%q<<<
29
30 /* the unquoted quote mark generated by %q
31 is balanced with a closing quote mark */
32 %put >>>%str(%q)"<<< ;
>>>"abc"<<<
33
34 /* error - the single quote mark is
35 neither balanced or quoted
36 */
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
2 %unquote(%str(%"abc))
makes sense since %STR acts at compile time and %UNQUOTE acts at
execution
time. We need to get the single quote mark past the compiler, but we
want
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
as
shown by the note following line 20. Second, line 37 has the
beginning
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
something
to do with the problem.
Hope this helps you study macro quoting. One word of caution. Many
things
are macro quoted by macro programmers that never need quoting. Hence
the
programs become harder to read and sometimes cause needless bugs in
their
programs. It is a good rule to always ask yourself, what are you
hiding,
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
execution
time during execution time
%NRSTR hides macro triggers, i.e. macro variable resolution
and macro execution at compile time and later at execution
time
%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
documentation.
Ian Whitlock