BookmarkSubscribeRSS Feed
SASguyCO
Calcite | Level 5

Anyone have any code that can be used to delete a file that does not involve using proc delete?

thanks!

14 REPLIES 14
art297
Opal | Level 21

What have you got against proc delete?  Do you also want to avoid proc datasets?  Based on which operating system you're on would one of the system commands (e.g., x), together with your operating system delete command, be more preferable?  Or were you looking for something else altogether?

Here is an alternative that Tom posted on SAS-L a couple of years ago:

%macro deletedsn(dsn);

%if %sysfunc(exist(&dsn,data)) %then %do;

proc sql;

   drop table &dsn;

quit;

%end;

%if %sysfunc(exist(&dsn,view)) %then %do;

proc sql;

   drop view &dsn;

quit;

%end;

%mend deletedsn;

FriedEgg
SAS Employee

proc datasets library=word; /* you could also use the kill option to delete all datasets from a library */

  delete blah;

run;

FriedEgg
SAS Employee

Side note... Guessing from you name, are you in Colorado?  I am in Boulder.

SASguyCO
Calcite | Level 5

Hey Art!, thanks for responding, I guess I should have been more specific...

I love Proc delete!, but it won't work on a "locked" data set Smiley Sad

Problem is each night there is some sort of back up running and the data set becomes locked and I need to overwrite it (every night) and the SAS process fails to overwrite as it says the file is locked.  I can however, go in and manually delete the data set in windows explorer and then recreate it, but I want to be able to either overwrite the existing one, or delete the old one first (from a locked state) and recreate it.

FriedEgg
SAS Employee

x rm -f /location/of/file; /* exact sytax would differ by operating system */

SASguyCO
Calcite | Level 5

Tried that FriedEgg, and got the following:

ERROR: Shell escape is not valid in this SAS session.

FriedEgg
SAS Employee

Check your options: xcmd, xsync, xwait

SASguyCO
Calcite | Level 5

You'll have to forgive my ignorance on this, but what do you mean by check your options: xcmd, xsync, xwait?  How do I do that?

I should state I'm doing all this in SAS EG but within actual code. 

FriedEgg
SAS Employee

Good information Art.  I cannot claim to be very familiar with EG administation (even though I do it sometimes, yikes!)

FriedEgg
SAS Employee

data _null_;

array opt[3] $5 ('xcmd' 'xsync' 'xwait');

do i=1 to 3;

call execute('proc options option=' || strip(opt) || '; run;');

end;

run;

xcmd is the most important.  If it is not defined you, or your sas administator will have to enable it in the config file for the server you are connecting to.  It is probably not enabled for a reason, which means you are out of luck... Smiley Sad

SASguyCO
Calcite | Level 5

Thanks again Art and FriedEgg.  Looks like it's not defined.  I looked at the link Art and it's certainly helpful.  Option 2 won't fly for sure, and option 1 requires me to gain admin rights which they will want to know what for so I'm not sure they'll let me go about it that way either.  Appreciate the help!

Peter_C
Rhodochrosite | Level 12

I don't know how you can unlock a file that is opened in some other task.

What you might be able to do is code a loop that exits once you can lock the file for your session.

The LOCK statement is documented at http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a001517609.htm

OS2Rules
Obsidian | Level 7

Throwing in my 2 cents worth....

It looks like you need to delete the entire dataset rather than a single SAS table.  My first course of action would be to identify who has the lock on the file, otherwise we have had success just running a DOS command to delete the file at the appropriate time. 

Here is a sample where we clean out a dataset of all html files before rewriting them.  This could be easily change to delete the entire file.  Just run the macro with the name of the file:

%macro deletefile(filen);

  %let delcmd1 = DEL "&prefix.\*.html ";

  %put deleting &filen data;

  data _null_;

  X &delcmd1;

  run;

%mend;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 14 replies
  • 3079 views
  • 0 likes
  • 5 in conversation