BookmarkSubscribeRSS Feed
ilikesas
Barite | Level 11

Hi,

 

In order to delete an external file I can use the fdelete funciton in the following way:

filename delfile 'myfile.test';

  data _null_;

  rc=fdelete('delfile');

  run;

 

But what I would like to do is to delete files dynamically. I have a data table witht the names and the paths of the files that I want to delete. I tried using the following code:

 

data _null_;

set files;

rc = fdelete(path);

run;

 

But couldn't get the files deleted. I didn't even have an error message, just a blue message saying that "the file ref exceeds 8 characters and will be truncated".

 

Thank you!

10 REPLIES 10
Reeza
Super User

 

Don't use a data _null_ step, save the data set. The RC variable indicates if the delete was successful, check the docs for the return codes that are valid. 

 

What about something like the following:

Courtesy of Joe via http://stackoverflow.com/questions/13399760/using-sas-to-delete-a-text-file

 

data test;
    fname="tempfile";
    rc=filename(fname,"physical-filename");
    if rc = 0 and fexist(fname) then
       rc=fdelete(fname);
    rc=filename(fname);
run;

 

 

ilikesas
Barite | Level 11

Hi Reeza,

 

The other question which you are refering to (and which you answered) is the one about creating a folder based on the file names and transfering the files into that folder. But the original files remain, so now I have 2 files: the one which is transfered into the folder, and the original one. So what I would like to achieve is to delete the original files.

 

I have a column for the file name and for the file path, and when I run the following code:

data test;
set filelist;
    
    rc=filename(the_name,the_path);
    if rc = 20014 and fexist(the_name) then
       rc=fdelete(the_name);
    rc=filename(the_name);
run;

I get a message that the fileref in fexist exceeds 8 characters and will be truncated, and as a result the original files are not deleted.

I also included quotes for the names and paths (for exampel, a file name in the column the_name is of the form "file1.txt" instead of file1.txt ) but the origina files are still not deleted.

 

Also, I did rc = 20014 becasue that's the value that I get, and when I do rc = 0 I don't even get the message that the fileref will be truncated etc. and the original files are still there.

 

Thanks!

 

 

Kurt_Bremser
Super User

You need to set the_name to a string that is a valid SAS name for a file reference (up to 8 characters, can only contain a-z, underline, and numbers, must start with underline or alphanumeric character). Easiest way to restrict the length is to do

length the_name $8;

before the set statement, if the_name in dataset filelist is defined too long.

This is what the message is complaining about.

And checking for rc = 20014 is bogus because any rc other that zero from the filename function means that this function failed and the file reference for the_name does not exist; therefore no further operations on this nonexistent file reference are possible.

ilikesas
Barite | Level 11

Hi Kurt,

I did include the length setting as you showed but still no result...

 

I tried to delete manually a file with the following code:

data test;
   the_name2="f1";
    rc=filename(the_name2,'C:\files\f1.txt');
    if rc = 0 and fexist(the_name2) then
       rc=fdelete(the_name2);
    rc=filename(the_name2);
run;

and it actually worked and the file was deleted!

So for some reason the code doesn't want to work when the fileref and physical-filename are not manually written but are referenced by the column variable where they reside.

 

Thanks!

Kurt_Bremser
Super User

If you don'the have a variable the_name2 in your dataset, then the filename function tries to use an empty string as a fileref, and that will fail.

Reeza
Super User

Also, what kind of files are you deleting?

I think you asked a very similar question recently or a similar one was on here, you can use call system, %sysexec or an X command to delete the files using OS commands. 

Kurt_Bremser
Super User

The fdelete function needs a fileref (result of a filename statement) as argument, not a physical filename. You need to use the filename function first.

From the examples for fdelete:

data _null_;
set files;
fname = 'tempref';
rc=filename(fname,path);
if rc = 0 and fexist(fname) then
  rc=fdelete(fname);
rc=filename(fname);
run;
ilikesas
Barite | Level 11

Hi Kurt,

 

your code is basically the same as Reeza's, and I tried it and didn't get the files deleted. Please read my reply to Reeza if you like as it contains more description.

 

Thanks!!!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why would you need to remove files from SAS in the first place?  SAS output will overwrite anything that is already there, and because you will be using a version control system, you can roll back/commit whatever you need.  I have yet to encounter a situation where creating anything on the operating system is necessary for normal day to day work, and those instances where it is necessary this is covered by other procedures - i.e. standard directory strcutures, version control software etc.

If you do attempt to do it this way, ensure you have a back as there WILL be a time when you program wipes out whole loads of data unexpectedly.

ilikesas
Barite | Level 11

Hi RW9,

 

The files are actually external files. Please read my reply to Reeza if you like because it contains more description.

 

Thanks!!!

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
  • 10 replies
  • 8555 views
  • 1 like
  • 4 in conversation