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
%end;
%else %put &filename could not be open.;
%mend FileAttribs;
%FileAttribs(c:aaa.txt)
Program Description
Begin the macro definition within one parameter.
%macro FileAttribs(filename);
Use the %LOCAL statement to make sure all macro variables created are local to the
macro FILEATTRIBS.
%local rc fid fidc Bytes CreateDT ModifyDT;
Use the FILENAME function to associate the fileref of ONEFILE with the file that is
passed to the macro (&FILENAME). The macro variable RC will contain a 0 if the
operation was successful; not 0 if unsuccessful.
%let rc=%sysfunc(filename(onefile,&filename));
Use the FOPEN function to open the file that was set up in the FILENAME function
above. The macro variable FID contains a file identifier value that is used to identify the
open file to other functions.
%let fid=%sysfunc(fopen(&onefile));
Use the %IF condition to check that the file opened successfully.
%if &fid ne 0 %then %do;
Use the FINFO function to retrieve the size, creation time, and last modified date. The
first argument to this function is the macro variable created from the FOPEN function
that holds an identifier to the file. Three macro variables are created: &BYTES,
&CREATEDT, and &MODIFYDT.
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let CreateDT=%sysfunc(finfo(&fid,Create Time));
%let ModifyDT=%sysfunc(finfo(&fid,Last Modified));
Use the FCLOSE function to close the file.
%let fidc=%sysfunc(fclose(&fid));
Use the FILENAME function without a second argument to deassign the fileref of
‘onefile’.
%let rc=%sysfunc(filename(onefile));
Use the %PUT statement to write the macro variable values to the log.
%put NOTE: File size of &filename is &bytes bytes;
%put NOTE- Created &createdt;
%put NOTE- Last modified &modifydt;
476 Appendix 5 SAS Macro Examples
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset