BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I work with many rtf documents, and I want to keep only 1 file.
How can I do to delete rtf files using SAS (Version 8.2)?

Many thanks for your help!

Violaine
4 REPLIES 4
deleted_user
Not applicable
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.

Data _NULL_;
InFile SYSREQST;
Input;
Put _InFile_;
Run;

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.

Kind regards

David
deleted_user
Not applicable
I have just tried the following code you give me:
FileName SYSREQST Pipe "del '&Path_Stat\MyDoc.rtf'";
Data _NULL_;
InFile SYSREQST;
Input;
Put _InFile_;
Run;

The log reports the following error message:
Stderr output:
The system cannot find the path specified.


Do you understand what is the problem?

Many thanks,

Violaine
deleted_user
Not applicable
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;
32
33 Filename SYSREQST Pipe "Del ""&MyPath.\deletecd.reg""";
34
35 Data _NULL_;
36 InFile SYSREQST;
37 Input;
38 Put _InFile_;
39 Run;

NOTE: The infile SYSREQST is:
Unnamed Pipe Access Device,
PROCESS=Del "C:\Documents and settings\david\deletecd.reg",
RECFM=V,LRECL=256

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.

Kind regards

David
deleted_user
Not applicable
It works very well!
Many thanks for your help!

Violaine

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1373 views
  • 0 likes
  • 1 in conversation