<?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: read same file twice inside datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595769#M171475</link>
    <description>&lt;P&gt;If I may, I think the TEMP filename would be a bit more OS independent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename f TEMP;
filename f LIST;
data _null_; /* force to create file */
  file f ;
  put    ;
  stop   ;
run;

data WORK.OUT;
	infile datalines;
	length INP $ 100.;
	input INP $ ; 

  infile dummy filevar=INP end=EOF;
  do while(^EOF);
    input VAR1 VAR2 VAR3;
    output;
  end;
  INP = pathname('f');
  infile dummy filevar=INP end=EOF;
  
datalines;
E:/SAS_WORK_5400/file1.txt
E:/SAS_WORK_5400/file2.txt
E:/SAS_WORK_5400/file2.txt
E:/SAS_WORK_5400/file3.txt
;
run;

filename f CLEAR;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 11 Oct 2019 15:10:39 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2019-10-11T15:10:39Z</dc:date>
    <item>
      <title>read same file twice inside datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595732#M171450</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I´m having trouble reading the same file twice inside a datastep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.OUT;
	infile datalines;
	length INP $40;
	input INP $;
	infile dummy filevar=INP end=EOF;
	do while(^EOF);
	  input VAR1 VAR2 VAR3;
	  output;
	end;
datalines;
/path/to/file/file1.txt
&lt;STRONG&gt;/path/to/file/file2.txt
/path/to/file/file2.txt&lt;/STRONG&gt;
/path/to/file/file3.txt
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Each file is read and output once even though file2.txt should have been output twice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is just exemplary. The real code depends on a list of items in a dataset for which (depending on a parameter) different external files should be read. If the same file occurs two times in a row it is not read again.&lt;/P&gt;
&lt;P&gt;If files alternate like below, they are read every time:&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/path/to/file/file1.txt&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/path/to/file/file&lt;STRONG&gt;2&lt;/STRONG&gt;.txt&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/path/to/file/file3.txt&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;/path/to/file/file&lt;STRONG&gt;2&lt;/STRONG&gt;.txt&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;What am I missing? Do I need to reset a pointer or the infile? Or is this not intended?&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;Any help is appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/edit: formatting...&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 13:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595732#M171450</guid>
      <dc:creator>Maze</dc:creator>
      <dc:date>2019-10-11T13:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: read same file twice inside datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595741#M171457</link>
      <description>&lt;P&gt;Yes. You need to run the INFILE statement twice to "clear" it. So point it to some other file between each real file in your list.&amp;nbsp; Try using the bit bucket on Unix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WORK.OUT;
  infile datalines;
  length INP filevar $40;
  input INP $;
  do filevar=inp,'/dev/null';
    infile dummy filevar=filevar end=EOF;
    do while(^EOF);
      input VAR1 VAR2 VAR3;
      output;
    end;
  end;
datalines;
/path/to/file/file1.txt
/path/to/file/file2.txt
/path/to/file/file2.txt
/path/to/file/file3.txt
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 14:41:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595741#M171457</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-11T14:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: read same file twice inside datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595742#M171458</link>
      <description>&lt;P&gt;From the documentation&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H4 class="xis-argument"&gt;FILEVAR=&lt;SPAN class="xis-userSuppliedValue"&gt;variable&lt;/SPAN&gt;&lt;/H4&gt;
&lt;DIV class="xis-argumentDescription"&gt;
&lt;P class="xis-paraSimpleFirst"&gt;specifies a variable &lt;STRONG&gt;whose change in value causes the INFILE statement to close the current input file and open a new one&lt;/STRONG&gt;. When the next INPUT statement executes, it reads from the new file that the FILEVAR= variable specifies. Like automatic variables, this variable is not written to the data set.&lt;/P&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want to read the file twice?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Oct 2019 14:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595742#M171458</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2019-10-11T14:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: read same file twice inside datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595769#M171475</link>
      <description>&lt;P&gt;If I may, I think the TEMP filename would be a bit more OS independent.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename f TEMP;
filename f LIST;
data _null_; /* force to create file */
  file f ;
  put    ;
  stop   ;
run;

data WORK.OUT;
	infile datalines;
	length INP $ 100.;
	input INP $ ; 

  infile dummy filevar=INP end=EOF;
  do while(^EOF);
    input VAR1 VAR2 VAR3;
    output;
  end;
  INP = pathname('f');
  infile dummy filevar=INP end=EOF;
  
datalines;
E:/SAS_WORK_5400/file1.txt
E:/SAS_WORK_5400/file2.txt
E:/SAS_WORK_5400/file2.txt
E:/SAS_WORK_5400/file3.txt
;
run;

filename f CLEAR;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Oct 2019 15:10:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/595769#M171475</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-10-11T15:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: read same file twice inside datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/596162#M171610</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Thanks everyone for the pointers, doc is pretty clear, I don´t know why I did not see that. &lt;img id="smileymad" class="emoticon emoticon-smileymad" src="https://communities.sas.com/i/smilies/16x16_smiley-mad.png" alt="Smiley Mad" title="Smiley Mad" /&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Why do you want to read the file twice?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I have a dataset with table names that include a structure tag. Based on that tag I read the according structure file to generate a DDL script (structures may repeat!).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 05:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/596162#M171610</guid>
      <dc:creator>Maze</dc:creator>
      <dc:date>2019-10-14T05:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: read same file twice inside datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/596163#M171611</link>
      <description>&lt;P&gt;Using TEMP to allocate a name is a good idea.&lt;/P&gt;
&lt;P&gt;It is interesting that it works using two different INFILE statements.&amp;nbsp; Note that they both have to use the same variable for the FILEVAR= option and they also have to both use the same filref (DUMMY in the example).&amp;nbsp; Otherwise the second one does not cause the first to forget the filename it was reading before.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 05:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/read-same-file-twice-inside-datastep/m-p/596163#M171611</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-14T05:53:54Z</dc:date>
    </item>
  </channel>
</rss>

