<?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 columns based on a reference table in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351561#M23162</link>
    <description>&lt;P&gt;Found one solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;

proc sql noprint;

select compress (Col_Names) into :list1 separated by " "
from ColTable;

select compress (Standard) into :list2 separated by " "
from sample;

quit;

%let Num_Col = %sysfunc(countw(&amp;amp;list1));
%do _j=1 %to &amp;amp;Num_Col;
%let ColName = %scan(&amp;amp;list1,&amp;amp;_j);
%let RefName = %scan(&amp;amp;list2,&amp;amp;_j);

data sample;
set sample;

rename &amp;amp;ColName = &amp;amp;RefName;
run;

%end;


%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, if the column names contain space, then this method might not work.&lt;/P&gt;</description>
    <pubDate>Thu, 20 Apr 2017 07:14:36 GMT</pubDate>
    <dc:creator>ayin</dc:creator>
    <dc:date>2017-04-20T07:14:36Z</dc:date>
    <item>
      <title>Rename columns based on a reference table</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351520#M23158</link>
      <description>&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Have:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;column&amp;nbsp;reference table (named 'ColTable'), that looks like -&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Standard Col_Names
Col_1      a1
Col_2      a2&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;one dataset (named 'sample'), that looks like -&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;a1  a2 
3   char&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Want:&lt;/STRONG&gt;&lt;/U&gt;&lt;BR /&gt;rename the columns in the dataset 'sample' to:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Col_1 Col_2
3     char&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Questions:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;How to achieve this?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2017 07:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351520#M23158</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-04-20T07:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on pre-defined reference tables (advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351525#M23159</link>
      <description>&lt;P&gt;So what exactly is your question?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2017 03:35:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351525#M23159</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-20T03:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on a reference table</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351561#M23162</link>
      <description>&lt;P&gt;Found one solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;

proc sql noprint;

select compress (Col_Names) into :list1 separated by " "
from ColTable;

select compress (Standard) into :list2 separated by " "
from sample;

quit;

%let Num_Col = %sysfunc(countw(&amp;amp;list1));
%do _j=1 %to &amp;amp;Num_Col;
%let ColName = %scan(&amp;amp;list1,&amp;amp;_j);
%let RefName = %scan(&amp;amp;list2,&amp;amp;_j);

data sample;
set sample;

rename &amp;amp;ColName = &amp;amp;RefName;
run;

%end;


%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, if the column names contain space, then this method might not work.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2017 07:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351561#M23162</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-04-20T07:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on a reference table</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351577#M23163</link>
      <description>&lt;P&gt;Yuk. &amp;nbsp;That macro code is a bit overcomplicated and re-creates the dataset (using resources). &amp;nbsp;Bettter off doing a proc datasets dynamically like below. &amp;nbsp;However you say there may be spaces? &amp;nbsp;If the names are not valida SAS names then it will fail - this is why you messing around with keeping metadata in datasets - presumable from the bad datasource Excel - needs lots of quality control;&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set col_ref end=last;
  if _n_=1 then call execute('proc datasets data=one noprint nolist;');
  call execute(cat('rename ',strip(col_names),'=',strip(standard),';'));
  if last then call execute(';run;quit;');
run;&lt;/PRE&gt;
&lt;P&gt;Do note you can re-label, format etc. at the same time with the above and it doesn't re-write the dataset (so on bigger data would be a saving). &amp;nbsp;Also&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;Post test data in the form of a datastep!!!!&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2017 08:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/351577#M23163</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-20T08:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on a reference table</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/352095#M23230</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120587"&gt;@ayin&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even though you've got already a solution I'd like to propose yet another solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;'s code only works when all columns in your ref table match with a column in your have table. Where I fully agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;is, that you should be using Proc Datasets and not a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Reason:&lt;/P&gt;
&lt;P&gt;Renaming table names and changing variable attributes (with exception of the length) is only a change to the descriptor part of a SAS table (the "header"). You don't need to process the data for such a change. That's what Proc Datasets does for you. A datastep on the other hand fully re-creates the table and iterates through all the data. That's may be not that important for your real use case but it's going to make a huge performance difference when dealing with large volume tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And yes, post in the future please sample data created via SAS data steps (as I've done now in below sample). That makes it easier for us to&amp;nbsp;propose unit tested code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data col_ref;
  input (standard col_names) ($);
  datalines;
Col_1 a1
Col_2 a2
Col_3 a3
;
run;

data have;
  a1=3;
  a2='char';
  a99=55;
  output;
  stop;
run;


%macro StandardizeTable(sourceDS,targetDS,refTbl);

  /* generate column rename statements */
  %local renameList;
  proc sql noprint;
    select cats(col_names,'=',standard) into :renameList separated by ' '
    from &amp;amp;refTbl
    where upcase(col_names) in
      (
        select upcase(name)
        from dictionary.columns
        where libname="%upcase(%scan(work.&amp;amp;sourceDS,-2))" and memname="%upcase(%scan(&amp;amp;sourceDS,-1))"
      )
    ;
  quit;

  /* rename table and columns */
  proc datasets lib=%scan(work.&amp;amp;sourceDS,-2) nolist nowarn;

    /* rename table if targetDS name provided */
    %if %bquote(&amp;amp;targetDS) ne %bquote() %then
      %do;
        delete want;
        run;
        change %scan(&amp;amp;sourceDS,-1)=&amp;amp;targetDS;
        run;
      %end;
    %else %let targetDS=%scan(&amp;amp;sourceDS,-1);

    /* rename columns if any match with cols in refTbl */
    %if %bquote(&amp;amp;renameList) ne %bquote() %then
      %do;
        modify &amp;amp;targetDS;
          rename
            &amp;amp;renameList
          ;
        run;
      %end;
  quit;

%mend;

%StandardizeTable(have,,col_ref);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 09:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Rename-columns-based-on-a-reference-table/m-p/352095#M23230</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-04-21T09:54:06Z</dc:date>
    </item>
  </channel>
</rss>

