Published on :

External File Deletion Macro

This code is also available in: Français Deutsch Español
Attention : This code requires administrator privileges.
This macro, named `_delFile`, is designed to delete an external file whose full path is provided as a parameter (`i_file`). It uses SAS© system functions `filename` to associate a temporary fileref with the file, then `fdelete` to perform the deletion. The operation's return code is returned by the macro. Finally, the temporary fileref is released.
Data Analysis

Type : EXTERNE


The script interacts with an external file specified by a parameter (`i_file`), performing a deletion operation directly on the OS file system. It neither consumes nor produces traditional SAS datasets.

1 Code Block
MACRO
Explanation :
This block defines the `_delFile` macro which takes `i_file` (the path of the file to delete) as a parameter.
1. `%LOCAL rc filrf;`: Declares macro variables `rc` (return code) and `filrf` (temporary fileref) as local to prevent conflicts.
2. `%LET filrf=_tmpf;`: Assigns the temporary fileref name `_tmpf` to the `filrf` variable.
3. `%LET rc=%sysfunc(filename(filrf,&i_file));`: Associates the fileref `_tmpf` with the file path provided by `&i_file`. The return code of this operation is stored in `rc`.
4. `%LET rc=%sysfunc(fdelete(_tmpf));`: Deletes the file associated with the fileref `_tmpf`. The deletion's return code is updated in `rc`.
5. `&rc`: This line outputs the value of `rc`, which represents the status of the deletion operation.
6. `%LET rc=%sysfunc(filename(filrf));`: Releases the association of the `_tmpf` fileref, a good practice for resource cleanup.
Copied!
1%MACRO _delFile(i_file);
2 
3 %LOCAL rc filrf;
4 %LET filrf=_tmpf;
5 %LET rc=%sysfunc(filename(filrf,&i_file));
6 %LET rc=%sysfunc(fdelete(_tmpf));
7 &rc
8 %LET rc=%sysfunc(filename(filrf));
9%MEND _delFile;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de. This file is part of SASUnit, the unit testing framework for SAS(R) programs. For copyright information and terms of use under the GNU Lesser General Public License, see the included README.md file or https://github.com/HMS-Analytical-Software/SASUnit/wiki/readme/.


Banner
Expert Advice
Expert
Simon
Expert SAS et fondateur.
« The _delFile macro demonstrates a clean, platform-independent approach to file management. By utilizing the FILENAME and FDELETE functions via the macro processor, you avoid the risks associated with OS-specific shell commands (like rm or del), ensuring your code remains portable across Windows and Unix environments.

Strategic Insights & Best Practices
Leveraging Return Codes: The strength of this macro is that it outputs the value of &rc. In SAS, a return code of 0 indicates success, while a non-zero value points to issues like insufficient permissions or a locked file. For professional-grade workflows, always check this result (e.g., %if %_delFile(...) = 0 %then...) before proceeding with dependent steps.

Fileref Cleanup: Using a temporary fileref (_tmpf) is good practice, but the final call to filename(filrf)—which effectively runs filename(_tmpf, )—is the most critical step. It unassigns the reference, preventing "fileref leakage" where logical names accumulate and potentially hit system limits during long-running sessions.

Handling Active Locks: Keep in mind that fdelete will fail if the file is currently opened by another SAS process or an external application (like Excel). If your process involves overwriting logs or temporary data, ensure all previous handles to the file are closed before calling this macro. »