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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 252 views
  • 2 likes
  • 2 in conversation