DATA Step, Macro, Functions and more

Delete if exist

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Delete if exist

Hi everybody, I need some help about the deleting of a file, if exist.

I have a daily schedulated job wich at the end of its execution creates an .ok file, so I need to insert a sas code in it that, at the start of the job, delete this .ok file.

I try to explain better: today the job creates the .ok file, tomorrow the job, at first MUST deletes the existing .ok file and the end it creates the .ok file about the current day. In this way there is always only 1 .ok file.

What sas code can I insert at the start of the job to deletes it?

I've tried 

data _null_;
if exist('file path\ file name.ok) then delete;
run;

but It doesn't work..

Is also important that, if the file doesn't exist, the job continues and execution doesn't stop.

How can I do it?

 

Thank you!


Accepted Solutions
Solution
‎09-29-2016 04:37 AM
Super User
Super User
Posts: 6,497

Re: Delete if exist

[ Edited ]

Just use the right functions.  THere is even an example in the documentation for the FDELETE() function.

 

Example 1: Deleting an External File
This example generates a fileref for an external file in the variable FNAME. Then it calls FDELETE to delete the file and calls the FILENAME function again to deassign the fileref.

 

Here is the code slightly updated to make it clearer how to apply in your situation.

 

data _null_;
    fname="OKFILE";
    rc=filename(fname,"physical-filename.ok");
    if rc = 0 then do;
        if fexist(fname) then do; 
           rc=fdelete(fname);
           if rc then put 'ERROR: Unable to delete OK file';
        end;   
        else put 'OK file does not exist';
        rc=filename(fname);
    end;
    else put 'Unable to make fileref pointing to OK file';
run;

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: Delete if exist

Why not use a dataset in the first place?  You can simply check its existence with libname and exist() function.  And you can access the data easily.  Saves messing about with an external file.

If you still want to proceed in that way then:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245893.htm

fdelete().  Though I would always recommend using caution, deleting files from your OS/network drive if not carefully done (I am thinking macros here) can seriously mess up your system, so I never recommend driving external functions like this from SAS - and in some scenarios the OS system is locked down so you can't - for a reason.

Solution
‎09-29-2016 04:37 AM
Super User
Super User
Posts: 6,497

Re: Delete if exist

[ Edited ]

Just use the right functions.  THere is even an example in the documentation for the FDELETE() function.

 

Example 1: Deleting an External File
This example generates a fileref for an external file in the variable FNAME. Then it calls FDELETE to delete the file and calls the FILENAME function again to deassign the fileref.

 

Here is the code slightly updated to make it clearer how to apply in your situation.

 

data _null_;
    fname="OKFILE";
    rc=filename(fname,"physical-filename.ok");
    if rc = 0 then do;
        if fexist(fname) then do; 
           rc=fdelete(fname);
           if rc then put 'ERROR: Unable to delete OK file';
        end;   
        else put 'OK file does not exist';
        rc=filename(fname);
    end;
    else put 'Unable to make fileref pointing to OK file';
run;

 

New Contributor
Posts: 2

Re: Delete if exist

Thank you for your answer.

I runned the code that you've specified and all is ok.

Thanks everybody! ;-)

Regular Contributor
Posts: 198

Re: Delete if exist

There are several exist functions for both SAS-managed objects

and operating system objects.

 

This page shows how to use them in macros.

 

http://www.sascommunity.org/wiki/Macro_Exist

 

Ron Fehd  mini existence ways maven

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 777 views
  • 1 like
  • 4 in conversation