<?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 Macro check if dataset is available in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916909#M361169</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to check if dataset Myfile is already in work library or not. If it is not there yet, create that file.&lt;/P&gt;
&lt;P&gt;The idea can be seen in the code below.&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_file_exist;
	%if FILE IS NOT THERE %then %do;
		data Myfile; set _NULL_;
		run;
	%end;
%Mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Feb 2024 04:57:03 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2024-02-20T04:57:03Z</dc:date>
    <item>
      <title>Macro check if dataset is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916909#M361169</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to check if dataset Myfile is already in work library or not. If it is not there yet, create that file.&lt;/P&gt;
&lt;P&gt;The idea can be seen in the code below.&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro check_file_exist;
	%if FILE IS NOT THERE %then %do;
		data Myfile; set _NULL_;
		run;
	%end;
%Mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 04:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916909#M361169</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2024-02-20T04:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Macro check if dataset is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916914#M361172</link>
      <description>&lt;P&gt;You can try this (If Myfile's file extension is sas7bdat):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%Macro check_file_exist;
	%if %sysfunc(exist(work.Myfile))=0 %then %do;
		data Myfile; set _NULL_;
		run;
	%end;
%Mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If Myfile is an external file (such as txt), try this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;filename myexfile "C:\Users\Administrator\Desktop\test.txt"; 
%macro check_file_exist;                                                                                                                            
  %if %sysfunc(fileexist(myexfile))=0 %then %do;
	data _null_;
		file myexfile;
	run;
  %end;
%Mend check_file_exist;                                                                                                                             
                                                                                                                                        
%check_file_exist;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Fred&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 05:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916914#M361172</guid>
      <dc:creator>FM_MF</dc:creator>
      <dc:date>2024-02-20T05:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro check if dataset is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916915#M361173</link>
      <description>&lt;P&gt;The question is: what do you do with a completely empty (no variables, no observations) dataset later on?&lt;/P&gt;
&lt;P&gt;It's much better to handle it there:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set
  x
  y
%if %sysfunc(exist(myfile)) %then %do;
  myfile
%end;
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can also set the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lesysoptsref/p1o6bsb5wep7ahn1f8mlw3jf2drq.htm" target="_blank" rel="noopener"&gt;NODSNFERR&lt;/A&gt; system option to prevent an ERROR for a missing dataset.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 06:34:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916915#M361173</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-02-20T06:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro check if dataset is available</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916916#M361174</link>
      <description>&lt;P&gt;That's not work. The use of&amp;nbsp;fileexist() is wrong. (&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068/show-comments/false" target="_self"&gt;#Maxim1&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;According to the documentation for&amp;nbsp;fileexist() not a&amp;nbsp;&lt;U&gt;fileref&lt;/U&gt; but fully a fully qualified path to the file is required (&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n06xm8hwk0t0axn10gj16lfiri43.htm)" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n06xm8hwk0t0axn10gj16lfiri43.htm)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To use fileref the solution is FEXIST() function:&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gh473azqo3han1edlhbyt04gxa.htm" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gh473azqo3han1edlhbyt04gxa.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look at this series of examples and it's log to see the issue. In the log the "R:\_TD6720_YABWONL5P_" is the WORK directory location for the running sesion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename myfileE "R:\test_Exists.txt"; 
filename myfileN "R:\test_NOT_exists.txt"; 

data _null_; /* create the file */
  file myfileE;
  put "42";
run;


data _null_;
  rc = fexist('myfileE');
  rctxt=sysmsg();
  put rc= / rctxt=;
run;

data _null_;
  rc = fileexist('myfileE');
  rctxt=sysmsg();
  put rc= / rctxt=;
run;

data _null_;
  rc = fexist('myfileN');
  rctxt=sysmsg();
  put rc= / rctxt=;
run;

data _null_;
  rc = fileexist('myfileN');
  rctxt=sysmsg();
  put rc= / rctxt=;
run;




data _null_;
  rc = fileexist("R:\test_Exists.txt");
  rctxt=sysmsg();
  put rc= / rctxt=;
run;

data _null_;
  rc = fileexist("R:\test_NOT_exists.txt");
  rctxt=sysmsg();
  put rc= / rctxt=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt;1    filename myfileE "R:\test_Exists.txt";
2    filename myfileN "R:\test_NOT_exists.txt";
3
4    data _null_; /* create the file */
5      file myfileE;
6      put "42";
7    run;

NOTE: The file MYFILEE is:
      Filename=R:\test_Exists.txt,
      RECFM=V,LRECL=32767,File Size (bytes)=0,
      Last Modified=20Feb2024:08:08:23,
      Create Time=20Feb2024:08:08:23

NOTE: 1 record was written to the file MYFILEE.
      The minimum record length was 2.
      The maximum record length was 2.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


8
9
10   data _null_;
11     rc = fexist('myfileE');
12     rctxt=sysmsg();
13     put rc= / rctxt=;
14   run;

rc=1
rctxt=
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


15
16   data _null_;
17     rc = fileexist('myfileE');
18     rctxt=sysmsg();
19     put rc= / rctxt=;
20   run;

rc=0
rctxt=WARNING: Physical file does not exist, R:\_TD6720_YABWONL5P_\myfileE.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


21
22   data _null_;
23     rc = fexist('myfileN');
24     rctxt=sysmsg();
25     put rc= / rctxt=;
26   run;

rc=0
rctxt=WARNING: Physical file does not exist, R:\test_NOT_exists.txt.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


27
28   data _null_;
29     rc = fileexist('myfileN');
30     rctxt=sysmsg();
31     put rc= / rctxt=;
32   run;

rc=0
rctxt=WARNING: Physical file does not exist, R:\_TD6720_YABWONL5P_\myfileN.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


33
34
35
36
37   data _null_;
38     rc = fileexist("R:\test_Exists.txt");
39     rctxt=sysmsg();
40     put rc= / rctxt=;
41   run;

rc=1
rctxt=
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


42
43   data _null_;
44     rc = fileexist("R:\test_NOT_exists.txt");
45     rctxt=sysmsg();
46     put rc= / rctxt=;
47   run;

rc=0
rctxt=WARNING: Physical file does not exist, R:\test_NOT_exists.txt.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2024 07:16:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-check-if-dataset-is-available/m-p/916916#M361174</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-02-20T07:16:34Z</dc:date>
    </item>
  </channel>
</rss>

