Use the FILEEXIST function to determine whether the file exists. A nonzero value is
returned if the file exists. If the file exists, execute the code within the %DO block.
%if %sysfunc(fileexist(&file)) ge 1 %then %do;
The &SYSSCP macro variable returns an abbreviated name of the operating system that
the code is running on. If the code is running on Windows, the %IF condition will be
true and the %SYSEXEC statement along with the DEL command will delete the file
that is passed to the macro. The NOXWAIT option specifies whether you have to enter
EXIT at the DOS prompt before the DOS shell closes. The NOXSYNC option controls
whether an X command or statement executes synchronously or asynchronously. If the
code is running on UNIX, the %ELSE will execute and the RM command will run on
UNIX and remove the file that is passed to the macro.
%if &sysscp=WIN %then %do;
options noxwait noxsync;
%sysexec del &file;
%end;
%else %do;
%sysexec rm &file;
%end;
end;
mend check;
Invoke the macro by passing in the full pathname of the file to be deleted.
%check(c: est.txt)
Example 16: Retrieve the File Size, Create Time,
and Last Modified Date of an External File
Details
This example uses the FOPEN and FINFO functions to retrieve certain attributes from
an external file.
Program
%macro FileAttribs(filename);
%local rc fid fidc Bytes CreateDT ModifyDT;
%let rc=%sysfunc(filename(onefile,&filename));
%let fid=%sysfunc(fopen(&onefile));
%if &fid ne 0 %then %do;
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let CreateDT=%sysfunc(finfo(&fid,Create Time));
%let ModifyDT=%sysfunc(finfo(&fid,Last Modified));
%let fidc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(onefile));
%put NOTE: File size of &filename is &bytes bytes;
%put NOTE- Created &createdt;
%put NOTE- Last modified &modifydt;
Example 16: Retrieve the File Size, Create Time, and Last Modified Date of an External
File 475