04-12-2016 11:29 PM
In order to delete an external file I can use the fdelete funciton in the following way:
filename delfile 'myfile.test';
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:
rc = fdelete(path);
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".
04-12-2016 11:55 PM
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;
04-15-2016 10:08 PM - edited 04-15-2016 10:09 PM
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.
04-18-2016 03:20 AM - edited 04-18-2016 03:25 AM
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.
04-19-2016 10:06 PM
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.
04-21-2016 12:31 PM
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.
04-13-2016 12:01 AM
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.
04-13-2016 01:37 AM
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;
04-15-2016 10:11 PM
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.
04-13-2016 04:22 AM
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.