<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Rename all variables. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374935#M65313</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to rename all variables of dataset thanks to another dataset with the same structure.&lt;/P&gt;&lt;P&gt;I used the macro from Prasad Ravi (: &lt;A href="http://www2.sas.com/proceedings/sugi28/118-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/118-28.pdf&lt;/A&gt;) with some modification but it doesn't work.&lt;/P&gt;&lt;P&gt;I took a simple exemple to illustrate what i want to achieve.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data one;
 U=1; 
V=2; 
X=3; 
Y=4; 
Z=5; 
Run; 
Data two;
 a=25; 
b=265; 
c=346; 
d=454; 
e=54; 
Run; &lt;BR /&gt;Data want;&lt;BR /&gt; U=25; &lt;BR /&gt;V=265; &lt;BR /&gt;X=346; &lt;BR /&gt;Y=454; &lt;BR /&gt;Z=54; &lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;

options macrogen mprint mlogic; 
%macro rename(lib,dsn,dsn1); 
options pageno=1 nodate;
 proc contents data=&amp;amp;lib..&amp;amp;dsn;
 title "Before Renaming All Variables"; 
run;
proc sql noprint;
select nvar into :num_vars
from dictionary.tables
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"; 

proc sql noprint;
select nvar into :num_vars1
from dictionary.tables
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN1"; 

select distinct(name) into :var1-
:var%TRIM(%LEFT(&amp;amp;num_vars))
from dictionary.columns
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"; 
quit;
run;

select distinct(name) into :varia1-
:varia%TRIM(%LEFT(&amp;amp;num_vars1))
from dictionary.columns
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN1"; 
quit;
run;


proc datasets library=&amp;amp;LIB;
modify &amp;amp;dsn1;
rename
%do i=1 %to &amp;amp;num_vars1;
 &amp;amp;&amp;amp;varia&amp;amp;i=&amp;amp;&amp;amp;var&amp;amp;i.
%end; 
;
quit;
run;
options pageno=1 nodate;
 proc contents data=&amp;amp;lib..&amp;amp;dsn;
 title "After Renaming All Variables"; 
run;
%mend rename;
 %rename(WORK,ONE,TWO);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jul 2017 13:15:07 GMT</pubDate>
    <dc:creator>adil256</dc:creator>
    <dc:date>2017-07-11T13:15:07Z</dc:date>
    <item>
      <title>Rename all variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374935#M65313</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to rename all variables of dataset thanks to another dataset with the same structure.&lt;/P&gt;&lt;P&gt;I used the macro from Prasad Ravi (: &lt;A href="http://www2.sas.com/proceedings/sugi28/118-28.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi28/118-28.pdf&lt;/A&gt;) with some modification but it doesn't work.&lt;/P&gt;&lt;P&gt;I took a simple exemple to illustrate what i want to achieve.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data one;
 U=1; 
V=2; 
X=3; 
Y=4; 
Z=5; 
Run; 
Data two;
 a=25; 
b=265; 
c=346; 
d=454; 
e=54; 
Run; &lt;BR /&gt;Data want;&lt;BR /&gt; U=25; &lt;BR /&gt;V=265; &lt;BR /&gt;X=346; &lt;BR /&gt;Y=454; &lt;BR /&gt;Z=54; &lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;

options macrogen mprint mlogic; 
%macro rename(lib,dsn,dsn1); 
options pageno=1 nodate;
 proc contents data=&amp;amp;lib..&amp;amp;dsn;
 title "Before Renaming All Variables"; 
run;
proc sql noprint;
select nvar into :num_vars
from dictionary.tables
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"; 

proc sql noprint;
select nvar into :num_vars1
from dictionary.tables
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN1"; 

select distinct(name) into :var1-
:var%TRIM(%LEFT(&amp;amp;num_vars))
from dictionary.columns
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"; 
quit;
run;

select distinct(name) into :varia1-
:varia%TRIM(%LEFT(&amp;amp;num_vars1))
from dictionary.columns
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN1"; 
quit;
run;


proc datasets library=&amp;amp;LIB;
modify &amp;amp;dsn1;
rename
%do i=1 %to &amp;amp;num_vars1;
 &amp;amp;&amp;amp;varia&amp;amp;i=&amp;amp;&amp;amp;var&amp;amp;i.
%end; 
;
quit;
run;
options pageno=1 nodate;
 proc contents data=&amp;amp;lib..&amp;amp;dsn;
 title "After Renaming All Variables"; 
run;
%mend rename;
 %rename(WORK,ONE,TWO);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2017 13:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374935#M65313</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2017-07-11T13:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: Rename all variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374944#M65314</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data one;
 U=1; 
V=2; 
X=3; 
Y=4; 
Z=5; 
Run; 
Data two;
 a=25; 
b=265; 
c=346; 
d=454; 
e=54; 
Run;

proc transpose data=one(obs=0) out=t1;
var _all_;
run;
proc transpose data=two(obs=0) out=t2;
var _all_;
run;
data rename;
 merge t1 t2(rename=(_name_=name));
run;

data _null_;
 set rename end=last;
 if _n_=1 then call execute('proc datasets library=work nolist nodetails;modify two; rename ');
 call execute(catt(name,'=',_name_));
 if last then call execute(';quit;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jul 2017 13:28:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374944#M65314</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-07-11T13:28:00Z</dc:date>
    </item>
    <item>
      <title>Re: Rename all variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374970#M65316</link>
      <description>&lt;P&gt;Thank you very much. it works perfectly. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I found my mistake in the previous code but I thinking I'm going to choose your method.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;corrected code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options macrogen mprint mlogic; 
%macro rename(lib,dsn,dsn1); 
options pageno=1 nodate;
 proc contents data=&amp;amp;lib..&amp;amp;dsn;
 title "Before Renaming All Variables"; 
run;
proc sql noprint;
select nvar into :num_vars
from dictionary.tables
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"; 
select distinct(name) into :var1-
:var%TRIM(%LEFT(&amp;amp;num_vars))
from dictionary.columns
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN"; 
quit;
run;

proc sql noprint;
select nvar into :num_vars1
from dictionary.tables
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN1"; 
select distinct(name) into :vari1-
:vari%TRIM(%LEFT(&amp;amp;num_vars))
from dictionary.columns
where libname="&amp;amp;LIB" and
memname="&amp;amp;DSN1"; 
quit;
run;




proc datasets library=&amp;amp;LIB;
modify &amp;amp;DSN1;
rename
%do i=1 %to &amp;amp;num_vars;
 &amp;amp;&amp;amp;vari&amp;amp;i=&amp;amp;&amp;amp;var&amp;amp;i.
%end; 
;
quit;
run;
options pageno=1 nodate;
 proc contents data=&amp;amp;lib..&amp;amp;dsn;
 title "After Renaming All Variables"; 
run;
%mend rename;
 %rename(WORK,ONE,TWO);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jul 2017 14:05:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rename-all-variables/m-p/374970#M65316</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2017-07-11T14:05:02Z</dc:date>
    </item>
  </channel>
</rss>

