<?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: Import an excel data with libname and change the column-variablename in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961117#M374714</link>
    <description>&lt;P&gt;If you are getting variable names like that from the column headers in your Excel sheet then you must be using this SAS option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=ANY ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In that case you will need to use NAME literals to rename the variables whose names are not valid SAS names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename " in bn. $"n=A ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to automate renaming the variables you could try something like this to generate OLD=NEW name pairs that you could use in a RENAME statement or RENAME= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('=',nliteral(name),cats('VAR',varnum))
  into :renames separated by ' '
  from dictionary.columns
  where libname='MYEXCEL' and memname='MYSHEET'
;
quit;

data mysheet;
  set myexcel.mysheet(rename=(&amp;amp;renames));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While you are at it you might want to attach those original names as the labels for the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('=',nliteral(name),cats('VAR',varnum))
     , catx('=',nliteral(name),quote(strip(name)))
  into :renames separated by ' '
     , :labels separated by ' '
  from dictionary.columns
  where libname='MYEXCEL' and memname='MYSHEET'
;
quit;

data mysheet;
  set myexcel.mysheet;
  rename &amp;amp;renames;
  label &amp;amp;labels;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Mar 2025 16:56:10 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-03-06T16:56:10Z</dc:date>
    <item>
      <title>Import an excel data with libname and change the column-variablename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961109#M374713</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;I&lt;/SPAN&gt; &lt;SPAN class=""&gt;have&lt;/SPAN&gt; &lt;SPAN class=""&gt;an&lt;/SPAN&gt; &lt;SPAN class=""&gt;Excel&lt;/SPAN&gt; &lt;SPAN class=""&gt;file&lt;/SPAN&gt; &lt;SPAN class=""&gt;that&lt;/SPAN&gt; &lt;SPAN class=""&gt;has&lt;/SPAN&gt; &lt;SPAN class=""&gt;different&lt;/SPAN&gt; &lt;SPAN class=""&gt;worksheets&lt;/SPAN&gt; &lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;one&lt;/SPAN&gt; &lt;SPAN class=""&gt;sheet&lt;/SPAN&gt; &lt;SPAN class=""&gt;for&lt;/SPAN&gt; &lt;SPAN class=""&gt;each&lt;/SPAN&gt; &lt;SPAN class=""&gt;month&lt;/SPAN&gt;&lt;SPAN class=""&gt;). On every worksheet are different "column-variablenames" e.g. " in bn. $" (with leading space and dollar sign) , "car manufacturer" and "number of cars sold in tsd/1000".&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;It looks like that:&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;&amp;nbsp;in bn. $&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;car manufacturer&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;number of cars sold in tsd&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;audi&lt;/TD&gt;&lt;TD&gt;2133&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;vauxhall&lt;/TD&gt;&lt;TD&gt;3121&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;toyota&lt;/TD&gt;&lt;TD&gt;13131&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;TD&gt;...&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I imported the excel data into SAS using the libname xlsx command. My problem is, that i can't change the column variables that SAS has assigned automatically. Especially the " in bn. $" (with leading space and $ sign) causes huge problems. Is there any way to delete all the headings and replace them with A, B and C for example.&amp;nbsp; The colum names are not necessarily identical in the other months (sometimes its also "number of cars in 1000")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be very grateful for your help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 16:19:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961109#M374713</guid>
      <dc:creator>MaxiHösi</dc:creator>
      <dc:date>2025-03-06T16:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Import an excel data with libname and change the column-variablename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961117#M374714</link>
      <description>&lt;P&gt;If you are getting variable names like that from the column headers in your Excel sheet then you must be using this SAS option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=ANY ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In that case you will need to use NAME literals to rename the variables whose names are not valid SAS names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rename " in bn. $"n=A ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to automate renaming the variables you could try something like this to generate OLD=NEW name pairs that you could use in a RENAME statement or RENAME= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('=',nliteral(name),cats('VAR',varnum))
  into :renames separated by ' '
  from dictionary.columns
  where libname='MYEXCEL' and memname='MYSHEET'
;
quit;

data mysheet;
  set myexcel.mysheet(rename=(&amp;amp;renames));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;While you are at it you might want to attach those original names as the labels for the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select catx('=',nliteral(name),cats('VAR',varnum))
     , catx('=',nliteral(name),quote(strip(name)))
  into :renames separated by ' '
     , :labels separated by ' '
  from dictionary.columns
  where libname='MYEXCEL' and memname='MYSHEET'
;
quit;

data mysheet;
  set myexcel.mysheet;
  rename &amp;amp;renames;
  label &amp;amp;labels;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Mar 2025 16:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961117#M374714</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-06T16:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Import an excel data with libname and change the column-variablename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961119#M374715</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;My problem is, that i can't change the column variables that SAS has assigned automatically.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I would have to disagree. You can name these columns anything you want. Example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile="example1.xlsx" out=a(rename=(_in_bn___=billion_dollars));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 17:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961119#M374715</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-06T17:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Import an excel data with libname and change the column-variablename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961175#M374723</link>
      <description>&lt;P&gt;An option that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; missed would be make sure that when using Proc Import that you use&lt;/P&gt;
&lt;PRE&gt;options validvarname=V7;&lt;/PRE&gt;
&lt;P&gt;The V7 refers to SAS version 7 where the longer variable names were acceptable. That option will turn all non-letter and non-digit characters into underscore and reduce the complexity of the rename code removing the need for the NLITERAL bits, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s example shows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps a more important question, at least in my line of work, is why each of these sheets has a different set of column names at all if this is all related to a single project. &lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 22:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961175#M374723</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-03-06T22:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: Import an excel data with libname and change the column-variablename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961179#M374726</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=any validmemname=extend;
libname x xlsx 'c:\temp\text.xlsx';
proc datasets library=work kill;
quit;
proc copy in=x out=work noclone;
run;




%macro rename(dsn=);
proc transpose data="&amp;amp;dsn."n(obs=0) out=_temp_;
var _all_;
run;
data _temp_;
 set _temp_;
 new_name=cats('_',compress(_name_,,'kad'));
run;
proc sql noprint;
select catx('=',nliteral(_name_),new_name) into : rename separated by ' '
 from _temp_;
quit;
proc datasets library=work nolist nodetails;
modify "&amp;amp;dsn."n;
rename &amp;amp;rename.;
quit;
%mend;

data _null_;
 set sashelp.vtable(where=(libname='X'));
 call execute(catt('%nrstr(%rename)(dsn=',memname,')')); 
run;

libname x clear;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Mar 2025 02:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961179#M374726</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-03-07T02:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Import an excel data with libname and change the column-variablename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961188#M374731</link>
      <description>&lt;P&gt;Awesome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE&gt;proc sql noprint;
select catx('=',nliteral(name),cats('VAR',varnum))
  into :renames separated by ' '
  from dictionary.columns
  where libname='MYEXCEL' and memname='MYSHEET'
;
quit;

data mysheet;
  set myexcel.mysheet(rename=(&amp;amp;renames));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;That's exactly what i wanted. Thanks a lot &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Mar 2025 08:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-an-excel-data-with-libname-and-change-the-column/m-p/961188#M374731</guid>
      <dc:creator>MaxiHösi</dc:creator>
      <dc:date>2025-03-07T08:34:14Z</dc:date>
    </item>
  </channel>
</rss>

