BookmarkSubscribeRSS Feed
Barite | Level 11



I need help on how to get a variable label name in to macro variable.


data one;
input age Ageu $;
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


Super User

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;
   label x ='Some label';

data _null_;
   set example (obs=1);
   labeltext= vlabel(x);
   call symputx('xlabel',labeltext);

%put label for x is: &xlabel.;

Or extract the value from sashelp.vcolumn view  or the Dictionary.columns table in Proc SQL using :INTO

Fluorite | Level 6
data one;
 input age Ageu $;
 attrib age label="Age in Years";
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 = ;

%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.


The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets



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
  • 4 replies
  • 5 in conversation