Sample 24577: Check for the existence of a file
The sample code on the Full Code tab illustrates how to check for the existence of a file before trying to use it in a SAS program. If the file does not exist, write a message to the SAS Log. If it does exist, read it into a SAS data set.
Note:
Run the program 'as is' first to see the note written to the SAS log. Then copy the data from the DOWNLOADS tab to a text file on your machine. Edit the INFILE statement to point to the file and resubmit to see the results generated when the file does exist.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create a macro variable that contains the name of the file to be read into */
/* a SAS data set. */
%let testfile = 'c:\temp\test.txt';
/* The DATA step will need to be wrapped in a macro. The FILEEXIST function */
/* is used to test for the existence of the file by it's fully qualified */
/* name. If the flat file does exist then the DATA step is executed. */
/* Otherwise a message is written to the SAS Log to notify the user that the */
/* file does not exist. */
%macro findit;
%if %sysfunc(fileexist(&testfile)) %then %do;
data one;
infile &testfile;
input a b c;
run;
%end;
%else %put The file &testfile does not exist. ;
%mend;
/* invoke the macro */
%findit;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
OUTPUT written to log when file does not exist
The file 'c:\temp\test.txt' does not exist.
RESULTS when file does exist
Obs a b c
1 1 2 3
2 11 22 33
3 111 222 333
Check for the existence of a file before trying to use it in a SAS program. If the file does not exist, write a message to the SAS log. If it does exist, read it into a SAS data set.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step Common Programming Tasks ==> Utilities
|
Date Modified: | 2005-12-08 11:34:06 |
Date Created: | 2004-09-30 14:08:55 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |