Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_getfoldertree.sas
Go to the documentation of this file.
1 /**
2  @file mm_getfoldertree.sas
3  @brief Returns all folders / subfolder content for a particular root
4  @details Shows all members and SubTrees recursively for a particular root.
5  Note - for big sites, this returns a lot of data! So you may wish to reduce
6  the logging to speed up the process (see example below)
7  Usage:
8 
9  options ps=max nonotes nosource;
10  %mm_getfoldertree(root=/My/Meta/Path, outds=iwantthisdataset)
11  options notes source;
12 
13  @param root= the parent folder under which to return all contents
14  @param outds= the dataset to create that contains the list of directories
15  @param mDebug= set to 1 to show debug messages in the log
16 
17  <h4> Dependencies </h4>
18 
19  @version 9.4
20  @author Allan Bowe
21 
22 **/
23 %macro mm_getfoldertree(
24  root=
25  ,outds=work.mm_getfoldertree
26  ,mDebug=0
27  ,depth=50 /* how many nested folders to query */
28  ,level=1 /* system var - to track current level depth */
29  ,append=NO /* system var - when YES means appending within nested loop */
30 )/*/STORE SOURCE*/;
31 
32 %if &level>&depth %then %return;
33 
34 %local mD;
35 %if &mDebug=1 %then %let mD=;
36 %else %let mD=%str(*);
37 %&mD.put Executing &sysmacroname;
38 %&mD.put _local_;
39 
40 %if &append=NO %then %do;
41  /* ensure table doesn't exist already */
42  data &outds; run;
43  proc sql; drop table &outds;
44 %end;
45 
46 /* get folder contents */
47 data &outds.TMP/view=&outds.TMP;
48  length metauri pathuri $64 name $256 path $1024
49  assoctype publictype MetadataUpdated MetadataCreated $32;
50  keep metauri assoctype name publictype MetadataUpdated MetadataCreated path;
51  call missing(of _all_);
52  path="&root";
53  rc=metadata_pathobj("",path,"Folder",publictype,pathuri);
54  if publictype ne 'Tree' then do;
55  putlog "%str(WAR)NING: Tree " path 'does not exist!' publictype=;
56  stop;
57  end;
58  __n1=1;
59  do while(metadata_getnasl(pathuri,__n1,assoctype)>0);
60  __n1+1;
61  /* Walk through all possible associations of this object. */
62  __n2=1;
63  if assoctype in ('Members','SubTrees') then
64  do while(metadata_getnasn(pathuri,assoctype,__n2,metauri)>0);
65  __n2+1;
66  call missing(name,publictype,MetadataUpdated,MetadataCreated);
67  __rc1=metadata_getattr(metauri,"Name", name);
68  __rc2=metadata_getattr(metauri,"MetadataUpdated", MetadataUpdated);
69  __rc3=metadata_getattr(metauri,"MetadataCreated", MetadataCreated);
70  __rc4=metadata_getattr(metauri,"PublicType", PublicType);
71  output;
72  end;
73  n1+1;
74  end;
75  drop __:;
76 run;
77 
78 proc append base=&outds data=&outds.TMP;
79 run;
80 
81 data _null_;
82  set &outds.TMP(where=(assoctype='SubTrees'));
83  call execute('%mm_getfoldertree(root='
84  !!cats(path,"/",name)!!",outds=&outds,mDebug=&mdebug,depth=&depth"
85  !!",level=%eval(&level+1),append=YES)");
86 run;
87 
88 %mend;