Hi all,
I'm using a data set to create labels as guided in the Stack Overflow thread, https://stackoverflow.com/questions/46407959/how-can-i-set-the-variable-labels-in-sas-using-a-table. How should the code be modified to make it allow special characters in the labels? Thanks a lot!
%macro label_it (var=, label=);
label &var. = "&label.";
%mend label_it;
proc sql;
select cats('%label_it(var=',var,',label=',description,')')
into :labellist separated by ' '
from varlabels;
quit;
data want;
set have;
&labellist.;
run;
That is easy.
Change the macro to expect a quoted string. Then pass it a quoted string. While you are at it make it compatible with VALIDVARNAME=ANY option by using NLITERAL() function.
%macro label_it (var=, label=);
label &var. = &label.;
%mend label_it;
proc sql;
select cats('%label_it'
,'(var=',nliteral(var)
,',label=',quote(trim(description),"'")
,')')
into :labellist separated by ' '
from varlabels;
quit;
data want;
set have;
&labellist.;
run;
That is easy.
Change the macro to expect a quoted string. Then pass it a quoted string. While you are at it make it compatible with VALIDVARNAME=ANY option by using NLITERAL() function.
%macro label_it (var=, label=);
label &var. = &label.;
%mend label_it;
proc sql;
select cats('%label_it'
,'(var=',nliteral(var)
,',label=',quote(trim(description),"'")
,')')
into :labellist separated by ' '
from varlabels;
quit;
data want;
set have;
&labellist.;
run;
Sounds like you only implement HALF of the suggested fix.
If you set the parameter LABEL to the value of
'My Label'
and then use
"&LABEL"
in the code you get
"'My Label'"
But if instead the macro code just uses
&LABEL
then you will get
'My Label'
in the code, which is exactly what you want.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.