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;
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;
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
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.