SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Mscarboncopy
Pyrite | Level 9

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.

4 REPLIES 4
Tom
Super User Tom
Super User

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)))
Mscarboncopy
Pyrite | Level 9

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) .

 

ballardw
Super User

@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.

Mscarboncopy
Pyrite | Level 9

Thank you !

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2971 views
  • 2 likes
  • 3 in conversation