Using SAS to delete a text file

10,304

Solution 1

From SAS documentation:

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

It's essentially OS independent, in that it will work on multiple OS's. You can't ever have true independence since the fileref would be OS-dependent, but if you specify that as an argument it shouldn't be a problem.

As a macro, you would use FDELETE in a SYSFUNC block:

%put %sysfunc(fdelete(myfile));

However, myfile needs to be a fileref, so if you were using only the file's actual physical location as an argument you'd need two steps:

%macro file_Delete(file);
filename __a "&file.";
%put %sysfunc(fdelete(__a));
%mend file_delete;

Solution 2

I took this one step further as follows:

%macro fdel(file);
  %let rc= %sysfunc(filename(fref,&file));
  %let rc= %sysfunc(fdelete(&fref));
%mend;

This makes it a bit more versatile. Thanks Joe!

Share:
10,304
Allan Bowe
Author by

Allan Bowe

SAS Consultant Projects: https://sasjs.io - Framework for building web apps on SAS https://sasensei.com - quiz game for SAS https://datacontroller.io - data capture, governance & approval https://rawsas.com - SAS blog

Updated on June 07, 2022

Comments

  • Allan Bowe
    Allan Bowe almost 2 years

    Am looking for a piece of code, preferably OS independent and macro based, for deleting a text file (or any file for that matter)