Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mm_createfolder.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Recursively create a metadata folder
4 @details This macro was inspired by Paul Homes who wrote an early
5 version (mkdirmd.sas) in 2010. The original is described here:
6 https://platformadmin.com/blogs/paul/2010/07/mkdirmd/
7
8 The macro will NOT create a new ROOT folder - not
9 because it can't, but more because that is generally not something
10 your administrator would like you to do!
11
12 The macro is idempotent - if you run it twice, it will only create a folder
13 once.
14
15 Usage:
16
17 %mm_createfolder(path=/some/meta/folder)
18
19 @param [in] path= Name of the folder to create.
20 @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
21
22
23 @version 9.4
24 @author Allan Bowe
25
26 @sasjslint {"maxLineLength": 82}
27**/
28
29%macro mm_createfolder(path=,mDebug=0);
30%put &sysmacroname: execution started for &path;
31%local dbg errorcheck;
32%if &mDebug=0 %then %let dbg=*;
33
34%local parentFolderObjId child errorcheck paths;
35%let paths=0;
36%let errorcheck=1;
37
38%if &syscc ge 4 %then %do;
39 %put SYSCC=&syscc - this macro requires a clean session;
40 %return;
41%end;
42
43data _null_;
44 length objId parentId objType parent child $200
45 folderPath $1000;
46 call missing (of _all_);
47 folderPath = "%trim(&path)";
48
49 /* remove any trailing slash */
50 if ( substr(folderPath,length(folderPath),1) = '/' ) then
51 folderPath=substr(folderPath,1,length(folderPath)-1);
52
53 /* name must not be blank */
54 if ( folderPath = '' ) then do;
55 put 'ERR' +(-1) "OR: &sysmacroname PATH parameter value must be non-blank";
56 end;
57
58 /* must have a starting slash */
59 if ( substr(folderPath,1,1) ne '/' ) then do;
60 put 'ERR' +(-1) "OR: &sysmacroname PATH param value must have starting slash";
61 stop;
62 end;
63
64 /* check if folder already exists */
65 rc=metadata_pathobj('',cats(folderPath,"(Folder)"),"",objType,objId);
66 if rc ge 1 then do;
67 put "NOTE: Folder " folderPath " already exists!";
68 stop;
69 end;
70
71 /* do not create a root (one level) folder */
72 if countc(folderPath,'/')=1 then do;
73 put 'ERR' +(-1) "OR: &sysmacroname will not create a new ROOT folder";
74 stop;
75 end;
76
77 /* check that root folder exists */
78 root=cats('/',scan(folderpath,1,'/'),"(Folder)");
79 if metadata_pathobj('',root,"",objType,parentId)<1 then do;
80 put 'ERR' +(-1) "OR: " root " does not exist!";
81 stop;
82 end;
83
84 /* check that parent folder exists */
85 child=scan(folderPath,-1,'/');
86 parent=substr(folderpath,1,length(folderpath)-length(child)-1);
87 rc=metadata_pathobj('',cats(parent,"(Folder)"),"",objType,parentId);
88 if rc<1 then do;
89 putlog 'The following folders will be created:';
90 /* folder does not exist - so start from top and work down */
91 length newpath $1000;
92 paths=0;
93 do x=2 to countw(folderpath,'/');
94 newpath='';
95 do i=1 to x;
96 newpath=cats(newpath,'/',scan(folderpath,i,'/'));
97 end;
98 rc=metadata_pathobj('',cats(newpath,"(Folder)"),"",objType,parentId);
99 if rc<1 then do;
100 paths+1;
101 call symputx(cats('path',paths),newpath);
102 putlog newpath;
103 end;
104 call symputx('paths',paths);
105 end;
106 end;
107 else putlog "parent " parent " exists";
108
109 call symputx('parentFolderObjId',parentId,'l');
110 call symputx('child',child,'l');
111 call symputx('errorcheck',0,'l');
112
113 &dbg put (_all_)(=);
114run;
115
116%if &errorcheck=1 or &syscc ge 4 %then %return;
117
118%if &paths>0 %then %do x=1 %to &paths;
119 %put executing recursive call for &&path&x;
120 %mm_createfolder(path=&&path&x)
121%end;
122%else %do;
123 filename __newdir temp;
124 options noquotelenmax;
125 %local inmeta;
126 %put creating: &path;
127 %let inmeta=<AddMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata>
128 <Tree Name='&child' PublicType='Folder' TreeType='BIP Folder'
129 UsageVersion='1000000'><ParentTree><Tree ObjRef='&parentFolderObjId'/>
130 </ParentTree></Tree></Metadata><NS>SAS</NS><Flags>268435456</Flags>
131 </AddMetadata>;
132
133 proc metadata in="&inmeta" out=__newdir verbose;
134 run ;
135
136 /* check it was successful */
137 data _null_;
138 length objId parentId objType parent child $200 ;
139 call missing (of _all_);
140 rc=metadata_pathobj('',cats("&path","(Folder)"),"",objType,objId);
141 if rc ge 1 then do;
142 putlog "SUCCCESS! &path created.";
143 end;
144 else do;
145 putlog 'ERR' +(-1) "OR: unsuccessful attempt to create &path";
146 call symputx('syscc',8);
147 end;
148 run;
149
150 /* write the response to the log for debugging */
151 %if &mDebug ne 0 %then %do;
152 data _null_;
153 infile __newdir lrecl=32767;
154 input;
155 put _infile_;
156 run;
157 %end;
158 filename __newdir clear;
159%end;
160
161%put &sysmacroname: execution finished for &path;
162%mend mm_createfolder;