BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
stataq
Quartz | Level 8

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.".

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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 (%)';

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

%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.

Cynthia_sas
SAS Super FREQ
Hi:
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
ChrisHemedinger
Community Manager

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;
...

 

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 723 views
  • 7 likes
  • 5 in conversation