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

Hi,

 

Is there an easy way to rename all variables in a dataset without having to write their names?

 

Let's say I have variables such as 202001, 202002, 202003 and so on. But I want to rename them all at once so that they are renamed as Jan-20, Feb-20, Mar-20 and so on.  

 

I thought about using an array of variable names to replace the current ones but not sure if that will be easy as in Python or R.

 

What would be the easiest way to tackle this problem?

 

Thank you in advance.

JL

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Ideally you can back up a few steps and avoid this issue....is there a step where you transpose or restructure the data?

If so, in that step you can dynamically specify your names and labels to generate what you need. Usually 202001 is not a valid SAS variable name as well. 

 

The second option, is to clean up after the fact and use the SASHELP.VCOLUMN or dictionary.column table to dynamically generate your rename and label statements. 

 

I would highly suggest the usage of labels instead of variable names such as Jan-20. 

 


@jlee8 wrote:

Hi,

 

Is there an easy way to rename all variables in a dataset without having to write their names?

 

Let's say I have variables such as 202001, 202002, 202003 and so on. But I want to rename them all at once so that they are renamed as Jan-20, Feb-20, Mar-20 and so on.  

 

I thought about using an array of variable names to replace the current ones but not sure if that will be easy as in Python or R.

 

What would be the easiest way to tackle this problem?

 

Thank you in advance.

JL


 

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

Don't keep data (dates) in structure (variable names). Transpose to a long layout, convert the _name_ variable to a SAS date, and assign a display format to your taste.

 

If you want code for that, supply example data in usable form (data step with datalines). Use the the "little running man" button to post the code.

Reeza
Super User

Ideally you can back up a few steps and avoid this issue....is there a step where you transpose or restructure the data?

If so, in that step you can dynamically specify your names and labels to generate what you need. Usually 202001 is not a valid SAS variable name as well. 

 

The second option, is to clean up after the fact and use the SASHELP.VCOLUMN or dictionary.column table to dynamically generate your rename and label statements. 

 

I would highly suggest the usage of labels instead of variable names such as Jan-20. 

 


@jlee8 wrote:

Hi,

 

Is there an easy way to rename all variables in a dataset without having to write their names?

 

Let's say I have variables such as 202001, 202002, 202003 and so on. But I want to rename them all at once so that they are renamed as Jan-20, Feb-20, Mar-20 and so on.  

 

I thought about using an array of variable names to replace the current ones but not sure if that will be easy as in Python or R.

 

What would be the easiest way to tackle this problem?

 

Thank you in advance.

JL


 

Shmuel
Garnet | Level 18

Are you importing data from excel with first line containing those dates as  labels 

to be treated in sas as variable names?

Can you post a sample of your data in a data step with datalines?

 

 

PaigeMiller
Diamond | Level 26

Agreeing with @Kurt_Bremser and @Reeza , don't structure your data like this.


Even in the case suggested by @Shmuel where you receive an Excel file with this poor structure, you should make the data set long with the dates as a value of a variable (which is much easier to handle in SAS code) instead of modifying the variable names to contain Jan or Feb or the like.

 

 

--
Paige Miller
Ksharp
Super User

202001, 202002, 202003 are not valid sas variable name .

 

options validvarname=any;
data have;
input  '202001'n '202002'n '202003'n;
cards;
1 2 3
;


proc transpose data=have(obs=0) out=temp;
var _all_;
run;
data temp;
 set temp;
 name=put(input(_name_,yymmn6.),monyy7.)	;
run;
proc sql noprint;
select catx('=',nliteral(_name_),name) into :rename separated by ' ' from temp;
quit;
proc datasets library=work nolist nodetails;
modify have;
rename &rename;
quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 669 views
  • 0 likes
  • 6 in conversation