BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

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

 

4 REPLIES 4
ballardw
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;
   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

chaatak
Fluorite | Level 6
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;
mkeintz
PROC Star

@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

--------------------------

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 11279 views
  • 7 likes
  • 5 in conversation