BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
buechler66
Barite | Level 11

Hi. Is there a way to preface every variable name in my dataset with a fixed string?  For instance I would like to append 'a_' in front of all variable in my dataset regardless of how many variable make up the dataset.

 

Any ideas would be greatly appreciated. 

 

In the end I would need the variable names in the dataset to be a_name and a_dept.

 

data person;
   infile datalines delimiter=','; 
   input name $ dept $;
   datalines;                      
John,Sales
Mary,Acctng
;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19
data person;
   infile datalines delimiter=','; 
   input name $ dept $;
   datalines;                      
John,Sales
Mary,Acctng
;;;;
   run;
*Make a list of names;
proc transpose data=person(obs=0) out=varnames;
   var _all_;
   run;
*Gen the name=newname value pairs;
proc sql noprint;
   select catx('=',nliteral(_name_),nliteral(cats('A_',_name_))) 
      into :renamelist separated by ' '
      from varnames;
   quit;
   run;
%put NOTE: &=renamelist;
*Modify person;
proc datasets;
   modify person;
   rename &renamelist;
   run;
   contents data=person;
   run;
   quit;

View solution in original post

5 REPLIES 5
data_null__
Jade | Level 19
data person;
   infile datalines delimiter=','; 
   input name $ dept $;
   datalines;                      
John,Sales
Mary,Acctng
;;;;
   run;
*Make a list of names;
proc transpose data=person(obs=0) out=varnames;
   var _all_;
   run;
*Gen the name=newname value pairs;
proc sql noprint;
   select catx('=',nliteral(_name_),nliteral(cats('A_',_name_))) 
      into :renamelist separated by ' '
      from varnames;
   quit;
   run;
%put NOTE: &=renamelist;
*Modify person;
proc datasets;
   modify person;
   rename &renamelist;
   run;
   contents data=person;
   run;
   quit;
buechler66
Barite | Level 11

Awesome help!  Ty!  Any thoughts on handling a variable name that is already 31 or 32 chars long?  In this case I'm getting the error:

 

1018     rename &renamelist;
ERROR: "A_A_PC_SCN_BEF_UNLD_SCAN_EXCL_IND" contains more than 32 characters.
ERROR: "A_A_BMEU_PC_SCAN_BEF_AET_EXCL_IND" contains more than 32 characters.
ERROR: "A_A_INVLD_CTR_LVL_ENT_FAC_EXCL_IND" contains more than 32 characters.
ERROR: "A_A_DSTNC_ORGN_FAC_DESTN_PLNT_ZIP" contains more than 32 characters.
ERROR: "A_A_DSTNC_ORGN_ZIP_DESTN_PLNT_ZIP" contains more than 32 characters.

Would there be anyway to just have it truncate the last to variable characters to make room for the 'a_'?

ballardw
Super User

This will TRUNCATE the rename to 32 characters

proc sql noprint;
   select (catx('=',nliteral(_name_),nliteral(substr(cats('A_',_name_),1,32)))) 
      into :renamelist separated by ' '
      from varnames;
   quit;
run;

However if you have any names where the last few characters are critical then you will likely want to pass the VARNAMES data set through a data step. I would sort by the Name to get similar names in order an selectively replace some text to abbreviate but you would have to make those decisions based on what you know of your data. Possibly something like further abbreviating EXCL to EX or ZIP to ZP.

 

ballardw
Super User

Do any of your existing variables have names of 31 characters or longer length? If so you may have to decide what to do with the prefixing.

 

Suppose you have two variables named:

A_Somewhat_longwinded_variableA

A_Somewhat_longwinded_variableB

These are  31 characters long. If you prefix them with A_ the length would become 33 characters and exceed the length of a valid variable name by 1. ALSO they would be identical for the 32 characters allowed. SAS datasets will not allow two or more variables with the same name. So you may have to provide some additional coding.

 

If you never have variables that exceed 30 characters you are okay but if you start using longer prefixes the likelihood of encountering this name length/ duplication issue increases.

 

buechler66
Barite | Level 11
Yes!!! You anticipated my very next problem!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 9209 views
  • 6 likes
  • 3 in conversation