- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I am trying to understand when to use %bquote() and how it works. I have following code setup as exercise. If I keep it as is, it won't work. If I changed 'not work' line to the next row it will work:
data df; infile datalines dsd truncover; input rowlbl $ sex $ stop; datalines; Y,Female,0 ; %macro m_lbl( grp=, _rowlbl=%bquote() ); data df; length rowlbl $200.; set df; grp=&grp.; %if &_rowlbl ne %bquote() %then %do; rowlbl=%bquote(&_rowlbl.); /*not work*/
/* rowlbl=&_rowlbl.; */ /*not work*/ /* rowlbl="&_rowlbl."; */ /*work*/ %end; run; %mend; %m_lbl( grp=1, _rowlbl=%bquote(Gender, n (%)) );
Could anyone tell me why I can't use ' rowlbl=%bquote(&_rowlbl.); ' in my code? Maybe also some guideance on when we should use %bquote(&_rowlbl.), when we should use &_rowlbl. , and when we should use "&_rowlbl.".
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
As @Kurt_Bremser said you issue is not with your macro logic.
Instead your issue is with the SAS code you are trying to use the macro logic to generate. Both of these statements:
rowlbl=%bquote(&_rowlbl.);
rowlbl=&_rowlbl.;
Will generate this SAS code:
rowlbl=Gender, n (%);
Does that look like valid SAS code to you?
If you want to add quotes use the QUOTE() function. You can use the optional second argument to the QUOTE() function to have the string bounded by single quotes to prevent any embedded macro triggers from being seen by the macro processor.
rowlbl=%sysfunc(quote(%bquote(&_rowlbl.),%str(%')));
Which will generate this SAS code:
rowlbl='Gender, n (%)';
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%BQUOTE is for masking special characters and strings (like AND), so that they are not resolved by the macro processor. Use it only for this purpose.
It does not put quotes around a string.
If a macro variable contains character data, enclose it in quotes in SAS code; if it contains numeric data, do not use quotes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I always find this chart about the quoting functions to be useful:
https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0pwrvnlcooi3tn0z3g1755ebcng.htm
(title: Summary of Macro Quoting Functions and the Characters That They Mask)
Cynthia
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If your goal is to check whether this macro parameter is empty/blank, this is the commonly used pattern in the form of a macro function:
/* Reliable way to check whether a macro value is empty/blank */
%macro isBlank(param);
%sysevalf(%superq(param)=,boolean)
%mend;
...
if %isBlank(&_rowlbl) %then %do;
...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
As @Kurt_Bremser said you issue is not with your macro logic.
Instead your issue is with the SAS code you are trying to use the macro logic to generate. Both of these statements:
rowlbl=%bquote(&_rowlbl.);
rowlbl=&_rowlbl.;
Will generate this SAS code:
rowlbl=Gender, n (%);
Does that look like valid SAS code to you?
If you want to add quotes use the QUOTE() function. You can use the optional second argument to the QUOTE() function to have the string bounded by single quotes to prevent any embedded macro triggers from being seen by the macro processor.
rowlbl=%sysfunc(quote(%bquote(&_rowlbl.),%str(%')));
Which will generate this SAS code:
rowlbl='Gender, n (%)';