Help using Base SAS procedures

Data set column rename

New Contributor
Posts: 2

Data set column rename


I have a list of column name in table temp --> var1, var2, var3, ..., var200.

I was able to rename var1 to studentID, var2 to studentGender. I would like to rename var3 to 2001-01, var4 to 2001-02, ... var13 to 2001-11, var14 to 2001-12, var15 to 2002-01, var16 to 2002-02, and ect. Is there a way to create those name in a do loop?

Many thanks!

Super User
Posts: 13,583

Re: Data set column rename

Please post questions in only one section of the forum unless recommended to a more appropriate location.

New Contributor
Posts: 2

Re: Data set column rename


This is one of way

%macro VarName(stuffin,indx);

    proc datasets library=work nolist;


   rename var&indx="&stuffin"; ( which is Oldname = Newname)

   format var&indx $8.;


%mend VarName;

/* call the macro that will create the labels */

%macro loopin();

   %local i;

   attrib thename length=$8;

   %do i=3 %to &num_month;  (loop to maximum number of the column you have)

     thename=put(DateVariable,yymmd.);  /* help on format SAS(R) 9.3 Formats and Informats: Reference*/

  call execute('%VarName('|| thename||"," ||&i||')');


%mend loopin;

We have used this for the labeling the column for the different year and month. I have modified little bit for you for the rename. If you have any problem let me.

Super User
Posts: 10,280

Re: Data set column rename

Be advised that names like 2002-01 require the validvarname=any system option and the '2002-01'n naming construct, which will be a PITA to maintain further on.

Consider to use _2002_01 or the like.

As a programmer, I positively hate unnecessary typing.

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation