Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_tree.sas
Go to the documentation of this file.
1 /**
2  @file mm_tree.sas
3  @brief Returns all folders / subfolder content for a particular root
4  @details Shows all members and SubTrees for a particular root.
5 
6  Model:
7 
8  metauri char(64),
9  name char(256) format=$256. informat=$256. label='name',
10  path char(1024),
11  publictype char(32),
12  MetadataUpdated char(32),
13  MetadataCreated char(32)
14 
15  Usage:
16 
17  %* load macros;
18  filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
19  %inc mc;
20 
21  %* export everything;
22  %mm_tree(root= ,outds=iwantthisdataset)
23 
24  %* export everything in a specific folder;
25  %mm_tree(root=%str(/my/folder) ,outds=stuff)
26 
27  %* export only folders;
28  %mm_tree(root=%str(/my/folder) ,types=Folder ,outds=stuf)
29 
30  %* show only exportable content;
31  %mm_tree(root=%str(/) ,types=EXPORTABLE ,outds=exportable)
32 
33  %* with specific types;
34  %mm_tree(root=%str(/my/folder)
35  ,types=
36  DeployedJob
37  ExternalFile
38  Folder
39  Folder.SecuredData
40  GeneratedTransform
41  InformationMap.Relational
42  Job
43  Library
44  Prompt
45  StoredProcess
46  Table
47  ,outds=morestuff)
48 
49  <h4> Dependencies </h4>
50  @li mf_getquotedstr.sas
51  @li mm_getpublictypes.sas
52  @li mf_isblank.sas
53 
54  @param root= the parent folder under which to return all contents
55  @param outds= the dataset to create that contains the list of directories
56  @param types= Space-seperated, unquoted list of types for filtering the
57  output. Special types:
58 
59  * ALl - return all types (the default)
60  * EXPORTABLE - return only the content types that can be exported in an SPK
61 
62  @version 9.4
63  @author Allan Bowe
64 
65 **/
66 %macro mm_tree(
67  root=
68  ,types=ALL
69  ,outds=work.mm_tree
70 )/*/STORE SOURCE*/;
71 options noquotelenmax;
72 
73 %if %mf_isblank(&root) %then %let root=/;
74 
75 %if %str(&types)=EXPORTABLE %then %do;
76  data;run;%local tempds; %let tempds=&syslast;
77  %mm_getpublictypes(outds=&tempds)
78  proc sql noprint;
79  select publictype into: types separated by ' ' from &tempds;
80  drop table &tempds;
81 %end;
82 
83 * use a temporary fileref to hold the response;
84 filename response temp;
85 /* get list of libraries */
86 proc metadata in=
87  '<GetMetadataObjects><Reposid>$METAREPOSITORY</Reposid>
88  <Type>Tree</Type><Objects/><NS>SAS</NS>
89  <Flags>384</Flags>
90  <XMLSelect search="*[@TreeType=&apos;BIP Folder&apos;]"/>
91  <Options/></GetMetadataObjects>'
92  out=response;
93 run;
94 /*
95 data _null_;
96  infile response;
97  input;
98  put _infile_;
99  run;
100 */
101 
102 /* create an XML map to read the response */
103 filename sxlemap temp;
104 data _null_;
105  file sxlemap;
106  put '<SXLEMAP version="1.2" name="SASObjects"><TABLE name="SASObjects">';
107  put "<TABLE-PATH syntax='XPath'>/GetMetadataObjects/Objects/Tree</TABLE-PATH>";
108  put '<COLUMN name="pathuri">';
109  put "<PATH syntax='XPath'>/GetMetadataObjects/Objects/Tree/@Id</PATH>";
110  put "<TYPE>character</TYPE><DATATYPE>string</DATATYPE><LENGTH>64</LENGTH>";
111  put '</COLUMN><COLUMN name="name">';
112  put "<PATH syntax='XPath'>/GetMetadataObjects/Objects/Tree/@Name</PATH>";
113  put "<TYPE>character</TYPE><DATATYPE>string</DATATYPE><LENGTH>256</LENGTH>";
114  put '</COLUMN></TABLE></SXLEMAP>';
115 run;
116 libname _XML_ xml xmlfileref=response xmlmap=sxlemap;
117 
118 data &outds;
119  length metauri pathuri $64 name $256 path $1024
120  publictype MetadataUpdated MetadataCreated $32;
121  set _XML_.SASObjects;
122  keep metauri name publictype MetadataUpdated MetadataCreated path;
123  length parenturi pname $128 ;
124  call missing(parenturi,pname);
125  path=cats('/',name);
126  /* get parents */
127  tmpuri=pathuri;
128  do while (metadata_getnasn(tmpuri,"ParentTree",1,parenturi)>0);
129  rc=metadata_getattr(parenturi,"Name",pname);
130  path=cats('/',pname,path);
131  tmpuri=parenturi;
132  end;
133 
134  if path=:"&root";
135 
136  %if "&types"="ALL" or ("&types" ne "ALL" and "&types" ne "Folder") %then %do;
137  n=1;
138  do while (metadata_getnasn(pathuri,"Members",n,metauri)>0);
139  n+1;
140  call missing(name,publictype,MetadataUpdated,MetadataCreated);
141  rc=metadata_getattr(metauri,"Name", name);
142  rc=metadata_getattr(metauri,"MetadataUpdated", MetadataUpdated);
143  rc=metadata_getattr(metauri,"MetadataCreated", MetadataCreated);
144  rc=metadata_getattr(metauri,"PublicType", PublicType);
145  %if "&types" ne "ALL" %then %do;
146  if publictype in (%mf_getquotedstr(&types)) then output;
147  %end;
148  %else output; ;
149  end;
150  %end;
151 
152  rc=metadata_resolve(pathuri,pname,tmpuri);
153  metauri=cats('OMSOBJ:',pname,'\',pathuri);
154  rc=metadata_getattr(metauri,"Name", name);
155  rc=metadata_getattr(pathuri,"MetadataUpdated", MetadataUpdated);
156  rc=metadata_getattr(pathuri,"MetadataCreated", MetadataCreated);
157  rc=metadata_getattr(pathuri,"PublicType", PublicType);
158  path=substr(path,1,length(path)-length(name)-1);
159  if publictype ne '' then output;
160 run;
161 
162 proc sort;
163  by path;
164 run;
165 
166 /* clear references */
167 filename sxlemap clear;
168 filename response clear;
169 libname _XML_ clear;
170 
171 %mend;