Hello,
I am curious about an issue which I am getting when using the FDELETE function via %SYSFUNC. I'd be interested in any explanation!
I inherited a macro for deleting files, which I tried to tidy up. The current state of the macro code is copied below. I am testing with the small program also copied below. The issue is with the line which is marked in RED. When this line is included in the macro code, everything works fine and I get the Log as shown in green. When this line is commented out, the macro does not work and I get the Log as shown in red.
Can anybody tell me why I need a macro variable with the name of "delfile" (the fileref), which is also populated with "delfile" ? I cannot see that this macro variable is actually used, but it seems that it needs to exist. Maybe I miss something obvious here?
(note: from the copy-and-pasted logs I've removed the first chunk of the printed filenames - they contain User Ids)
I am running this code in SAS EG 7.1 on UNIX platform.
Thanks & Regards,
Antony
Macro Code:
%macro mrmfile (filelst);
%local file i rc numfiles delfile;
%let numfiles=%sysfunc(COUNTW(&filelst.,%str(|)));
%if numfiles=0 %then %goto out;
%* Scan the specified file list and delete all existing files. ;
%do i = 1 %to &numfiles.;
%let file = %scan(&filelst.,&i.,%str(|));
%if %sysfunc(fileexist(&file.)) %then %do;
%* this macro var needs to be populated with the given fileref - not sure why!;
%*let delfile = delfile;
%* Assign a filename to the file to delete. ;
%let rc = %sysfunc(filename(delfile,&file.));
%* Delete the file. ;
%let rc = %sysfunc(fdelete(delfile));
%if &rc.=0 %then %put >>> INFO: &SYSMACRONAME. >> &file. deleted (rc=&rc.);
%else %put >>> ERROR: &SYSMACRONAME. >> Could not delete &file. (rc=&rc.);
%* Deassign the filename. ;
%let rc = %sysfunc(filename(delfile));
%end;
%else %put >>> INFO: &SYSMACRONAME. >> File &file. does not exist. ;
%end;
%out:
%mend mrmfile;
Test Program:
%let wpath=%sysfunc(pathname(work));
data work.test1;
a='test1';
run;
data _null_;
file "&wpath.\test2.txt";
put 'test2';
run;
%mrmfile(&wpath.\test1.sas7bdat | &wpath.\test2.txt);
Log when line is included:
37
38 %mrmfile(&wpath.\test1.sas7bdat | &wpath.\test2.txt);
>>> INFO: MRMFILE >> \SEG12956\SAS Temporary Files\_TD9088_DEFRALT004871_\Prc2\test1.sas7bdat deleted (rc=0)
>>> INFO: MRMFILE >> \SEG12956\SAS Temporary Files\_TD9088_DEFRALT004871_\Prc2\test2.txt deleted (rc=0)
Log when line is commented:
37
38 %mrmfile(&wpath.\test1.sas7bdat | &wpath.\test2.txt);
>>> ERROR: MRMFILE >> Could not delete \SEG12956\SAS Temporary Files\_TD9088_DEFRALT004871_\Prc2\test1.sas7bdat (rc=20004)
>>> ERROR: MRMFILE >> Could not delete \SEG12956\SAS Temporary Files\_TD9088_DEFRALT004871_\Prc2\test2.txt (rc=20004)
Maxim 1: Read the documentation.
From the documentation of the FDELETE function:
In a macro (for example, in the %SYSFUNC function), fileref is the name of a macro variable (without an ampersand) whose value contains the fileref to assign to the external file.
Maxim 1: Read the documentation.
From the documentation of the FDELETE function:
In a macro (for example, in the %SYSFUNC function), fileref is the name of a macro variable (without an ampersand) whose value contains the fileref to assign to the external file.
Hi Mr Bremser,
Aha! So yes, I miss something obvious here!
Thanks for your quick reply.
Regards,
Antony
TBH, your question made me read that specific part of the doc.
Since I always use the operating system's commands via filename pipe for deleting files, I didn't know that specific "quirk" of fdelete().
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: