<?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: Conditionally create a file in a data statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724543#M224944</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13728"&gt;@SASJedi&lt;/a&gt;&amp;nbsp;Thank you, that definitely worked.&amp;nbsp; However, I don't entirely understand why.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, if I modify your code to put the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement inside the conditional, it still creates the output file.&amp;nbsp; This means that the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement seems to execute no matter what.&amp;nbsp; Does the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement in your version execute, but no output file is created because &lt;STRONG&gt;fileloc&lt;/STRONG&gt; is undefined?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Second, in your code, how is it that the value of &lt;STRONG&gt;nobs&lt;/STRONG&gt; is available before you define it?&amp;nbsp; On the first iteration of the implicit loop of the data set, the conditional tests the value of &lt;STRONG&gt;nobs&lt;/STRONG&gt; before the &lt;STRONG&gt;SET&lt;/STRONG&gt; statement establishes it.&amp;nbsp; Shouldn't that throw an error?&amp;nbsp; Or is the value of &lt;STRONG&gt;nobs&lt;/STRONG&gt; determined during compilation and not during execution?&lt;/P&gt;</description>
    <pubDate>Mon, 08 Mar 2021 16:03:20 GMT</pubDate>
    <dc:creator>Paul_de_Barros</dc:creator>
    <dc:date>2021-03-08T16:03:20Z</dc:date>
    <item>
      <title>Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724490#M224934</link>
      <description>&lt;P&gt;I'm trying to use a data step to create a file, but only create it if there are records to be written to the file.&amp;nbsp; Since the data step exits when the SET statement runs and there are no more records, I though that putting the FILE statement after the SET statement would prevent the file from being created if there were no records in the dataset named in the SET statement.&amp;nbsp; However, the code below create an empty text file even when the rowcount of Work.dataset1 is zero.&amp;nbsp; Am I doing something wrong?&amp;nbsp; Why is the FILE statement executing at all?&amp;nbsp; Is what I am trying to do feasible, or should I be writing macro code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%Let filepath = /data/file.txt;

DATA _NULL_;
    SET Work.dataset1;
    FILE "&amp;amp;FilePath";
    IF _N_ = 1 THEN DO;
        put 'Line 1';
        put 'Line 2';
        put ' ';
        put 'First Last DOB'; /* These are the column headers. */
    END;
    put First ' ' Last ' ' DOB;
RUN;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 12:57:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724490#M224934</guid>
      <dc:creator>Paul_de_Barros</dc:creator>
      <dc:date>2021-03-08T12:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724498#M224938</link>
      <description>&lt;P&gt;Here is a solution set up for testing with the SASHELP.CLASS data set:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Make test data - uncomment stop to make empty data set */
data dataset1;
/*   stop;*/
   set sashelp.class(obs=1);
run;

DATA _NULL_;
    /* If there are observations, proceed*/
    if nobs&amp;gt;0 then fileloc="&amp;amp;FilePath";
    /*Otherwise, quit here */
    else stop;
    SET Work.dataset1 nobs=nobs;
    file x filevar=fileloc;
    IF _N_ = 1 THEN DO;
        put 'First Age'; /* These are the column headers. */
    END;
    put Name ' ' Age;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Mar 2021 13:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724498#M224938</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-03-08T13:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724543#M224944</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13728"&gt;@SASJedi&lt;/a&gt;&amp;nbsp;Thank you, that definitely worked.&amp;nbsp; However, I don't entirely understand why.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, if I modify your code to put the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement inside the conditional, it still creates the output file.&amp;nbsp; This means that the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement seems to execute no matter what.&amp;nbsp; Does the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement in your version execute, but no output file is created because &lt;STRONG&gt;fileloc&lt;/STRONG&gt; is undefined?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Second, in your code, how is it that the value of &lt;STRONG&gt;nobs&lt;/STRONG&gt; is available before you define it?&amp;nbsp; On the first iteration of the implicit loop of the data set, the conditional tests the value of &lt;STRONG&gt;nobs&lt;/STRONG&gt; before the &lt;STRONG&gt;SET&lt;/STRONG&gt; statement establishes it.&amp;nbsp; Shouldn't that throw an error?&amp;nbsp; Or is the value of &lt;STRONG&gt;nobs&lt;/STRONG&gt; determined during compilation and not during execution?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 16:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724543#M224944</guid>
      <dc:creator>Paul_de_Barros</dc:creator>
      <dc:date>2021-03-08T16:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724557#M224946</link>
      <description>&lt;P&gt;The posted code you are replying to was the test when the dataset was NOT empty, so the file should have been made.&lt;/P&gt;
&lt;P&gt;The NOBS= option on the SET statement will populate the value of the variable before the data step actually started iterating.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But there is no need to test the number of observations (in this case).&amp;nbsp; When you use the FILEVAR= option on the FILE statement SAS will not attempt to open the file until the FILE statement actually executes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a working example:&amp;nbsp; The first time when OBS=0 dataset option the step stops before ever getting to the FILE statement so the file is not created.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let fname=%sysfunc(pathname(work))/test.txt;

data _null_;
  set sashelp.class(obs=0);
  filevar="&amp;amp;fname";
  file dummy filevar=filevar;
  put 'hello there';
run;

data _null_;
  found=fileexist("&amp;amp;fname");
  put found=;
run;

data _null_;
  set sashelp.class(obs=2);
  filevar="&amp;amp;fname";
  file dummy filevar=filevar;
  put 'hello there';
run;

data _null_;
  found=fileexist("&amp;amp;fname");
  put found=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Mar 2021 16:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724557#M224946</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-08T16:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724559#M224947</link>
      <description>&lt;P&gt;This is making it a lot clearer, thank you.&amp;nbsp; A few more things, if you don't mind me continuing to pester you:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I should have noted in my previous reply that I had uncommented the &lt;STRONG&gt;stop&lt;/STRONG&gt; already.&lt;/LI&gt;&lt;LI&gt;You mentioned that the &lt;STRONG&gt;NOBS=&lt;/STRONG&gt; option on the &lt;STRONG&gt;SET&lt;/STRONG&gt; statement will populate the value of the variable before the data step starts iterating.&amp;nbsp; Does this mean that it gets populated during the compilation phase?&amp;nbsp; Or is the execution phase more complicated than I thought, with SAS looking for statements to run before the rest, regardless of their order in the code?&amp;nbsp; Or is there another phase between compilation and execution?&lt;/LI&gt;&lt;LI&gt;You mentioned that, when the &lt;STRONG&gt;FILEVAR=&lt;/STRONG&gt; option is used on the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement, SAS won't try to open the file until the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement actually executes.&amp;nbsp; I assume this means that, &lt;EM&gt;without&lt;/EM&gt; the &lt;STRONG&gt;FILEVAR=&lt;/STRONG&gt; option included, SAS attempts to open the file &lt;EM&gt;before&lt;/EM&gt; the &lt;STRONG&gt;FILE&lt;/STRONG&gt; statement executes.&amp;nbsp; When does it do that?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 16:38:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724559#M224947</guid>
      <dc:creator>Paul_de_Barros</dc:creator>
      <dc:date>2021-03-08T16:38:16Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724561#M224948</link>
      <description>&lt;P&gt;I don't know exactly the methods SAS uses internally to do run the data step.&amp;nbsp; For all we know that are 47 steps between definition and execution.&amp;nbsp; But there are definitely statements like SET and FILE that have both executable parts and definition impacts on the data step.&amp;nbsp; &amp;nbsp;Even a simple assignment statement can have an impact on the definition, for example if the variables referenced were never defined before the compiler sees that statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the FILE statement the compiler must notice that the target filename is static and optimize the data step to run that first (or at least open the file first).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Mar 2021 17:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/724561#M224948</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-08T17:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/726129#M225634</link>
      <description>&lt;P&gt;The most common form of the FILE statement uses a direct path to the file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   file "/mypath/myfolder/myfile.txt";
   set work.cars;
   put "TEST";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or a fileref previously assigned to a file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename thisFile "/mypath/myfolder/myfile.txt";
data _null_;
   file thisFile;
   put "TEST";
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In both of those cases, the DATA step file reference is static - so when the DATA step complies, it knows &lt;EM&gt;at compile time&lt;/EM&gt; where to write the PUT values. But what if you wanted to conditionally determine where to write? For this, we have to delay locating the output file until execution time - so we use FILEVAR instead of the hard-coded file location.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, consider the code below. During the compile phase, the &lt;STRONG&gt;sashelp.class&lt;/STRONG&gt; &lt;EM&gt;descriptor portion&lt;/EM&gt; is read in so that the PDV variables can be properly set up for processing. But when the execution phase begins, the STOP statement stops the DATA step before the SET statement can execute, so no observations are written to&amp;nbsp;&lt;STRONG&gt;dataset1&lt;/STRONG&gt; - the data set is empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data dataset1;
   stop;
   set sashelp.class(obs=1);
run;
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, this DATA step executes:&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _NULL_;
    /* If there are observations, proceed*/
    if nobs&amp;gt;0 then fileloc="&amp;amp;FilePath";
    /*Otherwise, quit here */
    else stop;
    SET dataset1 nobs=nobs;
    file x filevar=fileloc;
    IF _N_ = 1 THEN DO;
        put 'First Age'; /* These are the column headers. */
    END;
    put Name ' ' Age;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;At compile time,&amp;nbsp;the compiler notes that the FILE statement is using FILEVAR, and defers locating the output file until the FILE statement executes during the execution phase. This means that the output file isn't created yet. As usual, the compiler reads the&amp;nbsp;descriptor portion for the input dataset, in this case&amp;nbsp;&lt;STRONG&gt;dataset1,&lt;/STRONG&gt;&amp;nbsp;in order to set up the PDV. But because the descriptor data also contains the number of observations in the data set, and we have used the &lt;STRONG&gt;nobs=&lt;/STRONG&gt; option on the SET statement, the compiler also populates the value of NOBS during the compile phase. So when the execution phase starts, we already know the number of observations in &amp;nbsp;&lt;STRONG&gt;dataset1&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The very first thing our DATA step does is check the value of NOBS. If there are observations, the FILEVAR value is set. When the FILE statement subsequently executes, it will create our output file. But if there are no observations, the DATA step STOP statement ends processing immediately. Beacuse the FILE statement never executed, the output file was not created.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope that clarifies the code for you.&lt;/P&gt;
&lt;P&gt;May the SAS be with you!&lt;BR /&gt;Mark&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>Sun, 14 Mar 2021 14:47:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/726129#M225634</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2021-03-14T14:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally create a file in a data statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/726234#M225666</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13728"&gt;@SASJedi&lt;/a&gt;, this was fantastic, thank you.</description>
      <pubDate>Mon, 15 Mar 2021 11:06:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-create-a-file-in-a-data-statement/m-p/726234#M225666</guid>
      <dc:creator>Paul_de_Barros</dc:creator>
      <dc:date>2021-03-15T11:06:18Z</dc:date>
    </item>
  </channel>
</rss>

