Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mf_mkdir.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Creates a directory, including any intermediate directories
4  @details Works on windows and unix environments via dcreate function.
5 Usage:
6 
7  %mf_mkdir(/some/path/name)
8 
9 
10  @param dir relative or absolute pathname. Unquoted.
11  @version 9.2
12 
13 **/
14 
15 %macro mf_mkdir(dir
16 )/*/STORE SOURCE*/;
17 
18  %local lastchar child parent;
19 
20  %let lastchar = %substr(&dir, %length(&dir));
21  %if (%bquote(&lastchar) eq %str(:)) %then %do;
22  /* Cannot create drive mappings */
23  %return;
24  %end;
25 
26  %if (%bquote(&lastchar)=%str(/)) or (%bquote(&lastchar)=%str(\)) %then %do;
27  /* last char is a slash */
28  %if (%length(&dir) eq 1) %then %do;
29  /* one single slash - root location is assumed to exist */
30  %return;
31  %end;
32  %else %do;
33  /* strip last slash */
34  %let dir = %substr(&dir, 1, %length(&dir)-1);
35  %end;
36  %end;
37 
38  %if (%sysfunc(fileexist(%bquote(&dir))) = 0) %then %do;
39  /* directory does not exist so prepare to create */
40  /* first get the childmost directory */
41  %let child = %scan(&dir, -1, %str(/\:));
42 
43  /*
44  If child name = path name then there are no parents to create. Else
45  they must be recursively scanned.
46  */
47 
48  %if (%length(&dir) gt %length(&child)) %then %do;
49  %let parent = %substr(&dir, 1, %length(&dir)-%length(&child));
50  %mf_mkdir(&parent)
51  %end;
52 
53  /*
54  Now create the directory. Complain loudly of any errors.
55  */
56 
57  %let dname = %sysfunc(dcreate(&child, &parent));
58  %if (%bquote(&dname) eq ) %then %do;
59  %put %str(ERR)OR: could not create &parent + &child;
60  %abort cancel;
61  %end;
62  %else %do;
63  %put Directory created: &dir;
64  %end;
65  %end;
66  /* exit quietly if directory did exist.*/
67 %mend;