<?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 Add file name to all sas datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896423#M354205</link>
    <description>&lt;P&gt;Hello.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to import all excel files in a directory (all have very different names) and on import or after import, I need to add a column to each file and populate it with the file name.&amp;nbsp; So if I have say 10 excel files and each have a different name of the month as the file name, I want to import them all and add a column "month" and populate it with the file name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works but it's only one file at a time.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data list;&lt;BR /&gt;set list indsname=name;&lt;BR /&gt;tablename=scan(name,2);&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 29 Sep 2023 14:26:29 GMT</pubDate>
    <dc:creator>deblee73</dc:creator>
    <dc:date>2023-09-29T14:26:29Z</dc:date>
    <item>
      <title>Add file name to all sas datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896423#M354205</link>
      <description>&lt;P&gt;Hello.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to import all excel files in a directory (all have very different names) and on import or after import, I need to add a column to each file and populate it with the file name.&amp;nbsp; So if I have say 10 excel files and each have a different name of the month as the file name, I want to import them all and add a column "month" and populate it with the file name.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works but it's only one file at a time.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data list;&lt;BR /&gt;set list indsname=name;&lt;BR /&gt;tablename=scan(name,2);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2023 14:26:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896423#M354205</guid>
      <dc:creator>deblee73</dc:creator>
      <dc:date>2023-09-29T14:26:29Z</dc:date>
    </item>
    <item>
      <title>Re: Add file name to all sas datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896450#M354211</link>
      <description>How are you importing the files/sheets?Add it in that step.</description>
      <pubDate>Fri, 29 Sep 2023 14:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896450#M354211</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-09-29T14:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: Add file name to all sas datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896458#M354215</link>
      <description>&lt;P&gt;Not sure if you need to see all the code but the first big chunk gets a list of all the Excel files in the directory.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Then, the import:&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;DIV&gt;%macro import_file(path, file_name, dataset_name );&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;proc import&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;datafile="&amp;amp;path.\&amp;amp;file_name."&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;dbms=xlsx&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;out=&amp;amp;dataset_name;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%mend;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%*Call macro once for each entry in the list table created from the %list_files() macro;&lt;/DIV&gt;&lt;DIV&gt;data _null_;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set list;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;string = catt('%import_file(', dir, ', ',&amp;nbsp; name,', ', compress(substr(the_name,1,15)), ');');&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;call execute (string);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;STRONG&gt;Not sure where to put it in the above.&amp;nbsp; Honestly, it was taking a lot of google code until I got something to work and this is the last piece.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&lt;STRONG&gt;I honestly don't need the stop have the file named the same, I just need to fine name in a column.&amp;nbsp; I found some come that named each file Test01, Test02 on import which is ok but it didn't put the file name in the file.&amp;nbsp;&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 29 Sep 2023 15:17:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896458#M354215</guid>
      <dc:creator>deblee73</dc:creator>
      <dc:date>2023-09-29T15:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: Add file name to all sas datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896463#M354219</link>
      <description>&lt;P&gt;So this code is assuming that each XLSX workbook only has ONE worksheet (or that you only want the first worksheet when it has more than one).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just add the step to add the name into the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro import_file(path, file_name, dataset_name );
proc import 
  datafile="&amp;amp;path.\&amp;amp;file_name."
  dbms=xlsx
  out=&amp;amp;dataset_name
;
run;
data &amp;amp;dataset_name;
   set &amp;amp;dataset_name;
   length file_name $100;
   file_name="&amp;amp;file_name";
run; 
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Sep 2023 15:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896463#M354219</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-29T15:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: Add file name to all sas datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896471#M354223</link>
      <description>&lt;P&gt;Sa-Weeeet.&amp;nbsp; It works.&amp;nbsp; God bless you. Thank you so very much.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Sep 2023 16:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-file-name-to-all-sas-datasets/m-p/896471#M354223</guid>
      <dc:creator>deblee73</dc:creator>
      <dc:date>2023-09-29T16:35:46Z</dc:date>
    </item>
  </channel>
</rss>

