There are multiple ways in which this can be done, my favourite being the use of the Windows API. However, it has some fairly precise requirements. If you want to explore this option then my paper 248-30 from SUGI 30 explores the DeleteFile API and will give you a starting point I can flesh out for you. The paper can be found on this web site.
More commonly, users will write a system call to delete a file. The function Call System and the X command are two common ways to send a command to the operating system. The difficulty with this is forming the command correctly, especially if there are spaces in the path to the file name. Something of this form should work for you.
X "del 'c:\deleteme.pdf'";
A variation on this method is to use the unnamed pipe engine of the Filename statement. A statement like this is created first:
FileName SYSREQST Pipe "del 'c:\deleteme.pdf'";
Then the command is executed by calling the file reference into a data step and parsing the returns from the call.
With this approach, any return messages from the command will be written to the SAS log. The delete command by default does not produce any confirmation, but a malformed command or missing file should give you a log message.
I am assuming you want to create other files, not delete previous versions of the same file. By default the ODS RTF engine will replace previous versions without the need to delete the old version first.
Yes, I do know what is wrong. Some versions of Windows want quoted paths to use double quotes, not single quotes. I suspect that is the issue here.
However, you are using a macro symbol to identify the path to the file and you need to resolve macro symbols within double quotes, so we need to replace the single quotes.
Here is where a visit to the early part of the SAS Language manual would pay off. You would read about doubling and tripling quote marks to build strings and then find a way to have double quotes around the SAS filename path, and also have double quotes within the filename path.
The following syntax worked for me. Note the doubling of the quote marks before and after the path replacing the single quote marks.
31 %Let MyPath = C:\Documents and settings\david;
33 Filename SYSREQST Pipe "Del ""&MyPath.\deletecd.reg""";
35 Data _NULL_;
36 InFile SYSREQST;
38 Put _InFile_;
NOTE: The infile SYSREQST is:
Unnamed Pipe Access Device,
PROCESS=Del "C:\Documents and settings\david\deletecd.reg",
NOTE: 0 records were read from the infile SYSREQST.
NOTE: DATA statement used (Total process time):
real time 0.18 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
Be aware that the SAS Log Note that reports the "PROCESS" is extremely important. When you resolve a macro symbol into a command string, you need to review that string very carefully to ensure it has resolved as you expect. So if it doesn't work correctly again, check the process value first to ensure it has the correct path to the file you want to process. This is the most common problem found with building command strings from macro values.