Production Ready Macros for SAS Application Developers
mm_createfolder.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief 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=/Tests/some folder)
18 
19  @param path= Name of the folder to create.
20  @param mdebug= set DBG to 1 to disable DEBUG messages
21 
22  @version 9.4
23  @author Allan Bowe
24 
25 **/
26 
27 %macro mm_createfolder(path=,mDebug=0);
28 %local dbg errorcheck;
29 %if &mDebug=0 %then %let dbg=*;
30 %let errorcheck=1;
31 
32 data _null_;
33  length objId parentFolderObjId objType parent child $200
34  folderPath $1000;
35  call missing (of _all_);
36  folderPath = cats(symget('path'));
37 
38  * remove any trailing slash ;
39  if ( substr(folderPath,length(folderPath),1) = '/' ) then
40  folderPath=substr(folderPath,1,length(folderPath)-1);
41 
42  * name must not be blank;
43  if ( folderPath = '' ) then do;
44  put "%str(ERR)OR: &sysmacroname PATH parameter value must be non-blank";
45  end;
46 
47  * must have a starting slash ;
48  if ( substr(folderPath,1,1) ne '/' ) then do;
49  put "%str(ERR)OR: &sysmacroname PATH parameter value must have starting slash";
50  stop;
51  end;
52 
53  * check if folder already exists ;
54  rc=metadata_pathobj('',cats(folderPath,"(Folder)"),"",objType,objId);
55  if rc ge 1 then do;
56  put "NOTE: Folder " folderPath " already exists!";
57  stop;
58  end;
59 
60  * do not create a root (one level) folder ;
61  if countc(folderPath,'/')=1 then do;
62  put "%str(ERR)OR: &sysmacroname will not create a new ROOT folder";
63  stop;
64  end;
65 
66  * check that parent folder exists ;
67  child=scan(folderPath,-1,'/');
68  parent=substr(folderpath,1,length(folderpath)-length(child)-1);
69  put parent=;
70  rc=metadata_pathobj('',cats(parent,"(Folder)"),"",objType,parentFolderObjId);
71  if rc<1 then do;
72  put "%str(ERR)OR: &sysmacroname: parent folder does not exist! " parent;
73  put (_all_)(=);
74  stop;
75  end;
76 
77  call symputx('parentFolderObjId',parentFolderObjId,'l');
78  call symputx('child',child,'l');
79  call symputx('errorcheck',0,'l');
80 
81  &dbg put (_all_)(=);
82 run;
83 
84 %if &errorcheck=1 %then %return;
85 
86 /* now create the folder */
87 
88 filename __newdir temp;
89 options noquotelenmax;
90 %local inmeta;
91 %let inmeta=<AddMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata>
92  <Tree Name='&child' PublicType='Folder' TreeType='BIP Folder' UsageVersion='1000000'>
93  <ParentTree><Tree ObjRef='&parentFolderObjId'/></ParentTree></Tree></Metadata>
94  <NS>SAS</NS><Flags>268435456</Flags></AddMetadata>;
95 
96 proc metadata in="&inmeta"
97  out=__newdir verbose;
98 run;
99 
100 %if &mDebug ne 0 %then %do;
101  /* write the response to the log for debugging */
102  data _null_;
103  infile __newdir lrecl=32767;
104  input;
105  put _infile_;
106  run;
107 %end;
108 
109 filename __newdir clear;
110 
111 %mend;