<?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 position (37 columns) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706760#M216956</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/349857"&gt;@BlayLay&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I was just providing "COL1" as a means to represent the column number, the column names remain what was mentioned in the original post. Looking to see if there is a way to rename columns based on column number (i.e. position).&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I still don't understand. It seems as if you are trying to go&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Start Time -&amp;gt; COL1 -&amp;gt; StartDate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;instead of&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Start Time -&amp;gt; StartDate&lt;/P&gt;</description>
    <pubDate>Thu, 17 Dec 2020 19:15:14 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-12-17T19:15:14Z</dc:date>
    <item>
      <title>Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706722#M216932</link>
      <description>&lt;P&gt;I have a .xlsx that has 37 different columns. After import, I want to rename them based on column position. For example, reference below mockup table, I'd like to change "Start Time" to "StartDate" by referencing the column number (Column 1) if possible, as there are 37 columns all with varying names and lengths of names and was looking for an easier way to rename. Some other examples I've found have limitations after 26 columns. If there is a way to do this on import, referencing column position, I can do that as well. I'm just not sure of how to approach this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Start Time&lt;/TD&gt;&lt;TD&gt;Machine Name&lt;/TD&gt;&lt;TD&gt;Total Hours Worked&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 17 Dec 2020 17:42:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706722#M216932</guid>
      <dc:creator>BlayLay</dc:creator>
      <dc:date>2020-12-17T17:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706726#M216934</link>
      <description>&lt;P&gt;Maybe the best approach to this sort of thing is to save the Excel file to CSV format and use a data step to read it with the variable names and properties you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure where "column number" comes into this.&lt;/P&gt;
&lt;P&gt;Proc datasets is designed to do things to existing data in place.&lt;/P&gt;
&lt;P&gt;Since you show non-standard for SAS columns it might help to run Proc Contents on your existing data set and share the results.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 17:57:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706726#M216934</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-17T17:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706729#M216936</link>
      <description>&lt;P&gt;Why do you need to reference column #1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can't you just rename in SAS using a RENAME statement? Something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets library=work nolist;
     modify mydataset;
     rename start_time=startdate;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you know what each variable name is supposed to be renamed to, you can achieve this renamification (if that's a word) via either macros or CALL EXECUTE. Can you show us a portion of the list of renames by column?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 18:01:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706729#M216936</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-17T18:01:25Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706751#M216948</link>
      <description>Because I'll be renaming 37 columns, all with varying lengths. Was looking for a way of making it easier by referencing column position and just doing something like this:&lt;BR /&gt;&lt;BR /&gt;RENAME col1 = 'StartTime'n&lt;BR /&gt;col2 = 'MachineName'n&lt;BR /&gt;etc&lt;BR /&gt;col37 = 'ZipCode'n</description>
      <pubDate>Thu, 17 Dec 2020 18:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706751#M216948</guid>
      <dc:creator>BlayLay</dc:creator>
      <dc:date>2020-12-17T18:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706752#M216949</link>
      <description>&lt;P&gt;Varying lengths shouldn't matter, as long as the variable name is 32 characters or less.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why create a variable named COL1 if its just an intermediary that isn't needed? This will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets library=work nolist;
     modify mydataset;
     rename 'Start Time'n=startdate 'Machine Name'n=machinename
         'Total Hours Worked'n=totalhours;
run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 19:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706752#M216949</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-17T19:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706754#M216951</link>
      <description>&lt;P&gt;I don't understand.&amp;nbsp; In your original question you showed a spreadsheet with column headers.&amp;nbsp; A normal reading of the spreadsheet by SAS (proc import or XLSX libname engins) would have caused to used as the variable names. Yet now in this post you are showing variables named COL1, COL2, etc.&lt;/P&gt;
&lt;P&gt;Which of these do you have?&lt;/P&gt;
&lt;P&gt;Show more details about what you are doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 19:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706754#M216951</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-17T19:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706756#M216952</link>
      <description>I was just providing "COL1" as a means to represent the column number, the column names remain what was mentioned in the original post. Looking to see if there is a way to rename columns based on column number (i.e. position).</description>
      <pubDate>Thu, 17 Dec 2020 19:09:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706756#M216952</guid>
      <dc:creator>BlayLay</dc:creator>
      <dc:date>2020-12-17T19:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706760#M216956</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/349857"&gt;@BlayLay&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I was just providing "COL1" as a means to represent the column number, the column names remain what was mentioned in the original post. Looking to see if there is a way to rename columns based on column number (i.e. position).&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I still don't understand. It seems as if you are trying to go&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Start Time -&amp;gt; COL1 -&amp;gt; StartDate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;instead of&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Start Time -&amp;gt; StartDate&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 19:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706760#M216956</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-17T19:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706762#M216958</link>
      <description>&lt;P&gt;To rename a variable you need to know its name, its position does not matter.&amp;nbsp; So for your first example name you would want to rename using one of these two forms, depending on whether your variable names were made use VALIDVARNAME=ANY or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename Start_Time=StartDate;
rename 'Start Time'n=StartDate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is it possibly you wanted to match the names by position to some other list of names that you have?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new_names ;
  varnum+1;
  input new_name $32.;
cards;
StartDate
;
proc contents data=have noprint out=contents;
run;
proc sql noprint;
select catx('=',nliteral(a.name),nliteral(b.new_name))
  into :renames separated by ' '
  from contents a inner join new_names b
  on a.varnum = b.varnum
;
quit;
proc datasets nolist lib=work;
  modify have ;
    rename &amp;amp;renames;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 19:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706762#M216958</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-17T19:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706769#M216964</link>
      <description>I think you answered my question "position does not matter". I was hoping to be able to reference the position to rename the column, rather than typing out each column name in the rename step.</description>
      <pubDate>Thu, 17 Dec 2020 19:35:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706769#M216964</guid>
      <dc:creator>BlayLay</dc:creator>
      <dc:date>2020-12-17T19:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706787#M216986</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/349857"&gt;@BlayLay&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I think you answered my question "position does not matter". I was hoping to be able to reference the position to rename the column, rather than typing out each column name in the rename step.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is a snipped to help reduce the typing:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table temp as
   select catx(' ', strip(name),'=','" "') as string
   from dictionary.columns
   where libname='SASHELP' and memname='CLASS'
   ;
quit;

data _null_;
  set temp;
  file print;
  put string;
run;&lt;/PRE&gt;
&lt;P&gt;This will place something like this in the result window:That you can copy and put the new names into Rename syntax.&lt;/P&gt;
&lt;PRE&gt;Name = " "                                                      
Sex = " "                                                       
Age = " "                                                       
Height = " "                                                    
Weight = " "  &lt;/PRE&gt;
&lt;P&gt;Replace each set of " " with the new name.&lt;/P&gt;
&lt;P&gt;If have managed to create names with spaces then the above code would need&lt;/P&gt;
&lt;PRE&gt;   select cats(quote(strip(name)),'n','=','" "') as string
&lt;/PRE&gt;
&lt;P&gt;to create name literals like "start time"n&amp;nbsp; which is needed when using names with spaces or unusual characters not normally allowed in SAS variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The dictionary tables maintain the metadata of your data sets in a number of tables. The Columns has the names and characteristics of all the variables in all your data sets. When you use this code the Libname must be used or you can get odd results from data sets with the same name in different libraries, the Libname and Memname are stored in &lt;STRONG&gt;upper case&lt;/STRONG&gt; so the code needs to use the upper case names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 20:39:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706787#M216986</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-17T20:39:13Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706815#M217008</link>
      <description>&lt;P&gt;SAS has a function for generating name literals.&amp;nbsp; NLITERAL()&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 22:27:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706815#M217008</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-17T22:27:16Z</dc:date>
    </item>
    <item>
      <title>Re: Rename columns based on position (37 columns)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706831#M217016</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;SAS has a function for generating name literals.&amp;nbsp; NLITERAL()&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;I think I may have noticed that at some point but I do not use name literals so it never stuck.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 22:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-columns-based-on-position-37-columns/m-p/706831#M217016</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-17T22:59:37Z</dc:date>
    </item>
  </channel>
</rss>

