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