<?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: SAS macro to change column names in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492910#M72247</link>
    <description>&lt;P&gt;Please give a definative example, with test data in the form of a datastep, and what the output should look like.&amp;nbsp; Renaming variables is as simple as:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have (keep=xc_1 xc_2 xc_3);  /* Just to show an example */
  rename xc_1-xc-3=xn_1-xn_3;
run;&lt;/PRE&gt;
&lt;P&gt;Or you can normalise, then transpose.&lt;/P&gt;
&lt;P&gt;Or you can have arrays.&lt;/P&gt;
&lt;P&gt;All ways of using plain Base SAS code to achieve the end result.&lt;/P&gt;</description>
    <pubDate>Thu, 06 Sep 2018 08:19:21 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-09-06T08:19:21Z</dc:date>
    <item>
      <title>SAS macro to change column names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492885#M72244</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Anyone know how to use macro to change names of a table column ,except the ones that you point out.numeric type columns are added prefix "XC_" and char type columns are added prefix "XN_"&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 06:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492885#M72244</guid>
      <dc:creator>Geo-</dc:creator>
      <dc:date>2018-09-06T06:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro to change column names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492886#M72245</link>
      <description>&lt;P&gt;This has been asked and answered so many times ... you will find adaptable code, if you try.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 06:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492886#M72245</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-09-06T06:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro to change column names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492900#M72246</link>
      <description>&lt;P&gt;First, acquaint yourself with the DATASETS procedure, as that will do the work.&lt;/P&gt;
&lt;P&gt;Then retrieve the current column names in a data _null_ step from sashelp.vcolumn, and use call execute to dynamically create the code for the datasets procedure.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.vcolumn (where=(libname="LIBRARY" and memname = "DATASET")) end=eof;
length prefix $3;
if _n_ = 1 then call execute("
  proc datasets library=LIBRARY nolist;
  modify DATASET;
  rename
");
if type = 'char'
then prefix = "XC_";
else prefix = "XN_";
call execute(trim(name !! '=' !! prefix !! trim(name) !! ' '));
if eof then call execute("
  ;
  quit;
");
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see, no macro needed.&lt;/P&gt;
&lt;P&gt;Note that I use double quotes wherever the use of macro variables will come in handy (libname, memname, prefixes).&lt;/P&gt;
&lt;P&gt;If you replace those items with macro variables, you can wrap the code into a macro definition for easier re-use.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 07:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492900#M72246</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-06T07:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro to change column names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492910#M72247</link>
      <description>&lt;P&gt;Please give a definative example, with test data in the form of a datastep, and what the output should look like.&amp;nbsp; Renaming variables is as simple as:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have (keep=xc_1 xc_2 xc_3);  /* Just to show an example */
  rename xc_1-xc-3=xn_1-xn_3;
run;&lt;/PRE&gt;
&lt;P&gt;Or you can normalise, then transpose.&lt;/P&gt;
&lt;P&gt;Or you can have arrays.&lt;/P&gt;
&lt;P&gt;All ways of using plain Base SAS code to achieve the end result.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 08:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492910#M72247</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-06T08:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: SAS macro to change column names</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492920#M72251</link>
      <description>&lt;P&gt;I would use SQL to put the renames into macro variables, like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select cats(name,'=',case when type='num' then 'XN_' else 'XC_' end,name) into :renames separated by ' '
  from dictionary.columns where libname='SASHELP' and memname='CLASS';
quit;
%put &amp;amp;renames;
data test;
  set sashelp.class;
  rename &amp;amp;renames;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you have some ID columns that you do not want renamed, just add "and name not in(&amp;lt;variables&amp;gt;)" in the WHERE clause.&lt;/P&gt;&lt;P&gt;E.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select cats(name,'=',case when type='num' then 'XN_' else 'XC_' end,name) into :renames separated by ' '
  from dictionary.columns where libname='SASHELP' and memname='CLASS' and name not in('NAME','AGE');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Sep 2018 09:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SAS-macro-to-change-column-names/m-p/492920#M72251</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-09-06T09:21:29Z</dc:date>
    </item>
  </channel>
</rss>

