Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SAS to delete a text file

Tags:

sas

sas-macro

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

like image 498
Allan Bowe Avatar asked Nov 15 '12 14:11

Allan Bowe


People also ask

How do you delete all files in a folder using SAS?

To delete all files in a SAS library at one time, use the KILL option in the PROC DATASETS statement. CAUTION: The KILL option deletes all members of the library immediately after the statement is submitted.

How do I delete files from gnome?

Select the item you want to delete. Press and hold the Shift key, then press the Delete key on your keyboard. Because you cannot undo this, you will be asked to confirm that you want to delete the file or folder.

How do I delete a file in Apache?

Deleting a fileRight-click on the file name to display a context menu. Click Delete, and you will get a confirmation dialog.

How do I delete files from Linux drive?

Type "rm (filename)" in the Terminal to remove a file on Linux. To remove an entire folder (or directory) and all of its contents, type "rm -r (foldername)" into the Terminal instead. The rm and rmdir commands delete files and directories on Linux, macOS, and other Unix-like operating systems.


2 Answers

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;
like image 84
Joe Avatar answered Oct 19 '22 18:10

Joe


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!

like image 2
Allan Bowe Avatar answered Oct 19 '22 18:10

Allan Bowe