BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ItWorked
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

4 REPLIES 4
Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
ItWorked
Calcite | Level 5

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!

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Kurt_Bremser
Super User

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

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


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
  • 379 views
  • 2 likes
  • 4 in conversation