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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

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;
lichee
Quartz | Level 8
This works. However, there are unnecessary single quote marks in the labels.
Tom
Super User Tom
Super User

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.

 

SAS Innovate 2025: Register Now

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!

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
  • 3 replies
  • 730 views
  • 2 likes
  • 2 in conversation