<?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 Read 1 line From Multiple Files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138316#M261341</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was hoping somebody would have a simple solution to this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a directory that has a large number of files in it.&amp;nbsp; The first record of each file has a date on it, with the data following.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to read all the records from the directory for a specific month (and year).&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This would mean that I have to read all files in the directory, and keep the name of the file.&amp;nbsp; Then I would read the first line&lt;/P&gt;&lt;P&gt;to determine the date - if it is not the month that I was, I reject the file and read the next one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the file is within my date rage, I would keep the file name.&amp;nbsp; Once I have examined all files, I would then re-process just&lt;/P&gt;&lt;P&gt;the file that I retained.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This seems like a "brute force" approach and I was hoping for something a little more elegant - any ideas?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Mar 2014 14:05:04 GMT</pubDate>
    <dc:creator>OS2Rules</dc:creator>
    <dc:date>2014-03-10T14:05:04Z</dc:date>
    <item>
      <title>Read 1 line From Multiple Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138316#M261341</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was hoping somebody would have a simple solution to this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a directory that has a large number of files in it.&amp;nbsp; The first record of each file has a date on it, with the data following.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to read all the records from the directory for a specific month (and year).&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This would mean that I have to read all files in the directory, and keep the name of the file.&amp;nbsp; Then I would read the first line&lt;/P&gt;&lt;P&gt;to determine the date - if it is not the month that I was, I reject the file and read the next one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If the file is within my date rage, I would keep the file name.&amp;nbsp; Once I have examined all files, I would then re-process just&lt;/P&gt;&lt;P&gt;the file that I retained.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This seems like a "brute force" approach and I was hoping for something a little more elegant - any ideas?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 14:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138316#M261341</guid>
      <dc:creator>OS2Rules</dc:creator>
      <dc:date>2014-03-10T14:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Read 1 line From Multiple Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138317#M261342</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Brute force is the way to go, but doing via a program.&lt;/P&gt;&lt;P&gt;Get the list of file names.&amp;nbsp; I usually find this is easier with PIPEd operating system commands.&amp;nbsp; DIR /B in Windoze and ls in Unix.&lt;/P&gt;&lt;P&gt;You will need to supply input statement for reading the date out of the first line as the syntax will depend on how it is formatted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let path=mydir;&lt;/P&gt;&lt;P&gt;%let month=01JAN2014 ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data files ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; infile "ls &amp;amp;mydir/*" pipe truncover ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input filename $255. ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; filen = filename ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; infile dummy filevar=filen truncover end=eof ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if not eof then input date date9. @ ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if&amp;nbsp; date = "&amp;amp;month"d then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 14:24:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138317#M261342</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2014-03-10T14:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: Read 1 line From Multiple Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138318#M261343</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the help - it looks like brute force is the only way to go. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another question - is there a way for me to read only a single record from the file in a data step without reading the rest of the data?&amp;nbsp; Some of these files are somewhat large and I only need the header.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Currently I use something like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data one_file1;&lt;/P&gt;&lt;P&gt;infile myfile lrecl=100 pad missover;&lt;/P&gt;&lt;P&gt;input @001 text $char51.;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if _n_ = 1 then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else delete; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would rather not have to read the entire file if that is possible (option obs=1 possibly?)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 13:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138318#M261343</guid>
      <dc:creator>OS2Rules</dc:creator>
      <dc:date>2014-03-12T13:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Read 1 line From Multiple Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138319#M261344</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The code I posted before would only read one record per file because the main loop was the list of files.&lt;/P&gt;&lt;P&gt;If you are reading a single file then just add the option OBS=1 to the INFILE statement. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data one_file1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; infile myfile lrecl=100 obs=1 truncover ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; input text $char51.;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 13:25:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138319#M261344</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2014-03-12T13:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: Read 1 line From Multiple Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138320#M261345</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assuming that your files have a particular date structure, then you can get a list of files you want to process quite simply with a DOS command. &lt;/P&gt;&lt;P&gt;findstr "..MAY2013" *.txt&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So:&lt;/P&gt;&lt;P&gt;filename rob pipe 'findstr /m "..MAY2013" "s:\temp\rob\*.txt"';&lt;/P&gt;&lt;P&gt;data dir;&lt;BR /&gt;&amp;nbsp; attrib buffer format=$2000.;&lt;BR /&gt;&amp;nbsp; infile rob lrecl=30000 dsd;&lt;BR /&gt;&amp;nbsp; input buffer;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Will give me a dataset called dir which contains the path and filename of any files with .txt extension in s:\temp\rob which have a pattern --MAY2013.&amp;nbsp; I can then read those files in.&amp;nbsp; This means you wouldn't have to read each file, but does mean the data needs to be structured.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 14:19:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138320#M261345</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-03-12T14:19:31Z</dc:date>
    </item>
    <item>
      <title>Re: Read 1 line From Multiple Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138321#M261346</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately, the date is in the header of the data, not in the file name.&amp;nbsp; I also have to compare this to the date the file was created to determine if the file was rerun more than once, and keep the latest one only.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 14:23:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138321#M261346</guid>
      <dc:creator>OS2Rules</dc:creator>
      <dc:date>2014-03-12T14:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: Read 1 line From Multiple Files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138322#M261347</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The "obs=1" did the trick.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Mar 2014 14:24:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Read-1-line-From-Multiple-Files/m-p/138322#M261347</guid>
      <dc:creator>OS2Rules</dc:creator>
      <dc:date>2014-03-12T14:24:00Z</dc:date>
    </item>
  </channel>
</rss>

