DATA Step, Macro, Functions and more

Appending a string to all variable names

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Appending a string to all variable names

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
;

 

 


Accepted Solutions
Solution
‎09-14-2016 03:14 PM
Respected Advisor
Posts: 3,799

Re: Appending a string to all variable names

Posted in reply to buechler66
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


All Replies
Solution
‎09-14-2016 03:14 PM
Respected Advisor
Posts: 3,799

Re: Appending a string to all variable names

Posted in reply to buechler66
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;
Regular Contributor
Posts: 212

Re: Appending a string to all variable names

Posted in reply to data_null__

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_'?

Super User
Posts: 11,343

Re: Appending a string to all variable names

Posted in reply to buechler66

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.

 

Super User
Posts: 11,343

Re: Appending a string to all variable names

Posted in reply to buechler66

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.

 

Regular Contributor
Posts: 212

Re: Appending a string to all variable names

Yes!!! You anticipated my very next problem!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 834 views
  • 3 likes
  • 3 in conversation