Wednesday, 7 August 2019

Rename all variable all at once

Some times you may need to rename all data-set variable all at once, depending on various requirement like merge two data-set with same variable.

Below code can be used to  rename data-set variable all at once.

Suppose both data set have same variable name like VAR1, VAR2 ........

and you want to still have all variable in Final dataset so

First we will selecting all variable name from DS2 except one ID variable (if needed).
create a macro variable renamelist which will have value like below.

VAR1=VAR1_DS1 , VAR2=VAR2_DS1


proc sql noprint;
select trim(name) || '=' || 'DS1' || name
into :renamelist separated by ' ' from
 (select name
 from dictionary.columns
 where libname='WORK' and memname='DS1' and upcase(name) in
 (select upcase(name)
 from dictionary.columns
 where libname='WORK' and memname='DS2' and upcase(name) ne 'ID'));
quit;

%put &renamelist;


and we can pass renamelist macro variable in below merge statement

data final;
merge ds1 (&renamelist) ds2 ;
by id;
run ;

1 comment:

  1. Thanks for sharing rename for all variable article and i like your post visit my Bedroom furniture store in Jaipur.

    ReplyDelete