Published on :
Utility AUCUNE

Macro mf_mkdir - Recursive Directory Creation

This code is also available in: Deutsch Español Français
The `mf_mkdir` macro is designed to create a directory specified by the `dir` parameter. It robustly handles paths by checking and removing any trailing slash characters (`/` or `\`), and by ignoring paths that represent single drive letters (like 'C:').
Before attempting to create a directory, the macro uses `%sysfunc(fileexist())` to check for its existence. If the directory does not exist, it breaks down the path into its deepest child directory (`child`) and its parent directory (`parent`). It then recursively calls itself (`%mf_mkdir(&parent)`) to ensure all parent directories are created. Once the parents are established, it uses `%sysfunc(dcreate(&child, &parent))` to create the final directory. In case of creation failure, an error message is displayed via `%put ERROR:` and execution is aborted with `%abort cancel`. If the directory already exists, the macro exits silently. The macro is designed to work on both Windows and Unix environments, making it compatible with SAS© Viya 4.
Data Analysis

Type : AUCUNE


The macro does not process SAS datasets. It operates directly on the file system using a directory path provided as a parameter.

1 Code Block
Macro %mf_mkdir
Explanation :
This block contains the entire definition of the `%mf_mkdir` macro. The macro takes a directory path (`dir`) as a parameter.

It starts with input checks: it detects and ignores paths that are drive letters (e.g., 'C:') and removes trailing slashes if present, except for the root directory. This ensures consistent path handling.

The core of the logic is a conditional block that executes if the target directory does not exist (checked by `%sysfunc(fileexist)`). Inside this block:
1. The path is decomposed to identify the last component (`child`) and its parent path (`parent`).
2. The macro recursively calls itself (`%mf_mkdir(&parent)`) to ensure all parent directories exist before attempting to create the current directory.
3. The `child` directory is created within the `parent` using the `%sysfunc(dcreate(&child, &parent))` function. This function is compatible with both Windows and Unix file systems.
4. Basic error handling is included: if `dcreate` fails (returns an empty string), an error message is displayed (`%put ERROR:`) and the SAS session execution is stopped (`%abort cancel`). If successful, a confirmation message is displayed.

The macro is designed to be idempotent: if the directory already exists, it does nothing and exits silently.
Copied!
1%macro mf_mkdir(dir
2)/*/STORE SOURCE*/;
3 
4 %local lastchar child parent;
5 
6 %let lastchar = %substr(&dir, %LENGTH(&dir));
7 %IF (%bquote(&lastchar) eq %str(:)) %THEN %DO;
8 /* Cannot create drive mappings */
9 %return;
10 %END;
11 
12 %IF (%bquote(&lastchar)=%str(/)) or (%bquote(&lastchar)=%str(\\_)) %THEN %DO;
13 /* last char is a slash */
14 %IF (%LENGTH(&dir) eq 1) %THEN %DO;
15 /* one single slash - root location is assumed to exist */
16 %return;
17 %END;
18 %ELSE %DO;
19 /* strip last slash */
20 %let dir = %substr(&dir, 1, %LENGTH(&dir)-1);
21 %END;
22 %END;
23 
24 %IF (%sysfunc(fileexist(%bquote(&dir))) = 0) %THEN %DO;
25 /* directory does not exist so prepare to create */
26 /* first get the childmost directory */
27 %let child = %scan(&dir, -1, %str(/\\:));
28 
29 /*
30 If child name = path name then there are no parents to create. Else
31 they must be recursively scanned.
32 */
33 
34 %IF (%LENGTH(&dir) gt %LENGTH(&child)) %THEN %DO;
35 %let parent = %substr(&dir, 1, %LENGTH(&dir)-%LENGTH(&child));
36 %mf_mkdir(&parent)
37 %END;
38 
39 /*
40 Now create the directory. Complain loudly of any errs.
41 */
42 
43 %let dname = %sysfunc(dcreate(&child, &parent));
44 %IF (%bquote(&dname) eq ) %THEN %DO;
45 %put %str(ERR)OR: could not create &parent + &child;
46 %abort cancel;
47 %END;
48 %ELSE %DO;
49 %put Directory created: &dir;
50 %END;
51 %END;
52 /* exit quietly if directory did exist.*/
53%mend mf_mkdir;
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 (c) 2001-2006 Rodney Sparapani; Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de (information extracted from the _version.sas file referenced in the macro's help).