<?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: How to find the the specific file an obs came from while using a wildcard on the filename in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590233#M168884</link>
    <description>&lt;P&gt;Use the FILENAME= option of the INFILE statement.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;docsetVersion=1.0&amp;amp;locale=en#n1cssx3sn3j2kwn1s6mf1y9fh3n7" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;docsetVersion=1.0&amp;amp;locale=en#n1cssx3sn3j2kwn1s6mf1y9fh3n7&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note the documentation says: " Like automatic variables, the FILENAME= variable is not written to the data set. " So you have to do something silly to save this information for later use, like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;infile files lrecl=512 pad filename=tempvar;
savedfilename = tempvar;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I haven't done this in a while, so I can't remember, but you may need a LENGTH statement between INFILE and SAVEDFILENAME=&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length savedfilename $ 256;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 19 Sep 2019 23:01:59 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-09-19T23:01:59Z</dc:date>
    <item>
      <title>How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590224#M168880</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing an import code to turn some files into SAS data sets. The data set will be updated weekly and the new files are appended to the existing data set.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using a wildcard on the FILENAME statement but one of my fields needs to be the original file where the observation came from.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I shortened the code for the sake of brevity but I can provide more details if necessary. It looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Files to be loaded: file1, file2, file3, file4 etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FILENAME files "/my/path/*file*.csv";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA append;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; format file_name $100. update_date $8.f;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; INFILE files lrecl=512 pad;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; (import code for each field goes here...)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &lt;STRONG&gt;file_name = ???&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;This is where I need to create a field specifying the file name that each obs came from&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example if the row came from file1 I would need this field to say field_name = 'file1'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way for sas to do this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, is there a way to list everything that is in 'files' from FILENAME files "/my/path/*file*.csv"; ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for your help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 22:18:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590224#M168880</guid>
      <dc:creator>gmuersch</dc:creator>
      <dc:date>2019-09-19T22:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590227#M168882</link>
      <description>Usually you use FILENAME and store that in your data set as well. Then it will be in the data set and you can find out which records come from which file. &lt;BR /&gt;&lt;BR /&gt;infile files ..... filename = source;&lt;BR /&gt;file_name = source;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Some of this is outlined here, including how to avoid the line headers in each file.&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-text-files-that-have/ta-p/223627" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-text-files-that-have/ta-p/223627&lt;/A&gt;</description>
      <pubDate>Thu, 19 Sep 2019 22:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590227#M168882</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-19T22:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590232#M168883</link>
      <description>&lt;P&gt;I tried your suggestion and it did populate my file_name field, but it filled them all with the first part of the path and not the file name it came from&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 22:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590232#M168883</guid>
      <dc:creator>gmuersch</dc:creator>
      <dc:date>2019-09-19T22:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590233#M168884</link>
      <description>&lt;P&gt;Use the FILENAME= option of the INFILE statement.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;docsetVersion=1.0&amp;amp;locale=en#n1cssx3sn3j2kwn1s6mf1y9fh3n7" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?docsetId=lestmtsref&amp;amp;docsetTarget=n1rill4udj0tfun1fvce3j401plo.htm&amp;amp;docsetVersion=1.0&amp;amp;locale=en#n1cssx3sn3j2kwn1s6mf1y9fh3n7&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note the documentation says: " Like automatic variables, the FILENAME= variable is not written to the data set. " So you have to do something silly to save this information for later use, like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;infile files lrecl=512 pad filename=tempvar;
savedfilename = tempvar;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I haven't done this in a while, so I can't remember, but you may need a LENGTH statement between INFILE and SAVEDFILENAME=&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length savedfilename $ 256;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Sep 2019 23:01:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590233#M168884</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-19T23:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590234#M168885</link>
      <description>thanks, I gave this solution a shot and it was close but not quite right.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;FILENAME files "/my/path/*file*.csv";&lt;BR /&gt;&lt;BR /&gt;infile files lrecl=512 pad filename=tempvar;&lt;BR /&gt;savedfilename = tempvar;&lt;BR /&gt;&lt;BR /&gt;it populated savedfilename as /my for each observation (/my is the first directory of the path)</description>
      <pubDate>Thu, 19 Sep 2019 23:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590234#M168885</guid>
      <dc:creator>gmuersch</dc:creator>
      <dc:date>2019-09-19T23:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590235#M168886</link>
      <description>&lt;P&gt;Try the LENGTH statement. If that doesn't work, show us a screen capture.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 23:13:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590235#M168886</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-19T23:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590239#M168889</link>
      <description>&lt;P&gt;Your initial code has this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; format file_name $100. &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Not sure what you had in the code you actually ran, but if you had that, you would only get the first 100 characters.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 00:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590239#M168889</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-20T00:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590244#M168893</link>
      <description>&lt;P&gt;Here is the exact code I am running:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FILENAME RAPS "/sasdata/mra/users/gmu/projects/IMPORT_RRR_AUTO/IMPORT_FILES/*RAPS_RETURN_FLAT_SH9698*.DAT";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA append;&lt;BR /&gt;format file_name $100. update_date $8.f;&lt;BR /&gt;INFILE RAPS lrecl=512 pad filename=source/*here*/;&lt;BR /&gt;&lt;BR /&gt;INPUT&lt;BR /&gt;@1 RecordID $char3. @;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IF RecordID = "AAA" THEN DO; /*File Header*/&lt;BR /&gt;RETAIN SubmitterID FileID TransactionDate ProductionTestIndicator ICDver;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(A lot of irrelevant import/formatting code here similar to above INPUT part...)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;file_name = source;&lt;BR /&gt;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;field_name = '/sasdata' for every row. It seems to be truncating at 8 characters but I was explicit with 100.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas why its not running correctly?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 01:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590244#M168893</guid>
      <dc:creator>gmuersch</dc:creator>
      <dc:date>2019-09-20T01:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590246#M168895</link>
      <description>&lt;P&gt;Here is the exact code I am running:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FILENAME RAPS "/sasdata/mra/users/gmu/projects/IMPORT_RRR_AUTO/IMPORT_FILES/*RAPS_RETURN_FLAT_SH9698*.DAT";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA append;&lt;BR /&gt;format file_name $100. update_date $8.f;&lt;BR /&gt;INFILE RAPS lrecl=512 pad filename=source;&lt;BR /&gt;&lt;BR /&gt;INPUT&lt;BR /&gt;@1 RecordID $char3. @;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IF RecordID = "AAA" THEN DO; /*File Header*/&lt;BR /&gt;RETAIN SubmitterID FileID TransactionDate ProductionTestIndicator ICDver;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(A lot of irrelevant import/formatting code here similar to above INPUT part...)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;file_name = source;&lt;BR /&gt;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;field_name = '/sasdata' for every row. It seems to be truncating at 8 characters but I was explicit with 100.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas why its not running correctly?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 02:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590246#M168895</guid>
      <dc:creator>gmuersch</dc:creator>
      <dc:date>2019-09-20T02:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590272#M168904</link>
      <description>&lt;P&gt;Instead of format, use a length statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length file_name source $100;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Keep in mind that SAS uses a default length of 8 for all variables unless no other is specified.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 06:07:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590272#M168904</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-20T06:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590462#M168990</link>
      <description>&lt;P&gt;Thank you it is working now, I didnt realize I needed to set the length for source as well. Im still learning sas and programming in general, really appreciate all the help in this thread&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 16:17:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590462#M168990</guid>
      <dc:creator>gmuersch</dc:creator>
      <dc:date>2019-09-20T16:17:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the the specific file an obs came from while using a wildcard on the filename</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590467#M168991</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/276861"&gt;@gmuersch&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you it is working now, I didnt realize I needed to set the length for source as well. Im still learning sas and programming in general, really appreciate all the help in this thread&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It was actually the SOURCE variable that you needed to make sure was long enough since the simple assignment statement like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename = source ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is the first place the FILENAME is referenced will cause SAS to create FILENAME as the same length as SOURCE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that a FORMAT statement is for attaching formats to your variables to control how they are displayed as text. Most variables do not need formats attached as SAS knows how to display character strings and numbers. Trying to use it to DEFINE the variable only works as a side effect when the FORMAT statement is the first place where the variable is seen.&amp;nbsp; When that happens SAS will guess that the variables length should match the width of the format that is being attached to it.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 16:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-the-specific-file-an-obs-came-from-while-using-a/m-p/590467#M168991</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-20T16:44:59Z</dc:date>
    </item>
  </channel>
</rss>

