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!
%macro mf_mkdir(dir
)/*/STORE SOURCE*/;
%local lastchar child parent;
%let lastchar = %substr(&dir, %length(&dir));
%if (%bquote(&lastchar) eq %str(:)) %then %do;
/* Cannot create drive mappings */
%return;
%end;
%if (%bquote(&lastchar)=%str(/)) or (%bquote(&lastchar)=%str(\\_)) %then %do;
/* last char is a slash */
%if (%length(&dir) eq 1) %then %do;
/* one single slash - root location is assumed to exist */
%return;
%end;
%else %do;
/* strip last slash */
%let dir = %substr(&dir, 1, %length(&dir)-1);
%end;
%end;
%if (%sysfunc(fileexist(%bquote(&dir))) = 0) %then %do;
/* directory does not exist so prepare to create */
/* first get the childmost directory */
%let child = %scan(&dir, -1, %str(/\\:));
/*
If child name = path name then there are no parents to create. Else
they must be recursively scanned.
*/
%if (%length(&dir) gt %length(&child)) %then %do;
%let parent = %substr(&dir, 1, %length(&dir)-%length(&child));
%mf_mkdir(&parent)
%end;
/*
Now create the directory. Complain loudly of any errs.
*/
%let dname = %sysfunc(dcreate(&child, &parent));
%if (%bquote(&dname) eq ) %then %do;
%put %str(ERR)OR: could not create &parent + &child;
%abort cancel;
%end;
%else %do;
%put Directory created: &dir;
%end;
%end;
/* exit quietly if directory did exist.*/
%mend mf_mkdir;
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 */
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).
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.