Hello SAS Community,
I have dozens and dozens of variables that are named _2001_count, _2002_count, 2003_count, _2004_count, etc that I would like to rename to something like count_2001, count_2002, count_2003 etc. so I can work with them more easily. I don't want to have to type them all out each time I need to manipulate a variable and want them renamed so I can easily make variable lists. I have read several posts including https://communities.sas.com/t5/SAS-Programming/Rename-all-variables-starting-with-the-same-preffix-t... but my situation is slightly different. I am using base SAS 9.4.
Thank you for any help!
You can adapt the answers in the thread you mentioned, e.g.:
data have ;
call missing(_2001_count,_2002_count) ;
run ;
proc sql noprint;
select cats(name,'=',cats("count",substr(name,1,5))) into :renames separated by " "
from dictionary.columns
where libname="WORK"
and memname="HAVE"
and name eqt "_20"
;
quit;
%put >>&renames<< ;
proc datasets library=work nolist;
modify have;
rename &renames;
quit;
Or probably better would be to transpose your data into a "long skinny" format, where instead of have 10 Count variables for 10 years, you make a single variable named Year and and single variable for Count, and have one row per year.
You can adapt the answers in the thread you mentioned, e.g.:
data have ;
call missing(_2001_count,_2002_count) ;
run ;
proc sql noprint;
select cats(name,'=',cats("count",substr(name,1,5))) into :renames separated by " "
from dictionary.columns
where libname="WORK"
and memname="HAVE"
and name eqt "_20"
;
quit;
%put >>&renames<< ;
proc datasets library=work nolist;
modify have;
rename &renames;
quit;
Or probably better would be to transpose your data into a "long skinny" format, where instead of have 10 Count variables for 10 years, you make a single variable named Year and and single variable for Count, and have one row per year.
Thank you. This worked. I do transpose the data from wide to long in the very next step. I just wanted to create a summary variable (summing all 40+ variables across each year variable) before transposing. Thank you again!
I don't want to have to type them all out each time I need to manipulate a variable and want them renamed so I can easily make variable lists.
Use the long format, as mentioned. Make your variable named YEAR and then the value of the variable is the actual year number, and that's even easier to handle and much less typing.
Transpose your wide dataset to a long layout and extract the year in a following DATA step from _NAME_.
Maxim 19: Long Beats Wide. Do not keep data (e.g. years) in structure (variable names).
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.