DATA Step, Macro, Functions and more

Rename all variables by adding a term at the end

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Rename all variables by adding a term at the end

Hi Everyone,

 

I have 2 dataset that have similar variables. One file is for daily and the other file is for monthly.

I want to add a term say "_daily" after all variables in daily file and "_monthly" for those in monthly file.

Is there any way to do so instead of using the rename one by one?

 

Thank you.

 

HC

 

data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;

 


Accepted Solutions
Solution
‎01-13-2016 08:52 PM
Super User
Posts: 9,687

Re: Rename all variables by adding a term at the end

data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;


data _null_;
 set sashelp.vcolumn(where=(libname='WORK' and memname='HAVE') keep=libname memname name) end=last;
 if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify have;rename');
 call execute(cats(name,'=',name,'_daily'));
 if last then call execute(';quit;');
run;

View solution in original post


All Replies
Super User
Super User
Posts: 6,502

Re: Rename all variables by adding a term at the end

Use metadata (SASHELP.VCOLUMN or DICTIONARY.COLUMNS or output of PROC CONTENTS) to generate the rename code.

proc sql noprint ;
  select catx('=',name,cats(name,'_monthly'))
    into :monthly separated by ' '
    from dictionary.columns
    where libname='WORK'
      and memname='MONTHLY'
  ;
quit;
data want ;
   set monthly (rename=(&monthly)) ;
run;
Super User
Posts: 17,899

Re: Rename all variables by adding a term at the end

Slight modification to Tom's code - I would suggest using proc datasets to modify the variable names instead. 

If you're merging the files though, do it in that step instead of a separate rename step. 

Solution
‎01-13-2016 08:52 PM
Super User
Posts: 9,687

Re: Rename all variables by adding a term at the end

data have;
input time var1x var3;
datalines;
1 1 1
2 3 5
1 2 3
;
run;


data _null_;
 set sashelp.vcolumn(where=(libname='WORK' and memname='HAVE') keep=libname memname name) end=last;
 if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify have;rename');
 call execute(cats(name,'=',name,'_daily'));
 if last then call execute(';quit;');
run;
Contributor
Posts: 67

Re: Rename all variables by adding a term at the end

Hi,

I used this code and it worked successfully. But before this step I want to remove "_" from all my variable names - is there a quick way? I tried using compress with your code but failed. 

Thank you.

Super User
Super User
Posts: 6,502

Re: Rename all variables by adding a term at the end

The same steps should apply.  Try just running the part that calculates the new variable name from the old name in a data step so you can look at the result and figure what is happening.

 

new_name = compress(name,'_');
Contributor
Posts: 67

Re: Rename all variables by adding a term at the end

Thank you, now need a step to keep the ones without '_' as is. My data has a mix of vars with '_' and some without.
Super User
Posts: 9,687

Re: Rename all variables by adding a term at the end

if not findc(name,'_') then  call execute(cats(name,'=',name,'_daily'));
Contributor
Posts: 67

Re: Rename all variables by adding a term at the end

[ Edited ]

Thank you can I apply the same logic or code to change the labels for all my variables - i mean i want to add a term before the label name for all vars:

 

something like this:

PROC CONTENTS DATA=sat1 OUT=tmp_sat2;
RUN;
data _null_;
set WORK.tmp_SAT2(where=(libname='WORK' and memname='SAT1') keep=libname memname label) end=last;
if _n_ eq 1 then call execute('proc datasets library=work nolist nodetails;modify SAT1;LABEL');
call execute(cats(label,'=','SAT_',label));
if last then call execute(';quit;');
run;

 

Your feedback is highly appreciated

Super User
Posts: 9,687

Re: Rename all variables by adding a term at the end

Sure. Your code looks good .
Contributor
Posts: 67

Re: Rename all variables by adding a term at the end

Hi,

Its giving me errors - Variable not found!

 

Help

Super User
Posts: 9,687

Re: Rename all variables by adding a term at the end

Can you post the content of table tmp_SAT2 , and in call execute(cats(label,'=','SAT_',label)); you use the same variable name at the both side, That doesn't look right.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 370 views
  • 5 likes
  • 5 in conversation