- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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) .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you !