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

Hi Everyone,

 

I am trying to rename all my column names to something that is meaningful to the audience.

My column names vary from demand1-demand288. I would like it to be renamed to

Input Table:

Demand1, Demand2, Demand3, Demand4........Demand288

 

Output Table:

Demand0000, Demand0005,Demand0010.........Demand2355

 

Required:
Demand1 --> Demand0000

Demand2 --> Demand0005
Demand3 --> Demand0010
:

:

Demand12 --> Demand0055
Demand13 --> Demand1000
:

:

Demand288 --> Demand2355

 

Sample Code:

DATA WORK.DEMAND_TEST;;

ARRAY DEMAND [288] DEMAND1 - DEMAND288;

run;

%MACRO TEST_RENAME;

 

DATA WORK.RENAME_TEST;

SET WORK.DEMAND_TEST;

%DO i = 0 %TO 23 %BY 1;

%DO j = 0 %TO 55 %BY 5;

k+1;

CALL SYMPUTX("k" ,k);

 

RENAME DEMAND&k = DEMAND&i&j;

OUTPUT;

%END;

%END;

 

RUN;

%MEND;

 

%TEST_RENAME;

 

Any help is much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
SHAWNCHACKO
Calcite | Level 5

Hi All,

This is what I figured out. I will look at all the other suggested solutions by users.

%MACRO TEST_RENAME;
	DATA WORK.RENAME_TEST;
	SET WORK.DEMAND_TEST;
	%let k=0;
		%DO i = 0 %TO 23 %BY 1;
			%DO j = 0 %TO 55 %BY 5;
				%let i=%sysfunc(putn(&i,Z2));
				%let j=%sysfunc(putn(&j,Z2));
			  	%let k=%eval(&k+1);

				RENAME DEMAND&K = DEMAND&i&j;
			%END;
		%END;
	RUN;
%MEND;
%TEST_RENAME;

 

View solution in original post

3 REPLIES 3
Reeza
Super User

@SHAWNCHACKO wrote:

Hi Everyone,

 

I am trying to rename all my column names to something that is meaningful to the audience.

My column names vary from demand1-demand288. I would like it to be renamed to

Input Table:

Demand1, Demand2, Demand3, Demand4........Demand288

 

Output Table:

Demand0000, Demand0005,Demand0010.........Demand2355

 

Required:
Demand1 --> Demand0000

Demand2 --> Demand0005
Demand3 --> Demand0010
:

:

Demand12 --> Demand0055
Demand13 --> Demand1000
:

:

Demand288 --> Demand2355

 

Sample Code:

DATA WORK.DEMAND_TEST;;

ARRAY DEMAND [288] DEMAND1 - DEMAND288;

run;

%MACRO TEST_RENAME;

 

DATA WORK.RENAME_TEST;

SET WORK.DEMAND_TEST;

%DO i = 0 %TO 23 %BY 1;

%DO j = 0 %TO 55 %BY 5;

k+1;

CALL SYMPUTX("k" ,k);

 

RENAME DEMAND&k = DEMAND&i&j;

OUTPUT;

%END;

%END;

 

RUN;

%MEND;

 

%TEST_RENAME;

 

Any help is much appreciated.



Use the approach in my answer here

 

https://communities.sas.com/t5/SAS-Programming/How-to-rename-a-variable-in-a-dataset-with-value-of-a...

 

You can also use the fact that your new index is essentially (varnum-1)*5

 

 

Kurt_Bremser
Super User

Don't rename, transpose and transform:

proc transpose
  data=have
  out=trans (rename=(col1=demand))
;
var demand:;
run;

data want;
set trans;
*where demand ne .;
length demand_type $4;
demand_type = put(input(substr(_name_,7),best.),z4.);
drop _name_;
run;

Your want dataset will be much easier to work with, and if you uncomment the where, most probably much smaller.

SHAWNCHACKO
Calcite | Level 5

Hi All,

This is what I figured out. I will look at all the other suggested solutions by users.

%MACRO TEST_RENAME;
	DATA WORK.RENAME_TEST;
	SET WORK.DEMAND_TEST;
	%let k=0;
		%DO i = 0 %TO 23 %BY 1;
			%DO j = 0 %TO 55 %BY 5;
				%let i=%sysfunc(putn(&i,Z2));
				%let j=%sysfunc(putn(&j,Z2));
			  	%let k=%eval(&k+1);

				RENAME DEMAND&K = DEMAND&i&j;
			%END;
		%END;
	RUN;
%MEND;
%TEST_RENAME;

 

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
  • 3 replies
  • 1604 views
  • 2 likes
  • 3 in conversation