DATA Step, Macro, Functions and more

Curious about FDELETE issue

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Curious about FDELETE issue

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)

 

 

 

 

    


Accepted Solutions
Solution
‎11-21-2017 06:49 AM
Super User
Posts: 9,611

Re: Curious about FDELETE issue

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎11-21-2017 06:49 AM
Super User
Posts: 9,611

Re: Curious about FDELETE issue

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 8

Re: Curious about FDELETE issue

Posted in reply to KurtBremser

Hi Mr Bremser,

 

Aha!  So yes, I miss something obvious here!   Smiley Embarassed

 

Thanks for your quick reply.

 

Regards,

Antony

Super User
Posts: 9,611

Re: Curious about FDELETE issue

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().

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 144 views
  • 0 likes
  • 2 in conversation