BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to add labels to colums in  data set Have.

I want to use other  data set and take the labels from there instead of typing it manually.

What is the way to do it please?

data labels_info_tbl;
infile datalines dlm="," dsd;
length label $100 Var_name $10;
input Var_name $ label;
cards;
custID,Customer Id number
Wealth,wealth in bank in USD
education,level of education 
;
run;


data have;
/*label*/
/*custID= 'Customer Id number'*/
/*Wealth= 'wealth in bank in USD'*/
/*education= 'level of education'*/
/*;*/
input  custID Wealth education;
cards;
111 30000 15
222 7000 12 
333 350000 21
;
Run;
3 REPLIES 3
Ronein
Onyx | Level 15

Ifound nice solution, maybe people can show other ways?

 

data labels_info_tbl;
infile datalines dlm="," dsd;
length label $100 Var_name $10;
input Var_name $ label;
cards;
custID,Customer Id number
Wealth,wealth in bank in USD
education,level of education 
;
run;


data have;
/*label*/
/*custID= 'Customer Id number'*/
/*Wealth= 'wealth in bank in USD'*/
/*education= 'level of education'*/
/*;*/
input  custID Wealth education;
cards;
111 30000 15
222 7000 12 
333 350000 21
;
Run;


proc sql  noprint;
select catx('=',Var_name,quote(trim(label))) into  : label_stmt separated by ' '
from labels_info_tbl
;
quit;
%put label_stmt=&label_stmt;
/*label_stmt=custID="Customer Id number" Wealth="wealth in bank in USD" education="level of education"*/


proc datasets lib=work nolist;
modify have;
label &label_stmt;
quit;

 
Ksharp
Super User

Using CALL EXECUTE(), your macro variable's max length is 65534. 

Once you have lots of variable label, your code is unable to work.

 

data labels_info_tbl;
infile datalines dlm="," dsd;
length label $100 Var_name $10;
input Var_name $ label;
cards;
custID,Customer Id number
Wealth,wealth in bank in USD
education,level of education 
;
run;


data have;
/*label*/
/*custID= 'Customer Id number'*/
/*Wealth= 'wealth in bank in USD'*/
/*education= 'level of education'*/
/*;*/
input  custID Wealth education;
cards;
111 30000 15
222 7000 12 
333 350000 21
;
Run;

data _null_;
set labels_info_tbl end=last;
if _n_=1 then call execute('proc datasets library=work nodetails nolist;modify have;label ');
call execute(catt(var_name,"='",label,"'"));
if last then call execute(';quit;');
run;


 

Tom
Super User Tom
Super User

Sounds like you want to use DATA from another dataset, not the LABEL attached the variables in that dataset.

 

Converting your metadata data step into code is trivial.

filename code temp;
data _null_;
  set labels_info_tbl;
  length nliteral $65 ;
  nliteral=nliteral(var_name);
  file code;
  put 'label ' nliteral '=' label :$quote. ';' ;
run;

Now you can use %INCLUDE to run those generated LABEL statements where ever you need them.

In your data step to make the dataset.

data have;
%include code / source2 ;
  input  custID Wealth education;
cards;
111 30000 15
222 7000 12 
333 350000 21
;

Or in a PROC DATASETS step to modify the labels in an existing dataset.

proc datasets nolist lib=work;
  modify have;
%include code / source2;
  run;
quit;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 213 views
  • 2 likes
  • 3 in conversation