Hi
I need to rename a lot of variables that share part of a name, s seen in the example below.
The Xs here are all different words for each variable name in the data set, they do not match.
The renaming would be only adding a number to the first part of the common (shared) name.
ppa50XXXXXX
and I need them all to be
pp02a50XXXXXX
I did not want to do an array, if there is a more simple way to do this, without having to list each single variable.
Thank you in advance.
You will need to list all of the variables. But you can use a program to make the list for you.
If the list is short (less than 1000 variables) then you can just put the list into a single macro variable (limit 64K bytes).
Say you have a dataset named WORK.HAVE that you want to rename the variables for. First get the list of variables, find the ones that match your pattern, generate the new name and then produce a string of OLD=NEW pairs that can be used in a rename statement (or rename= dataset option).
proc contents data=work.have out=contents noprint ; run;
proc sql noprint;
select catx('=',name,'ppa02a50'||substr(name,6))
into :renames separated by ' '
from contents
where upcase(name) like 'PPA50%'
;
quit;
proc datasets nolist lib=WORK;
modify have ;
rename &renames;
run;
quit;
If your dataset has non-standard names (allowed when VALIDVARNAME option is set to ANY) then include calls to the NLITERAL() function.
catx('=',nliteral(name),nliteral('ppa02a50'||substr(name,6)))
Hi Tom,
Thank you. I do have more than 1.000 variables but I could subset the files.
I will try doing that and see what I get. I have used macro variables before so I should be able to do what you suggested (put the list into a single macro variable) .
@Mscarboncopy wrote:
Hi
I need to rename a lot of variables that share part of a name, s seen in the example below.
The Xs here are all different words for each variable name in the data set, they do not match.
The renaming would be only adding a number to the first part of the common (shared) name.
ppa50XXXXXX
and I need them all to be
pp02a50XXXXXX
I did not want to do an array, if there is a more simple way to do this, without having to list each single variable.
Thank you in advance.
Before you turn any automated renaming process loose on variables when the new names would be longer than the old ones you should verify that none of the lengths of the current variable names plus the count of new characters would exceed 32 characters.
Thank you !
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.