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.".
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 (%)';
%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.
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;
...
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 (%)';
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.