Dear,
I need help on how to get a variable label name in to macro variable.
data one;
input age Ageu $;
datalines;
12 Y
;
Suppose the age variable label is 'age in years'. I need to create a macro variable with label value.
%let want=;
output needed;
want='age in years'.
Please suggest. Thank you
So where are we suppose to "get" the variable label from? The data set you create has no label assigned so there isn't any place to get the label from.
If you SET from a data set that has an assigned label you can use the vlabel function in a data step.
data example; x=3; label x ='Some label'; run; data _null_; set example (obs=1); labeltext= vlabel(x); call symputx('xlabel',labeltext); run; %put label for x is: &xlabel.;
Or extract the value from sashelp.vcolumn view or the Dictionary.columns table in Proc SQL using :INTO
Try the VARLABEL Function
data one;
input age Ageu $;
attrib age label="Age in Years";
datalines;
12 Y
;
proc print data = one label;
data _NULL_;
set one;
file print;
length lab $50.;
call label (age, lab);
call symput ('want', lab);
put lab = ;
run;
%put &want;
@chaatak :
Good advice, but I would tweak your program just a little. If the only goal is to retrieve and report the label of a variable, then there is no need to read every observation (or any observation at all) from data set ONE. After all, if ONE had one million observations, there is no need to issue the CALL label and CALL symput 1mm times. Yes, in your sample dataset, this not a problem.
But as a defensive measure you could just change the "set one:" statement to:
set one (obs=1);
I did say you don't even have to read one observation, because another option is:
if 0 then set one;
For this task, it won't be any more efficient than the "(obs=1)" approach, but it's a good device for reminder yourself that this data step is about meta-data, not data.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.