Thursday, August 21, 2008

How to verify the existence of the external file:

Verify the existence of an external file

Conditionally execute code to read in a file only when the file exists.
Note: Although your operating environment utilities may recognize partial physical filenames, you must always use fully qualified physical filenames with FILEEXIST.
This example verifies the existence of an external file. If the file exists, read in the file using INFILE and INPUT statements. If the file does not exist, display a message in the SAS log that states the file does not exist.
Note that in a macro statement you do not enclose character strings in quotation marks.

/* If the file passed to the macro does exist, read in the file and create */
/* a character variable called VAR with a default length of 8 bytes. If */
/* file named in the macro call does not exist, write "FILE DOES NOT EXIST..." */
/* to the log. */

%macro in_file(file);
%if %sysfunc(fileexist(&file))=1 %then %do;
data a;
infile "&file";
input var $;
%else %do;

data _null_;
put "FILE DOES NOT EXIST: &file";

/* Call the macro with file that does not exist */
/* Call the macro with file that does exist */


LOG when file does not exist
FILE DOES NOT EXIST: c:\bloom.txt

LOG when the file does exist ...typical notes seen when a file is read

NOTE: The infile "c:\tracks\x1.txt" is:
File Name=c:\tracks\x1.txt,

NOTE: 2 records were read from the infile "c:\tracks\x1.txt".
The minimum record length was 14.
The maximum record length was 20.
NOTE: The data set WORK.A has 2 observations and 1 variables.


Post a Comment