BookmarkSubscribeRSS Feed
hhchenfx
Rhodochrosite | Level 12

Hi,

I want to check if dataset Myfile is already in work library or not. If it is not there yet, create that file.

The idea can be seen in the code below.

Thank you,

HHC

%macro check_file_exist;
	%if FILE IS NOT THERE %then %do;
		data Myfile; set _NULL_;
		run;
	%end;
%Mend;

 

 

3 REPLIES 3
FM_MF
Calcite | Level 5

You can try this (If Myfile's file extension is sas7bdat):

%Macro check_file_exist;
	%if %sysfunc(exist(work.Myfile))=0 %then %do;
		data Myfile; set _NULL_;
		run;
	%end;
%Mend;

 

If Myfile is an external file (such as txt), try this:

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;

Hope this helps.

 

Thanks,

Fred

yabwon
Onyx | Level 15

That's not work. The use of fileexist() is wrong. (#Maxim1)

According to the documentation for fileexist() not a fileref but fully a fully qualified path to the file is required (https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n06xm8hwk0t0axn10gj16lfiri43.h...

 

To use fileref the solution is FEXIST() function: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gh473azqo3han1edlhbyt04gxa.h...

 

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.

 

Code:

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;

Log:

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

 

Bart

 

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kurt_Bremser
Super User

The question is: what do you do with a completely empty (no variables, no observations) dataset later on?

It's much better to handle it there:

data want;
set
  x
  y
%if %sysfunc(exist(myfile)) %then %do;
  myfile
%end;
;
run;

You can also set the NODSNFERR system option to prevent an ERROR for a missing dataset.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 862 views
  • 0 likes
  • 4 in conversation