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!
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;
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.
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;
Thank you for your answer.
I runned the code that you've specified and all is ok.
Thanks everybody! 😉
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.