Published on :
Utility INTERNAL_CREATION

Checking for File Existence on SAS Drive

This code is also available in: Deutsch Español Français
The `%mfv_existfile` macro takes a `filepath` parameter (the full path to the file on SAS© Drive, e.g., /Public/myfile.txt). It uses `%mf_getuniquefileref()` to generate a temporary fileref. The file name and path are extracted from `filepath`. An attempt is made to assign the fileref to the file using the `filename()` function with the 'filesrvc' engine. If the assignment is successful (returns 0), the `fexist()` function is used to confirm the actual existence of the file. If `filename()` fails, the macro returns 0 and ensures that the `&syscc` system macro variable is reset to 0 to avoid masking potential subsequent issues. An initial check of `&syscc` is performed via `%mf_abort` to ensure that the macro is not called in an error state.
Data Analysis

Type : INTERNAL_CREATION


The script neither uses nor creates data in the sense of traditional SAS datasets. Its purpose is to interact with the metadata of the SAS Drive file system to check for the existence of an object (file), whose path is provided as a parameter.

1 Code Block
Macro Call (%mf_abort)
Explanation :
This block calls the `%mf_abort` macro to check if the `&syscc` system macro variable (error return code) is non-zero. If so, it indicates a previous error, and the macro stops, thus avoiding executing the code in an error state.
Copied!
1%mf_abort(
2 iftrue=(&syscc ne 0),
3 msg=Cannot enter mfv_existfile.sas with syscc=&syscc
4 )
2 Code Block
Macro Variable Manipulation
Explanation :
This block declares local macro variables (`fref`, `rc`, `path`, `name`). It initializes `fref` with a unique file reference generated by `%mf_getuniquefileref()`. The `%scan` and `%substr` macro functions are used to extract the file name (`name`) and its path (`path`) from the provided `filepath` parameter.
Copied!
1%local fref rc path name;
2 %let fref=%mf_getuniquefileref();
3 %let name=%scan(&filepath,-1,/);
4 %let path=%substr(&filepath,0,%LENGTH(&filepath)-%LENGTH(&name)-1);
3 Code Block
SAS Functions (filename, fexist)
Explanation :
This conditional block attempts to assign the fileref (`fref`) to the specified file on SAS Drive using the `filename()` function with the `filesrvc` engine. If the assignment is successful (returns 0), `fexist(&fref)` is called to check for the file's existence, and its result is returned. If the assignment fails, the macro returns `0`, and the `&syscc` macro variable is explicitly reset to `0`.
Copied!
1%IF %sysfunc(filename(fref,,filesrvc,folderPath="&path" filename="&name"))=0
2 %THEN %DO;
3 %sysfunc(fexist(&fref))
4 %let rc=%sysfunc(filename(fref));
5 %END;
6 %ELSE %DO;
7 0
8 %let syscc=0;
9 %END;
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 : Main author of the `mfv_existfile` macro: Allan Bowe. This macro calls or references distinct and copyrighted components or macros, notably: '_version.sas' (Copyright (c) 2001-2006 Rodney Sparapani) and 'print_macro_parameters.sas' (Copyright © 2022, SAS Institute Inc.).