You are using FILEEXISTS() to test if the physical file exists and then using the FDELETE() function to delete whatever physical file the filref PDF_REF is pointing at. There are two problems with that:
that fileref might not have been defined yet
that fileref might be pointing at a completely different file.
It might make more sense to run the FILENAME() function first. You can then use the FILEREF() function to check if (A) the filename function worked and (B) the file it points to exists or not. Then you can decide if the existing file needs to be deleted.
Example of using FILEREF().
1 * Macro to check a fileref and report results;
2 %macro file_chk(fileref);
3 %put NOTE: &=fileref %sysfunc(choosec(2+
%sysfunc(sign(%sysfunc(fileref(&fileref))))
4 ,is Defined but does NOT Exist
5 ,is Defined and Exists
6 ,is NOT Defined)).;
7 %mend file_chk;
8
9 * Make sure PDF_REF fileref is not defined ;
10 %let fileref=pdf_ref;
11 %put rc=%sysfunc(filename(fileref));
rc=0
12
13 %let path=%sysfunc(pathname(work));
14 %let fileref=pdf_ref;
15 * Check FILEREF ;
16 %file_chk(&fileref)
NOTE: FILEREF=pdf_ref is NOT Defined.
17 * Use FILENAME() to create FILEREF ;
18 %let rc=%sysfunc(filename(fileref,"&path/test.pdf"));
19 %file_chk(&fileref)
NOTE: FILEREF=pdf_ref is Defined but does NOT Exist.
20
21 * Write something to the file ;
22 data _null_;
23 file &fileref;
24 put 'hello';
25 run;
NOTE: The file PDF_REF is:
(system-specific pathname),
(system-specific file attributes)
NOTE: 1 record was written to the file (system-specific pathname).
The minimum record length was 5.
The maximum record length was 5.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
26 %file_chk(&fileref)
NOTE: FILEREF=pdf_ref is Defined and Exists.
27
28 * Remove the file ;
29 %let rc=%sysfunc(fdelete(&fileref));
30 %file_chk(&fileref)
NOTE: FILEREF=pdf_ref is Defined but does NOT Exist.
31
... View more