Begin the macro definition with one parameter.
Use the %LET statement to create a macro variable named &FILRF that will contain the
fileref name that will be used later.
Use the %IF statement with the FILEEXIST function to verify that the file that is passed
to the macro does exist.
%if %sysfunc(fileexist(&outf)) %then %do;
Use the FILENAME function to associate a fileref with the file that is passed into the
macro named &OUTF. Use the FOPEN function to open that file and assign a file
identifier value to the macro variable &FID.
Use the %IF statement to make sure the file was successfully opened. FOPEN returns a
0 if the file could not be open and a nonzero if the file was successfully open.
%if &fid > 0 %then %do;
Use the FREAD function to read a record from the file. Use the FGET function to copy
the data into a macro variable named &MYSTRING.
Use the %IF statement to make sure the FGET function was successful. The FGET
function returns a 0 if the operation was successful. If the FGET function was
successful, then use a %PUT statement to write the contents of the variable named
&MYSTRING to the log.
%if &rc = 0 %then %put &mystring;
If the %IF condition is false, then use a %PUT statement to write a message to the log
stating that the file is empty.
%else %put file is empty;
Use the FCLOSE function to close the file.
End the inner %IF %THEN %DO block.
Use the FILENAME function to disassociate the fileref from the file.
End the outer %IF %THEN %DO block.
If the %IF statement that uses the FILEEXIST function is false, then use the %PUT
statement to write that the file does not exist to the log.
%else %put file does not exist;
Example 9: Use Macro Logic to Determine If an External File Is Empty 465