Help using Base SAS procedures

Renaming the variables

Reply
Frequent Contributor
Posts: 124

Renaming the variables

How do I rename the Variables based on the following: I have variables like    MAT___09_20091                                              MAT___09_20092                                                MAT___09_20093 Based on the last character 1 (abc) , 2 (xyz)  and  3 (hgk), I want to rename them as abc_09_2009, xyz_09_2009, and   hgk_09_2009. I may have various variables like this MAT___09_20101, MAT___09_20102 and so on.  Also I want to rename the following field as  MAT____09_2009, MAT____09_2010  as Growth_09_2009, Growth_09_2010 Thanks for any help..

Frequent Contributor
Posts: 124

Re: Renaming the variables

any help???

Frequent Contributor
Posts: 124

Re: Renaming the variables

I tried the following but it does not work.  What I am doing wrong data want;   set sashelp.vcolumn;   where libname='WORK' and memname='TEST';   if substr(name,1,3) = "MAT" and substr(name,7,1) = "_"  then     call execute('proc datasets ;modify TEST;rename  name  = "Growth" || substr(name,7)'); run;

Super User
Posts: 5,099

Re: Renaming the variables

The approach looks quite feasible.  One area where you are encountering trouble is the code being passed through CALL EXECUTE.  In your DATA step, NAME refers to the name of a variable within sashelp.vcolumn.  It should not be in quotes if you want it to be replaced with the NAME's value.  So this string would be closer to what you need:

call execute('proc datasets; modify TEST; rename ' || name || '= Growth' || substr(name,7) || ';');

Good luck.

Frequent Contributor
Posts: 124

Re: Renaming the variables

Thanks Astounding it worked..

Super User
Posts: 5,099

Re: Renaming the variables

Great.  A few tweaks then ...

Your data set WANT does not need to be saved.  You could use data _null_ instead.

Your code is generating many instances of PROC DATASETS.  You could generate just one instead:

if _n_=1 then call execute('proc datasets; modify TEST;');

Then the later IF/THEN statements could generate just the RENAME statements.

Good luck.

Frequent Contributor
Posts: 124

Re: Renaming the variables

Astounding I got error for the following, how can I get rid of that error.  Is there any string function I should be using. data _NULL_;   set sashelp.vcolumn;   where libname='WORK' and memname='TEST';     if substr(name,1,3) = "MAT" and substr(name,14,1) ^= " "  then do;       if substr(name,14,1)  = "1"  then do;      call execute('proc datasets; modify TEST; rename ' || name || '= MA-ABC.GROWTH' || substr(name,6,13) || ';');  end; end; run; I got error for MA-ABC.GROWTH.

Super User
Posts: 5,099

Re: Renaming the variables

Well, names of variables in SAS are restricted to underscores, letters and numbers.  Dashes and periods would not be valid characters for naming a variable.

There are ways around this, but they make future programming very cumbersome.

Ask a Question
Discussion stats
  • 7 replies
  • 245 views
  • 3 likes
  • 2 in conversation