BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aknight1
Obsidian | Level 7

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)

 

 

 

 

    

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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.

aknight1
Obsidian | Level 7

Hi Mr Bremser,

 

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

 

Thanks for your quick reply.

 

Regards,

Antony

Kurt_Bremser
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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