BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

I have one dataset with a list of variables (let's say, AA, BB, CC, .... ZZ). I also have another dataset that includes two columns, one for the list of variables in the former set and the other for their definitions. So, the second set looks like...

 

VARIABLE_NAME   DEFINITION

AA                            Definition of AA

AA                            Definition of BB

AA                            Definition of CC

...

AA                            Definition of ZZ

 

I was wondering if I label variables in the first set using the second set's definition list. Thanks!

 

5 REPLIES 5
gamotte
Rhodochrosite | Level 12

Hello,

 

Try as follows.

 

data have;
input A B C;
cards;
1 2 3
4 5 6
;
run;

data labels;
input var $ label $;
cards;
A label_A
B label_B
C label_C
;
run;

data _NULL_;
set labels end=fend;

if _N_=1 then call execute('data want; set have; label ');

call execute(cats(var,'="',label,'"'));

if fend then call execute('; run;');
run;
PeterClemmensen
Tourmaline | Level 20

Alternatively

 

data one;
input VARIABLE_NAME $ DEFINITION :$20.;
infile datalines dlm=',';
datalines;
AA,Definition of AA
BB,Definition of BB
CC,Definition of CC
;

data two;
AA=1;BB=2;CC=3;
run;

proc sql noprint;
    select cats(VARIABLE_NAME, "='", DEFINITION, "'") into :label separated by ' '
    from one;
run;

%put &label.;

data want;
    set two;
    label &label.;
run;

proc print data=want label;
run;
FreelanceReinh
Jade | Level 19

Or, with either of the two suggested techniques, use PROC DATASETS to assign the labels (and thus avoid potentially time-consuming read and write operations for a large dataset).

 

Example with CALL EXECUTE:

data labelds;
input variable_name $ definition :&$40.;
cards;
AA  First label
BB  Second label
;

data have;
AA=1;
BB='X';
run;

data _null_;
if _n_=1 then call execute('proc datasets lib=work nolist; modify have; label');
set labelds end=last;
call execute(variable_name||'='||definition);
if last then call execute('; quit;'); 
run;
Reeza
Super User

https://gist.github.com/statgeek/f18931085f6a0009185c

 

*Create label data set;
data label_data_set;
length name label $25.;
name="Sex"; label="Gender"; output;
name="height"; label="Height (in)"; output;
name="weight"; label="Weight (lbs)"; output;
run;
 
 
*Create sample dataset to apply label;
data class;
set sashelp.class;
run;
 
 
*Create macro variable that holds label statement;
proc sql noprint;
select catx("=", name, quote(trim(label)))
  into :label_list separated by " "
from label_data_set;
quit;
 
 
*Display macro variable in log;
%put &label_list.;
 
 
*Apply labels without recreating dataset;
proc datasets library=work;
modify class;
label &label_list.;
run;quit;
 
 
*Print the dataset to display new labels;
proc print data=class label noobs;
run;
ballardw
Super User

And another option is to create a program file with the label statements and %include it where wanted instead of permanently changing the labels:

Something like:

data _null_;
   file "c:\folder\subfolder\labelprog.sas";
   set yourdataset;
   str=catx(' ','Label',variable_name,"=",quote(definition),";");
   put str;
run;

proc print data=yourotherdata;
   %include "c:\folder\subfolder\labelprog.sas";
run;

Change your data set names and paths as needed.

This has minor advantage of you can example the %include file with a text editor and modify any label statement if needed. Or you want to make a slightly modified set of labels for some reason.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1072 views
  • 12 likes
  • 6 in conversation