If the number of variables is small (or more accurately the total length of all of the new labels is small) enough to fit into a macro variable just use an SQL query to generate the code needed.
proc sql noprint;
select catx('=',nliteral(name),quote(catx(' ',substr(name,1,3),label),"'"))
into :labels separated by ' '
where libname = 'WORK'
and memname='HAVE'
and upcase(substr(name,1,3)) in ('VAR','NUM')
and label ne ' '
;
quit;
proc datasets nolist lib=work;
modify have ;
label &labels;
run;
quit;
If the number of variables is small (or more accurately the total length of all of the new labels is small) enough to fit into a macro variable just use an SQL query to generate the code needed.
proc sql noprint;
select catx('=',nliteral(name),quote(catx(' ',substr(name,1,3),label),"'"))
into :labels separated by ' '
where libname = 'WORK'
and memname='HAVE'
and upcase(substr(name,1,3)) in ('VAR','NUM')
and label ne ' '
;
quit;
proc datasets nolist lib=work;
modify have ;
label &labels;
run;
quit;
Hi @Emma2021
One wat to get around the potential "too long" scenario would be writing the code into a separate file, and then running the newly created file 😉
Basically take @Tom original code and re-arrange it slightly
filename dyncode temp;
data _null_;
length str $300;
file dyncode lrecle=300;
put 'proc datasets nolist lib=work;' ;
put +3 'modify have ;' ;
put +3 'label ';
do until(eof);
SET sashelp.vcolumn(where=(libname='WORK' and memname='HAVE' and upcase(substr(name,1,3)) in ('VAR','NUM') and label ne ' '))
end=eof;
str = catx('=',nliteral(name),quote(catx(' ',substr(name,1,3),label),"'"));
put +6 str;
end;
put +3 ';';
put 'run; quit; ';
run;
%include dyncode;
The generated code should be a single Proc Datasets step.
Hope this help
You already have a solution, but just for fun, an alternative one with proc transpose and call execute():
data have;
retain var1 - var78 "abc" num1 - num20 0 A B C D E F G "xxx";
ATTRIB
var1 -- var78 label= "some label"
num1 -- num20 label= "other lable"
A -- G label= 'different variables'
;
run;
proc transpose data=have(obs=0) out=temp;
var var: num:;
run;
data _null_;
call execute('
proc datasets nolist lib=work;
modify have ;
label');
do until(eof);
set temp end=eof;
call execute(cat(_NAME_,"='",substr(_NAME_,1,3)," ",_LABEL_,"'"));
end;
call execute(';run;quit;');
stop;
run;
Bart
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!
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.