Published on :
Utility CREATION_INTERNE

Check for Folder Existence on SAS Drive

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
The `mfv_existfolder` macro takes a folder path as a parameter (`path`). It uses the `filename` function with the `filesrvc` engine to test for the presence of the folder on SAS© Drive. The standard behavior of the `filename` function is to modify the value of `&syscc` on failure (folder not found). To control this behavior, the macro includes an explicit reset of `&syscc` to 0 if the folder is not found. Calls to the `mf_abort` macro are present at the beginning and end to handle error conditions and ensure a clean exit if `&syscc` indicates an issue before or after the main body of the macro executes. Using a unique fileref (`%mf_getuniquefileref()`) ensures there are no conflicts with other filerefs. Interaction with SAS© Drive and the `filesrvc` engine may involve operations requiring administrative privileges, hence the 'admin: 1' classification.
Data Analysis

Type : CREATION_INTERNE


The macro does not process data per se, but interacts with the SAS Drive file system (folder existence metadata). No external data is read or created within the macro itself for analytical processing.

1 Code Block
MACRO (mf_abort)
Explanation :
This block checks the value of the `&syscc` system variable. If `&syscc` is not zero (indicating a previous error), the `mf_abort` macro is called to stop execution and display an error message. This ensures that the `mfv_existfolder` macro does not run if the environment is already in an error state.
Copied!
1%mf_abort(
2 iftrue=(&syscc ne 0),
3 msg=Cannot enter mfv_existfolder.sas with syscc=&syscc
4 )
2 Code Block
MACRO
Explanation :
Declares the local macro variables `fref`, `rc`, and `var`. The `fref` variable is then assigned a unique fileref via the `mf_getuniquefileref` macro to avoid conflicts and ensure the uniqueness of the temporary fileref.
Copied!
1%local fref rc var;
2%let fref=%mf_getuniquefileref();
3 
3 Code Block
MACRO
Explanation :
This block is the core of the macro. It attempts to assign a fileref (`fref`) to a folder path (`&path`) on SAS Drive using the `filesrvc` engine. The `filename` function returns 0 if the assignment is successful (which indicates that the folder exists). If the folder exists, the macro returns the value '1'. If the folder does not exist, the `filename` function sets `&syscc` to a non-zero value; the macro then resets `&syscc` to 0 in the `%else` block to prevent this failure from affecting future operations. The `var` variable and the fileref are cleaned up after use.
Copied!
1%IF %sysfunc(filename(fref,,filesrvc,folderPath="&path"))=0 %THEN %DO;
2 1
3 %let var=_FILESRVC_&fref._URI;
4 %let rc=%sysfunc(filename(fref));
5 %symdel &var;
6 %END;
7 %ELSE %DO;
8 0
9 %let syscc=0;
10 %END;
4 Code Block
MACRO (mf_abort)
Explanation :
This block re-checks the value of `&syscc` before exiting the macro. If `&syscc` is non-zero, the `mf_abort` macro is called to signal an error, indicating a problem that occurred during the execution of the `mfv_existfolder` macro's body.
Copied!
1%mf_abort(
2 iftrue=(&syscc ne 0),
3 msg=Cannot leave mfv_existfolder.sas with syscc=&syscc
4 )
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 : Allan Bowe