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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.