Sample 24795: Conditionally read in an external file based on the file's existence
The sample code on the Full Code tab shows how to conditionally execute code that reads in a file only when the file exists.
Note:
Although your operating environment utilities might
recognize partial physical filenames, you must always
use fully-qualified physical filenames with the FILEEXIST function.
This example verifies the existence of an external file.
If the file exists, the file is read in 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.
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.
/* 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)) %then %do;
data a;
infile "&file";
input var $;
run;
%end;
%else %do;
%put FILE DOES NOT EXIST: &file;
%end;
%mend;
/* Call the macro with file that does not exist */
%in_file(c:\bloom.txt);
/* Call the macro with file that does exist */
%in_file(c:\tracks\x1.txt);
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.
SAS Log messages when the file does not exist
FILE DOES NOT EXIST: c:\bloom.txt
SAS log messages 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,
RECFM=V,LRECL=256
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.
Conditionally execute code to read in a file only when the file exists.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> External Files
|
| Date Modified: | 2005-12-16 03:02:59 |
| Date Created: | 2004-09-30 14:09:15 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |