<?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: Proc import for XLSX files, controlling variable name with getnames=no in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905946#M357778</link>
    <description>&lt;P&gt;Your last description sums it up. I don't know which column the named range will be referring to. I have now sorted out a work-around. A bit clunky, but I can rely on the range being just a single column, and so the imported datafile will only have one variable. Then I can use this code to pull out the name of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro FirstVName(data);
%* returns the name of the first variable in the file;
%let dsid = %sysfunc(open(&amp;amp;data,i));
%sysfunc(varname(&amp;amp;dsid,1))
%mend FirstVName;&lt;/PRE&gt;</description>
    <pubDate>Mon, 04 Dec 2023 08:10:51 GMT</pubDate>
    <dc:creator>BruceBrad</dc:creator>
    <dc:date>2023-12-04T08:10:51Z</dc:date>
    <item>
      <title>Proc import for XLSX files, controlling variable name with getnames=no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905934#M357772</link>
      <description>&lt;P&gt;I want to read a named range of data from an xlsx file with getnames=no. It is a single column of numbers. How do I control the variable name that gets assigned to the data? It seems to be using the column name for the data (eg B if the range is in column B). However, the range is in different columns in different spreadsheets, so I don't know this in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 02:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905934#M357772</guid>
      <dc:creator>BruceBrad</dc:creator>
      <dc:date>2023-12-04T02:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: Proc import for XLSX files, controlling variable name with getnames=no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905942#M357777</link>
      <description>&lt;P&gt;Is this a named range or do you actually provide the range explicitly to Proc Import?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's an explicit range then you know the column and you could simply rename the column in the out statement: out=work.want(rename=(e=wantVarName))&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If these are named ranges then there are other options possible.&lt;/P&gt;
&lt;P&gt;To provide "the right" solution for this case if necessary: Are you running a set of Proc Imports and then got a set of tables that follow a naming convention but with always a single column with different name? Like: Table_1 with column C, Table_2 with column E, etc.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 07:25:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905942#M357777</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-04T07:25:59Z</dc:date>
    </item>
    <item>
      <title>Re: Proc import for XLSX files, controlling variable name with getnames=no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905946#M357778</link>
      <description>&lt;P&gt;Your last description sums it up. I don't know which column the named range will be referring to. I have now sorted out a work-around. A bit clunky, but I can rely on the range being just a single column, and so the imported datafile will only have one variable. Then I can use this code to pull out the name of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro FirstVName(data);
%* returns the name of the first variable in the file;
%let dsid = %sysfunc(open(&amp;amp;data,i));
%sysfunc(varname(&amp;amp;dsid,1))
%mend FirstVName;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Dec 2023 08:10:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905946#M357778</guid>
      <dc:creator>BruceBrad</dc:creator>
      <dc:date>2023-12-04T08:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: Proc import for XLSX files, controlling variable name with getnames=no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905974#M357784</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12505"&gt;@BruceBrad&lt;/a&gt;&amp;nbsp;I was thinking about something similar to what you have done.&lt;/P&gt;
&lt;P&gt;In case you create this set of tables that all have a common "root" name and you want to combine the data into a single result table then below approach should also work. Using a SQL UNION will also ensure that the length of your variable will be the max length on any of the source variables - which when using Proc Import could differ between the tables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample data */
data table_1(keep=E) table_2(keep=B) table_3(keep=F);
  E='A';
  B='XXX';
  F='9';
run;

/* generate SQL to combine the tables */
proc sql;
  select 
    'select * from '||cats(libname,'.',memname) 
      into :genSQL separated by ' union all '
  from dictionary.tables
  where libname='WORK' and memname like 'TABLE^_%' escape '^'
  ;
quit;

/* create zero row table structure with desired column name and attributes */
data table_structure;
  stop;
  attrib my_var length=$1;
run;

/* concatenate data */
proc sql;
  create table want as
  select * from table_structure
  union all 
  &amp;amp;genSQL
  ;
quit;

proc contents data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1701690606428.png" style="width: 310px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/90738i35AD8DC17DAB82C2/image-dimensions/310x95?v=v2" width="310" height="95" role="button" title="Patrick_0-1701690606428.png" alt="Patrick_0-1701690606428.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Or here another option&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set table_:;
  array vars {*} _character_;
  length new_var $3;
  new_var=coalescec(of vars[*]);
  keep new_var;
run;&lt;/CODE&gt;&lt;/PRE&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>Mon, 04 Dec 2023 12:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/905974#M357784</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-04T12:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: Proc import for XLSX files, controlling variable name with getnames=no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/906017#M357798</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12505"&gt;@BruceBrad&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Your last description sums it up. I don't know which column the named range will be referring to. I have now sorted out a work-around. A bit clunky, but I can rely on the range being just a single column, and so the imported datafile will only have one variable. Then I can use this code to pull out the name of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro FirstVName(data);
%* returns the name of the first variable in the file;
%let dsid = %sysfunc(open(&amp;amp;data,i));
%sysfunc(varname(&amp;amp;dsid,1))
%mend FirstVName;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Exactly.&amp;nbsp; &amp;nbsp;You can ask SAS to find the names once it has made the dataset.&lt;/P&gt;
&lt;P&gt;For your example of making a series of datasets you could just use PROC CONTENTS or DICTIONARY.COLUMNS to get the information you need to generate the code.&lt;/P&gt;
&lt;P&gt;For example you could generate DSNAME and RENAME strings from DICTIONARY.COLUMNS&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table varlist as
  select memname as dsname length=32
        , catx('=',name,'want') as rename length=65 
  from dictionary.columns
  where libname='WORK' and memname like 'TABLE%' 
    and varnum=1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use that to generate the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set varlist end=eof;
  if _n_=1 then call execute('proc&amp;nbsp;datasets&amp;nbsp;nolist&amp;nbsp;lib=work;');
&amp;nbsp;&amp;nbsp;call&amp;nbsp;execute(catx('&amp;nbsp;','modify',dsname,';rename',rename,';run;'));
&amp;nbsp;&amp;nbsp;if&amp;nbsp;eof&amp;nbsp;then&amp;nbsp;call&amp;nbsp;execute('quit;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Dec 2023 14:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-import-for-XLSX-files-controlling-variable-name-with/m-p/906017#M357798</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-04T14:46:45Z</dc:date>
    </item>
  </channel>
</rss>

