<?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 Re: Rename variable with macro in two datasets in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Rename-variable-with-macro-in-two-datasets/m-p/80274#M23119</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You will need to either add an "order by varnum" statement into your sql code or get rid off "distinct" to have it return the variables in the order they are in the data set.&lt;/P&gt;&lt;P&gt;The same goes for data set X where the variables in it happened to be in alphabetical order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* got rid off distinct */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select name into :var1-&lt;/P&gt;&lt;P&gt;:var%TRIM(%LEFT(&amp;amp;num_vars))&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname="WORK" and&lt;/P&gt;&lt;P&gt;memname="L2"&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* added order by varnum */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select distinct(name) into :var1-&lt;/P&gt;&lt;P&gt;:var%TRIM(%LEFT(&amp;amp;num_vars))&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname="WORK" and&lt;/P&gt;&lt;P&gt;memname="L2"&lt;/P&gt;&lt;P&gt;order by varnum&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 12 Jun 2012 12:50:39 GMT</pubDate>
    <dc:creator>Alpay</dc:creator>
    <dc:date>2012-06-12T12:50:39Z</dc:date>
    <item>
      <title>Rename variable with macro in two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rename-variable-with-macro-in-two-datasets/m-p/80273#M23118</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;tt&lt;BR /&gt;I want to Rename the variable names by using macro from dataset l2 and dataset x variable&lt;BR /&gt;names should change in the order they are but when u do the folloiwng macro&lt;BR /&gt;the renaming is happening but in different order,in the macro it was sorting and changing &lt;BR /&gt;the order how can i do it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Log:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;NOTE: Renaming variable v to b2.&lt;BR /&gt;NOTE: Renaming variable x2 to s3.&lt;BR /&gt;NOTE: Renaming variable z3 to z.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;But it should be :&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;varible v to z&lt;BR /&gt;varible x2 to b2&lt;BR /&gt;varible z3 to s3&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data l2;&lt;BR /&gt;input z b2 s3;&lt;BR /&gt;cards;&lt;BR /&gt;1 2 3&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data x;&lt;BR /&gt;input v x2 z3;&lt;BR /&gt;cards;&lt;BR /&gt;1 2 3&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select nvar into :num_vars&lt;BR /&gt;from dictionary.tables&lt;BR /&gt;where libname="WORK" and&lt;BR /&gt;memname="L2";&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select distinct(name) into :var1-&lt;BR /&gt;:var%TRIM(%LEFT(&amp;amp;num_vars))&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname="WORK" and&lt;BR /&gt;memname="L2";&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;%put &amp;amp;num_vars;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select nvar into :s2num_vars&lt;BR /&gt;from dictionary.tables&lt;BR /&gt;where libname="WORK" and&lt;BR /&gt;memname="X";&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select distinct(name) into :Svar1-&lt;BR /&gt;:Svar%TRIM(%LEFT(&amp;amp;s2num_vars))&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where libname="WORK" and&lt;BR /&gt;memname="X";&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;%put &amp;amp;s2num_vars;&lt;/P&gt;&lt;P&gt;%MACRO RENAME(DSN=);&lt;BR /&gt;proc datasets library=WORK;&lt;BR /&gt;modify &amp;amp;DSN;&lt;BR /&gt;rename&lt;BR /&gt;%do i=1 %to &amp;amp;num_vars;&lt;BR /&gt;&amp;amp;&amp;amp;Svar&amp;amp;i=&amp;amp;&amp;amp;var&amp;amp;i.&lt;BR /&gt;%end;&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;BR /&gt;%MEND;&lt;/P&gt;&lt;P&gt;%RENAME(DSN=X);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jun 2012 12:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rename-variable-with-macro-in-two-datasets/m-p/80273#M23118</guid>
      <dc:creator>My_SAS</dc:creator>
      <dc:date>2012-06-12T12:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable with macro in two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rename-variable-with-macro-in-two-datasets/m-p/80274#M23119</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You will need to either add an "order by varnum" statement into your sql code or get rid off "distinct" to have it return the variables in the order they are in the data set.&lt;/P&gt;&lt;P&gt;The same goes for data set X where the variables in it happened to be in alphabetical order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* got rid off distinct */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select name into :var1-&lt;/P&gt;&lt;P&gt;:var%TRIM(%LEFT(&amp;amp;num_vars))&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname="WORK" and&lt;/P&gt;&lt;P&gt;memname="L2"&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* added order by varnum */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select distinct(name) into :var1-&lt;/P&gt;&lt;P&gt;:var%TRIM(%LEFT(&amp;amp;num_vars))&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname="WORK" and&lt;/P&gt;&lt;P&gt;memname="L2"&lt;/P&gt;&lt;P&gt;order by varnum&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jun 2012 12:50:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rename-variable-with-macro-in-two-datasets/m-p/80274#M23119</guid>
      <dc:creator>Alpay</dc:creator>
      <dc:date>2012-06-12T12:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable with macro in two datasets</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rename-variable-with-macro-in-two-datasets/m-p/80275#M23120</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dictionary table COLUMNS will keep variable name in the order they are stored in dataset .&lt;/P&gt;&lt;P&gt;As long as you can ensure they have the same number of variables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data l2;
input z b2 s3;
cards;
1 2 3
;
run;

 

data x;
input v x2 z3;
cards;
1 2 3
;
run;

data _null_ ;
 merge sashelp.vcolumn(keep=name libname memname where=(libname='WORK' and memname='X'))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sashelp.vcolumn(keep=name libname memname where=(libname='WORK' and memname='L2') rename=(name=_name)) end=last;
 if _n_ eq 1 then call execute('proc datasets library=work nolist memtype=data; modify x; rename ');
 call execute(catx('=',name,_name));
 if last then call execute('; quit;');
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jun 2012 02:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rename-variable-with-macro-in-two-datasets/m-p/80275#M23120</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-06-13T02:49:57Z</dc:date>
    </item>
  </channel>
</rss>

