<?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: Store Filename as Column while importing multiple files from FTP server in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/872109#M38634</link>
    <description>&lt;P&gt;In order to get the filenames, retrieve the directory first, then read each file individually.&lt;/P&gt;
&lt;P&gt;The second can be done in a single data step, where you read the filenames from the dataset containing the directory listing, use the FILEVAR= option in the INFILE statement, and a DO loop to read the file.&lt;/P&gt;
&lt;P&gt;My presentation&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/838344" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/838344&lt;/A&gt;&amp;nbsp;contains an example where I do this with INFILE PIPE, you need to try this with INFILE FTP.&lt;/P&gt;</description>
    <pubDate>Wed, 26 Apr 2023 05:09:18 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-04-26T05:09:18Z</dc:date>
    <item>
      <title>Store Filename as Column while importing multiple files from FTP server</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/871972#M38627</link>
      <description>&lt;P&gt;So I am currently importing multiple text files from an FTP server into a single dataset. Due to the nature of the text files, I have to manually define the column lengths and names. What I am unable to figure out is how do I store the filename as a variable and insert it as a column so that the data for each file has the corresponding file name. The FTP I am using in WinSCP, and the files are in a subdirectory, where the prefix of the file is constant, but the suffix is changed based off of the timestamp that it gets uploaded. Currently my code looks like this (generalized)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;filename in ftp 'SUBFOLDER/common_file_prefix.*' /* The suffix is a variable name that has the timestamp of when the data was uploaded*/
user = 'user' 
password = 'password'
host = 'host'
mget debug;

DATA project_data;
INFILE IN TRUNCOVER;
INPUT
      COL1 $0-10
       ...
      COL 30 $500-550
RUN;


&lt;/PRE&gt;&lt;P&gt;I tried to add a column at the end using FILE_NAME = fname but that did not work, I have also tried to use the FILENAME = option I haven't gotten it to work for me yet either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 18:47:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/871972#M38627</guid>
      <dc:creator>shubh1996</dc:creator>
      <dc:date>2023-04-25T18:47:26Z</dc:date>
    </item>
    <item>
      <title>Re: Store Filename as Column while importing multiple files from FTP server</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/871995#M38628</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA project_data;
length file_name fname $200;
INFILE IN TRUNCOVER filename=fname;
INPUT
      COL1 $0-10
       ...
      COL 30 $500-550
;
file_name = fname;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Apr 2023 19:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/871995#M38628</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-25T19:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: Store Filename as Column while importing multiple files from FTP server</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/871997#M38629</link>
      <description>&lt;P&gt;When code doesn't work but you think that it should, then show us the log with the code and any messages received.&lt;/P&gt;
&lt;P&gt;Copy the text from the log and then paste that into a text box opened on the forum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Infile statement FILENAME= option creates a temporary variable. If you want the result in the output then you need to define another variable long enough to hold expected values and assign the value to that variable.&lt;/P&gt;
&lt;P&gt;Something like this: But I haven't used this with FTP file method and different FTP servers may behave / differently.&lt;/P&gt;
&lt;PRE&gt;DATA project_data;
INFILE IN TRUNCOVER filename=infile;
INPUT
      COL1 $0-10
      &amp;lt;rest of input statement&amp;gt;
;
length Source $ 300;
source = infile;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Apr 2023 19:51:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/871997#M38629</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-25T19:51:46Z</dc:date>
    </item>
    <item>
      <title>Re: Store Filename as Column while importing multiple files from FTP server</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/872002#M38630</link>
      <description>&lt;P&gt;Neither of the methods have worked, SAS is able to recognize the filename in the LOG, but for some reason it doesn't seem to be getting stored.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shubh1996_1-1682452581163.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83219iF93CE0456471440C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shubh1996_1-1682452581163.png" alt="shubh1996_1-1682452581163.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shubh1996_3-1682452690647.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83221iC4CF33B3FE26D137/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shubh1996_3-1682452690647.png" alt="shubh1996_3-1682452690647.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shubh1996_2-1682452610412.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83220iE5A3C79CC4C35093/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shubh1996_2-1682452610412.png" alt="shubh1996_2-1682452610412.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 19:58:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/872002#M38630</guid>
      <dc:creator>shubh1996</dc:creator>
      <dc:date>2023-04-25T19:58:33Z</dc:date>
    </item>
    <item>
      <title>Re: Store Filename as Column while importing multiple files from FTP server</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/872016#M38631</link>
      <description>&lt;P&gt;Logs really should include the actual submitted code so we actually see the options you submitted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the documentation of Filename FTP&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xis-refDictEntry"&gt;
&lt;DIV class="xis-syntax"&gt;
&lt;DIV class="xis-syntaxDescription"&gt;
&lt;DIV class="xis-otherArgGroup"&gt;
&lt;DIV id="p0hl3dlxomvxsrn1ndhm04fgqt0c" class="xis-argDescriptionPair"&gt;
&lt;H4 class="xis-argument"&gt;MGET&lt;/H4&gt;
&lt;DIV class="xis-argumentDescription"&gt;
&lt;P class="xis-paraSimpleFirst"&gt;transfers multiple files, similar to the &lt;FONT style="background-color: #fcdec0;"&gt;FTP&lt;/FONT&gt; command MGET.&lt;/P&gt;
&lt;TABLE class="xis-summary"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD rowspan="2" class="xis-summaryTip"&gt;Tips&lt;/TD&gt;
&lt;TD class="xis-summaryText"&gt;The whole transfer is treated as one file. However, as the transfer of each new file is started, the EOV= variable is set to 1.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="xis-summaryText"&gt;Specify MPROMPT to prompt the user before each file is sent.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this means that the FILENAME= infile option is only going to get one string, content unknown.&lt;/P&gt;
&lt;P&gt;You might use the EOV= option to set a file counter in a retained variable and then use a separate connection to get this of files then match them up. Maybe.&lt;/P&gt;
&lt;PRE&gt;data example;
   infile in &amp;lt;other options&amp;gt; eov=newfile;
   retain filecount;
   if newfile then filecount+1;
run;&lt;/PRE&gt;
&lt;P&gt;And then use a separate FTP command to execute the LIST command. Look at examples for "Retrieving a Directory Listing"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know if it will help and it has been a while since I actually used FTP much but perhaps if instead of&lt;/P&gt;
&lt;PRE&gt;filename in ftp 'SUBFOLDER/common_file_prefix.*' /*&lt;/PRE&gt;
&lt;P&gt;That you connect to the site and then use the CD= option to move your connection point to the SUBFOLDER directory.&lt;/P&gt;
&lt;P&gt;Somewhat of a shot in the dark but it might be that the information passed to include the SUBFOLDER from the connection isn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The size of your files appears to be relatively small. Perhaps just use MGET to download all of the files to a local drive and then the INFILE with Filename= shouldn't have any problem.&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>Tue, 25 Apr 2023 21:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/872016#M38631</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-25T21:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: Store Filename as Column while importing multiple files from FTP server</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/872109#M38634</link>
      <description>&lt;P&gt;In order to get the filenames, retrieve the directory first, then read each file individually.&lt;/P&gt;
&lt;P&gt;The second can be done in a single data step, where you read the filenames from the dataset containing the directory listing, use the FILEVAR= option in the INFILE statement, and a DO loop to read the file.&lt;/P&gt;
&lt;P&gt;My presentation&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/838344" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-User-Groups-Library/WUSS-Presentation-Talking-to-Your-Host/ta-p/838344&lt;/A&gt;&amp;nbsp;contains an example where I do this with INFILE PIPE, you need to try this with INFILE FTP.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2023 05:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Store-Filename-as-Column-while-importing-multiple-files-from-FTP/m-p/872109#M38634</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-04-26T05:09:18Z</dc:date>
    </item>
  </channel>
</rss>

