Help using Base SAS procedures

Data set column rename

Reply
New Contributor
Posts: 2

Data set column rename

Hi,

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: 11,343

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

Hello;

This is one of way

%macro VarName(stuffin,indx);

    proc datasets library=work nolist;

   modify YOUR_DATASETNAME;

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

   format var&indx $8.;

  run;

%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||')');

   %end;

%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: 7,863

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
Ask a Question
Discussion stats
  • 3 replies
  • 323 views
  • 0 likes
  • 4 in conversation