BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

There are some variables that end in "zz". How to rename only those variables by removing "zz" from variable name?

For example Temperaturezz should be renamed to Temperature.

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

And you want to do so for all variables in a specific data set? 

SASPhile
Quartz | Level 8

Yes. Most of the variables end with "zz" so would like to remove zz and those without zz Would like to keep them as is

PeterClemmensen
Tourmaline | Level 20

Ok. Here is one way

 

data test;
input onezz two threezz;
datalines;
1 2 3
;
 
data _null_;
   set sashelp.vcolumn end=lr;
   where libname='WORK' and memname='TEST';
 
   if _n_ = 1 then 
      call execute('proc datasets lib=work nolist; modify test;');
 
   if substr(name,length(name)-1,2)='zz' then
      call execute(compbl(cat('rename ', name, '=', tranwrd(name, 'zz', ''), ';')));
 
   if lr then call execute('quit;');
run;
Tom
Super User Tom
Super User

Looks good, but you probably will want to tighten up the logic.

Make sure test for names that end in ZZ or zZ or Zz also.

lowcase(substr(name,length(name)-1,2))='zz'

Also you don't want to remove the zz from the middle of a name like PIzzazz.

 call execute(catx(' ','rename',name,'=',substr(name,1,length(name)-2)))

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 857 views
  • 1 like
  • 3 in conversation