CORE
mf_mkdir.sas
Go to the documentation of this file.
1 
14 %macro mf_mkdir(dir);
15 
16  %local lastchar child parent;
17 
18  %let lastchar = %substr(&dir, %length(&dir));
19  %if (%bquote(&lastchar) eq %str(:)) %then %do;
20  /* Cannot create drive mappings */
21  %return;
22  %end;
23 
24  %if (%bquote(&lastchar)=%str(/)) or (%bquote(&lastchar)=%str(\)) %then %do;
25  /* last char is a slash */
26  %if (%length(&dir) eq 1) %then %do;
27  /* one single slash - root location is assumed to exist */
28  %return;
29  %end;
30  %else %do;
31  /* strip last slash */
32  %let dir = %substr(&dir, 1, %length(&dir)-1);
33  %end;
34  %end;
35 
36  %if (%sysfunc(fileexist(%bquote(&dir))) = 0) %then %do;
37  /* directory does not exist so prepare to create */
38  /* first get the childmost directory */
39  %let child = %scan(&dir, -1, %str(/\:));
40 
41  /*
42  If child name = path name then there are no parents to create. Else
43  they must be recursively scanned.
44  */
45 
46  %if (%length(&dir) gt %length(&child)) %then %do;
47  %let parent = %substr(&dir, 1, %length(&dir)-%length(&child));
48  %mf_mkdir(&parent);
49  %end;
50 
51  /*
52  Now create the directory. Complain loudly of any errors.
53  */
54 
55  %let dname = %sysfunc(dcreate(&child, &parent));
56  %if (%bquote(&dname) eq ) %then %do;
57  %put ERROR: could not create &parent\&child;
58  %abort cancel;
59  %end;
60  %else %do;
61  %put Directory created: &dir;
62  %end;
63  %end;
64  /* exit quietly if directory did exist.*/
65 %mend;