<?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: Identify first and last record in a file when reading a list of text files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407457#M99297</link>
    <description>&lt;P&gt;Although &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;'s reply is an elegant use of the FILEVAR and EOF options, there is a relatively simple way to look-ahead for an incoming file change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just add a second FILENAME statement with the same input as the primary filename statement.&amp;nbsp; Then, in the data step, use it in a 2nd INFILE statement with a FIRSTOBS=2 option, and followed by a dummy INPUT statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename myfiles ('c:\temp\file1.txt','c:\temp\file2.txt','c:\temp\file3.txt');
filename myfiles2 (myfiles);

data want ;
  length _fvar _fvar2 $24;
  infile myfiles filename=_fvar end=_end;
  input x  ;

  infile myfiles2 filename=_fvar2 firstobs=2;
  if _end=0 then input ;
  else _fvar2=' ';

  begin=(_fvar^=lag(_fvar));
  end=(_fvar^=_fvar2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 25 Oct 2017 20:20:05 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-10-25T20:20:05Z</dc:date>
    <item>
      <title>Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407290#M99233</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm&amp;nbsp; reading a list of text files, and would like a way to identify whether a record I am reading is the first record of a file or not, and whether it is the last record of a file or not.&amp;nbsp; I read the options for the infile statement, but can't seem to get what I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample have three files like:&lt;/P&gt;
&lt;PRE&gt;file1.txt
1
2
3

file2.txt
40
50
60
70

file3.txt
800
900
1000&lt;/PRE&gt;
&lt;P&gt;WANT data like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;  id    first   last

   1      1       0
   2      0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0
   3      0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1
  40      1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0
  50      0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0
  60      0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0
  70      0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1
 800      1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0
 900      0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0
1000      0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can get FIRST by comparing each filename to the lag of filename:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename myfiles("d:\junk\file1.txt" "d:\junk\file2.txt" "d:\junk\file3.txt" );&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want;
  length filename $20;
  infile myfiles filename=filename;
  input id;
  first=filename ne lag(filename);
*  last= ??? ;
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;But how can I get LAST?&amp;nbsp; Do I need to do some sort of lookahead?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Big picture,&amp;nbsp;before I start reading from a new file, I want to do some setup stuff.&amp;nbsp; After I have read the last record from a file I want to do some post processing stuff.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 14:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407290#M99233</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-25T14:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407309#M99238</link>
      <description>Consider using INFILE statement option FILEVAR where you can use END to detect EOF.</description>
      <pubDate>Wed, 25 Oct 2017 14:47:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407309#M99238</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-10-25T14:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407323#M99246</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That gives it a nice DoW loop structure to allow preprocessing and postprocessing of each file.&amp;nbsp; Should be cleaner than all the RETAINING I have been doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   length myfile $ 300;
   input myfile $ ;
   infile dummy filevar=myfile end=done; 

   put "Pre-processing " myfile=; 
   do while(not done);
     input id ;
     put (id myfile)(=);
     output;
   end;
   put "Post-processing " myfile=;

   datalines;
d:\junk\file1.txt
d:\junk\file2.txt
d:\junk\file3.txt
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Oct 2017 15:13:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407323#M99246</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-25T15:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407405#M99273</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That gives it a nice DoW loop structure to allow preprocessing and postprocessing of each file.&amp;nbsp; Should be cleaner than all the RETAINING I have been doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   length myfile $ 300;
   input myfile $ ;
   infile dummy filevar=myfile end=done; 

   put "Pre-processing " myfile=; 
   do while(not done);
     input id ;
     put (id myfile)(=);
     output;
   end;
   put "Post-processing " myfile=;

   datalines;
d:\junk\file1.txt
d:\junk\file2.txt
d:\junk\file3.txt
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes a lot less fiddly. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &amp;nbsp; You could even use an INFILE DUMMY PIPE FILEVAR='command to return file names like DIR' in place of the names you are reading from CARDS. &amp;nbsp; Perhaps more useful if you have many file to read.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 18:14:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407405#M99273</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-10-25T18:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407457#M99297</link>
      <description>&lt;P&gt;Although &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;'s reply is an elegant use of the FILEVAR and EOF options, there is a relatively simple way to look-ahead for an incoming file change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just add a second FILENAME statement with the same input as the primary filename statement.&amp;nbsp; Then, in the data step, use it in a 2nd INFILE statement with a FIRSTOBS=2 option, and followed by a dummy INPUT statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename myfiles ('c:\temp\file1.txt','c:\temp\file2.txt','c:\temp\file3.txt');
filename myfiles2 (myfiles);

data want ;
  length _fvar _fvar2 $24;
  infile myfiles filename=_fvar end=_end;
  input x  ;

  infile myfiles2 filename=_fvar2 firstobs=2;
  if _end=0 then input ;
  else _fvar2=' ';

  begin=(_fvar^=lag(_fvar));
  end=(_fvar^=_fvar2);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Oct 2017 20:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407457#M99297</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-25T20:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407461#M99299</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was thinking there must be an infile option that does what I had hoped.&amp;nbsp; That not being there, your approach is what I was trying to come up with.&amp;nbsp; Looks like the same&amp;nbsp;as one of&amp;nbsp;the &amp;nbsp;"standard" look-ahead approaches for SAS datasets, I just failed to make it work with infile.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sadly I missed your leads and lags talk at BASUG earlier this year.&amp;nbsp; If I had seen I'd, I'm sure I would have learned this and more.&amp;nbsp;&amp;nbsp;: )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-Q.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 20:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407461#M99299</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-25T20:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407463#M99301</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;:&amp;nbsp; I'm giving the Lags and Leads talk again at SESUG next month.&amp;nbsp; If you're coming please find me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2017 20:35:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407463#M99301</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-10-25T20:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: Identify first and last record in a file when reading a list of text files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407465#M99303</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; Excellent!  I'll be there, and will definitely track you down.</description>
      <pubDate>Wed, 25 Oct 2017 20:39:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-first-and-last-record-in-a-file-when-reading-a-list-of/m-p/407465#M99303</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-25T20:39:02Z</dc:date>
    </item>
  </channel>
</rss>

