<?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: Explanation of behaviour when conditionally executing an infile statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742244#M232134</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I deduced the same about compile time action when I couldn't see the value of variable &lt;FONT face="courier new,courier"&gt;file_found&lt;/FONT&gt; in the log, but assumed the &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement was being executed, as opposed to the literal file name being checked, as you mentioned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll see if anyone else has anything to add.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
    <pubDate>Tue, 18 May 2021 17:12:05 GMT</pubDate>
    <dc:creator>Amir</dc:creator>
    <dc:date>2021-05-18T17:12:05Z</dc:date>
    <item>
      <title>Explanation of behaviour when conditionally executing an infile statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742219#M232111</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using Enterprise Guide 8.2, SAS 9.4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm looking for an explanation of the behaviour shown herein, e.g., documented behaviour (with a link).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to check the existence of a file using the &lt;FONT face="courier new,courier"&gt;fileexist()&lt;/FONT&gt; function before I attempt to read it &lt;EM&gt;conditionally&lt;/EM&gt; using the &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement, as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let my_file = /file/does/not/exist;

data tmp;
   length record $ 1000;

   /* check if file exists */
   file_found = fileexist("&amp;amp;my_file");

   putlog file_found;

   /* if file exists then read contents */
   if file_found then
   do;
      infile "&amp;amp;my_file" end = last_record;

      do until (last_record);

         input;

         record = _infile_;

         output;
      end;
   end;
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;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code works OK when the file exists, but when a file does not exist I get the error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;
28         %let my_file = /file/does/not/exist;
29         
30         data tmp;
31            length record $ 1000;
32         
33            /* check if file exists */
34            file_found = fileexist("&amp;amp;my_file");
35         
36            putlog file_found;
37         
38            /* if file exists then read contents */
39            if file_found then
40            do;
41               infile "&amp;amp;my_file" end = last_record;
42         
43               do until (last_record);
44         
45                  input;
46         
47                  record = _infile_;
48         
49                  output;
50               end;
51            end;
52         run;

&lt;FONT color="#FF6600"&gt;ERROR: Physical file does not exist, /file/does/not/exist.&lt;/FONT&gt;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TMP may be incomplete.  When this step was stopped there were 0 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have achieved this in the past using macro code, but I don't see why the error message is appearing when the &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement is not being executed in data step code as it is an executable statement as opposed to a declarative one (see list at end of&amp;nbsp;&lt;A title="Executable and Declarative Statements" href="https://documentation.sas.com/doc/en/pgmsascdc/v_008/lestmtsref/p1bp8z934fjg2pn1rjlh9vrqq0iv.htm#n1ar8fd7c4qfqen1iknj7kaaiow2" target="_blank" rel="noopener"&gt;Executable and Declarative Statements&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can use alternative coding methods, but as I said at the start,&amp;nbsp;I'm looking for an explanation of the behaviour shown herein, e.g., documented behaviour (with a link).&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;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 15:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742219#M232111</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2021-05-18T15:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: Explanation of behaviour when conditionally executing an infile statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742224#M232116</link>
      <description>&lt;P&gt;If you use a literal file name, the existence of the file is checked at compile time; you can see this because the PUT of your file_found variable is never executed.&lt;/P&gt;
&lt;P&gt;You can avoid this by making the filename dynamic through the FILEVAR= option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tmp;
fname = "&amp;amp;my_file.";
length record $ 1000;
file_found = fileexist(fname);
put file_found=;
if file_found
then do;
  infile dummy filevar=fname end=last_record;
  do until (last_record);
    input;
    record = _infile_;
    output;
  end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 May 2021 16:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742224#M232116</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-18T16:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: Explanation of behaviour when conditionally executing an infile statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742244#M232134</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I deduced the same about compile time action when I couldn't see the value of variable &lt;FONT face="courier new,courier"&gt;file_found&lt;/FONT&gt; in the log, but assumed the &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement was being executed, as opposed to the literal file name being checked, as you mentioned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll see if anyone else has anything to add.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 17:12:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742244#M232134</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2021-05-18T17:12:05Z</dc:date>
    </item>
    <item>
      <title>Re: Explanation of behaviour when conditionally executing an infile statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742262#M232142</link>
      <description>&lt;P&gt;There are actions that happen before the data step starts running for INFILE statement.&amp;nbsp; Are you at the end of the file yet (END= option) for example.&amp;nbsp; In particular when the name is fixed the file is checked for existence. Note that to SAS the value of the macro variable is a fixed string since it does not know you used a macro variable to generate it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the FILEVAR= option to have the filename be dynamic and SAS cannot (and so will not) check if the file exists before the data step starts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let my_file = /file/does/not/exist;
data tmp;
   length record $ 1000;
   filename="&amp;amp;my_file";
   if fileexist(filename) then do;
     infile dummy filevar=filename truncover ;
     input record $char1000. ;
  end;
  else do;
    putlog "File not found. " filename= :$quote. ;
    stop;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 May 2021 18:42:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/742262#M232142</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-18T18:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Explanation of behaviour when conditionally executing an infile statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/743908#M232993</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apologies for the delayed response. I think linking the behaviour with that of how the &lt;FONT face="courier new,courier"&gt;end=&lt;/FONT&gt; option is processed helps compartmentalise how to think of this, even though the more general &lt;EM&gt;file-specification&lt;/EM&gt; is not an optional argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also tried using the &lt;FONT face="courier new,courier"&gt;filename()&lt;/FONT&gt; function, but encountered similar issues even though a &lt;EM&gt;fileref&lt;/EM&gt; was used in the &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement.&amp;nbsp;For example, in the changed code below I commented out the code in the &lt;FONT face="courier new,courier"&gt;do-end&lt;/FONT&gt; block of the &lt;FONT face="courier new,courier"&gt;if&lt;/FONT&gt; statement and the &lt;FONT face="courier new,courier"&gt;file_found&lt;/FONT&gt; flag is displayed in the log, but uncommenting the code does not display the &lt;FONT face="courier new,courier"&gt;file_found&lt;/FONT&gt; flag, even though the explicit file reference is first assigned to a data step variable before being used in the &lt;FONT face="courier new,courier"&gt;filename()&lt;/FONT&gt; function before being indirectly referenced in the &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement using a &lt;EM&gt;fileref&lt;/EM&gt;. This seems like a lot of evaluation before running the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let my_file = /file/does/not/exist;

data tmp;
   length record $ 1000;

   my_file = "&amp;amp;my_file";
   file_found = filename('in_file',my_file);

   putlog file_found=;

   /* if file exists then read contents */
   if file_found eq 0 then
   do;
/*      infile in_file end = last_record;*/
/**/
/*      do until (last_record);*/
/**/
/*         input;*/
/**/
/*         record = _infile_;*/
/**/
/*         output;*/
/*      end;*/
   end;
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;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 14:58:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/743908#M232993</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2021-05-26T14:58:56Z</dc:date>
    </item>
    <item>
      <title>Re: Explanation of behaviour when conditionally executing an infile statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/743911#M232994</link>
      <description>&lt;P&gt;Not sure what you are saying here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use a fileref in the INFILE statement I would assume that the existence of the FILEREF will be checked when the data step is compiled (or really during what ever happends before it start really running) and that the fileref would be opened for read access.&amp;nbsp; If you then tried to redefine the fileref using the FILENAME() function while the step is running I would assume you would get an error saying the fileref cannot be redefined because it is already in use (by this data step!).&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 15:03:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/743911#M232994</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-26T15:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: Explanation of behaviour when conditionally executing an infile statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/744041#M233036</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Following the suggestions already given about using the &lt;FONT face="courier new,courier"&gt;filevar=&lt;/FONT&gt; option, I thought this degree of separation from using the literal file name in the &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement would also work for a &lt;EM&gt;fileref&lt;/EM&gt; that is assigned using the &lt;FONT face="courier new,courier"&gt;filename()&lt;/FONT&gt; function, but this was evidently not the case. I wasn't trying to redefine the &lt;EM&gt;fileref&lt;/EM&gt;&amp;nbsp;at any point in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you implied, there seems to be more going on than just compile-time actions and run-time actions.&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;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 20:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Explanation-of-behaviour-when-conditionally-executing-an-infile/m-p/744041#M233036</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2021-05-26T20:44:24Z</dc:date>
    </item>
  </channel>
</rss>

