BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
liveorexist
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Tom
Super User Tom
Super User

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;

 

liveorexist
Calcite | Level 5

Thank you for your answer.

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

Thanks everybody! 😉

Ron_MacroMaven
Lapis Lazuli | Level 10

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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