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 ;
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;
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;
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_'?
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.