- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
They all have labels. I want to change the labels by adding 3 strings : for example, for all variables with start var then add VAR at the beginning of the labels if num1 etc all variables with start num then add NUM at the beginning of the all labels. How can I do that? Thank you.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
FROM dictionary.columns
above
Where libname = 'WORK'
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug
"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings
SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation