<?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 import if file exists and if it doesn't exists then create  empty data set in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738382#M230323</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to import if file exists and if it doesn't exists then I want to create an empty data set.&lt;/P&gt;
&lt;P&gt;What is wrong in my code?&lt;/P&gt;
&lt;P&gt;How can I solve it please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=20120404+20210417+20210319;
%let n=3;

%macro ttt; 
%do j=1 %to &amp;amp;n.;
%let YYYYMMDD=%scan(&amp;amp;List.,&amp;amp;j.,+);
%if %sysfunc(fileexist("/path/A&amp;amp;YYYYMMDD."))
%then %do;   
DATA t&amp;amp;YYYYMMDD.;
INFILE "/path/A&amp;amp;YYYYMMDD."
firstobs=2;
INPUT 
@1  X1 8. 
@10 X2 2. 
@13 X3 3. 
@17 X4 9. 
;
RUN;

%else %do;
Data t&amp;amp;YYYYMMDD.;
X1=.;
X2=.;
X3=.;
X4=.;
Run;


%end;
%end;
%mend ttt;
%ttt; 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 02 May 2021 11:01:50 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2021-05-02T11:01:50Z</dc:date>
    <item>
      <title>import if file exists and if it doesn't exists then create  empty data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738382#M230323</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to import if file exists and if it doesn't exists then I want to create an empty data set.&lt;/P&gt;
&lt;P&gt;What is wrong in my code?&lt;/P&gt;
&lt;P&gt;How can I solve it please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=20120404+20210417+20210319;
%let n=3;

%macro ttt; 
%do j=1 %to &amp;amp;n.;
%let YYYYMMDD=%scan(&amp;amp;List.,&amp;amp;j.,+);
%if %sysfunc(fileexist("/path/A&amp;amp;YYYYMMDD."))
%then %do;   
DATA t&amp;amp;YYYYMMDD.;
INFILE "/path/A&amp;amp;YYYYMMDD."
firstobs=2;
INPUT 
@1  X1 8. 
@10 X2 2. 
@13 X3 3. 
@17 X4 9. 
;
RUN;

%else %do;
Data t&amp;amp;YYYYMMDD.;
X1=.;
X2=.;
X3=.;
X4=.;
Run;


%end;
%end;
%mend ttt;
%ttt; 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 May 2021 11:01:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738382#M230323</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-05-02T11:01:50Z</dc:date>
    </item>
    <item>
      <title>Re: import if file exists and if it doesn't exists then create  empty data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738385#M230326</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;If you claim that something does not work. Then please post the LOG. That will facilitate things for everyone!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, I do not think anything is wrong with the the FILEEXIST function, but there's something wrong with the macro definition.&lt;/P&gt;
&lt;P&gt;There are 3 %DO's but only 2 %END's.&lt;/P&gt;
&lt;P&gt;Therefore, I get this error when I run your code:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;ERROR: There is no matching %IF statement for the %ELSE.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;ERROR: A dummy macro will be compiled.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sun, 02 May 2021 11:21:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738385#M230326</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-02T11:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: import if file exists and if it doesn't exists then create  empty data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738390#M230331</link>
      <description>&lt;P&gt;Based on what you've posted below code that should work for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic;
%let list=20120404 20210417 20210319;
%let path=c:\\temp;

%macro ttt;
  %local path_delim;

  %if %sysfunc(findc(%nrbquote(%path),%nrbquote(\))) %then
    %let path_delim=\;
  %else %let path_delim=/;
  %local n;
  %let n=%sysfunc(countw(&amp;amp;list));

  %do j=1 %to &amp;amp;n.;
    %let yyyymmdd=%scan(&amp;amp;list.,&amp;amp;j.);
    %put &amp;amp;=yyyymmdd;

    %if %sysfunc(fileexist("&amp;amp;path.&amp;amp;path_delim.A&amp;amp;yyyymmdd..txt")) %then
      %do;
        data t&amp;amp;yyyymmdd.;
          infile "&amp;amp;path.&amp;amp;path_delim.A&amp;amp;yyyymmdd..txt" firstobs=2 truncover;
          input 
            @1  x1 :8. 
            @10 x2 :2. 
            @13 x3 :3. 
            @17 x4 :9. 
          ;
        run;
      %end;
    %else
      %do;
        data t&amp;amp;yyyymmdd.;
          stop;
          length x1 x2 x3 x4 8;
        run;
      %end;
  %end;
%mend ttt;

%ttt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For the cases where you want to write an empty data step use a STOP statement as else the data step will first go through the compile phase where it creates the table structure (which is what you want) but then also goes trough the first iteration of the execution phase and will write an empty row to the table (which you don't want). Using a STOP statement prevents this from happening.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The execution phase will still start but end as soon as it hits the stop statement - and as it never reaches the RUN statement there won't be an implicit OUTPUT (writing the empty row).&lt;/P&gt;
&lt;P&gt;You could position the STOP statement anywhere in the data step. I prefer to have it at the beginning for such cases to make it clear in the code that we only want the result from the compilation phase (all commands like keep, put, length, attrib and the like take effect in the compilation phase and though can come after the STOP statement that only comes into play during the execution phase).&lt;/P&gt;</description>
      <pubDate>Sun, 02 May 2021 12:03:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738390#M230331</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-05-02T12:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: import if file exists and if it doesn't exists then create  empty data set</title>
      <link>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738396#M230336</link>
      <description>&lt;P&gt;It might be clearer to put the %IF/%THEN inside the data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro ttt; 
  %local j yyyymmdd ;
  %do j=1 %to %sysfunc(countw(&amp;amp;list.,+);
    %let YYYYMMDD=%scan(&amp;amp;List.,&amp;amp;j.,+);
DATA t&amp;amp;YYYYMMDD.;
  LENGTH X1-X4 8 ;
  %if %sysfunc(fileexist("/path/A&amp;amp;YYYYMMDD.")) %then %do;   
  INFILE "/path/A&amp;amp;YYYYMMDD." firstobs=2 truncover ;
  INPUT 
    @1  X1 8. 
    @10 X2 2. 
    @13 X3 3. 
    @17 X4 9. 
  ;
  %end;
  %else %do;
  STOP;
  %end;
RUN;
%mend ttt;

%let list=20120404+20210417+20210319;
%ttt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 02 May 2021 12:40:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/import-if-file-exists-and-if-it-doesn-t-exists-then-create-empty/m-p/738396#M230336</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-02T12:40:53Z</dc:date>
    </item>
  </channel>
</rss>

