Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_tree.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Recursively scans a directory tree to get all subfolders and content
4 @details
5 Usage:
6
7 %mp_tree(dir=/tmp, outds=work.tree)
8
9 Credits:
10
11 Roger Deangelis:
12https://communities.sas.com/t5/SAS-Programming/listing-all-files-within-a-directory-and-subdirectories/m-p/332616/highlight/true#M74887
13
14 Tom:
15https://communities.sas.com/t5/SAS-Programming/listing-all-files-of-all-types-from-all-subdirectories/m-p/334113/highlight/true#M75419
16
17
18 @param dir= Directory to be scanned (default=/tmp)
19 @param outds= Dataset to create (default=work.mp_tree)
20
21 @returns outds contains the following variables:
22
23 - `dir`: a flag (1/0) to say whether it is a directory or not. This is not
24 reliable - folders that you do not have permission to open will be flagged
25 as directories.
26 - `ext`: file extension
27 - `filename`: file name
28 - `dirname`: directory name
29 - `fullpath`: directory + file name
30
31 @version 9.2
32**/
33
34%macro mp_tree(dir=/tmp
35 ,outds=work.mp_tree
36)/*/STORE SOURCE*/;
37
38data &outds ;
39 length dir 8 ext filename dirname $256 fullpath $512 ;
40 call missing(of _all_);
41 fullpath = "&dir";
42run;
43
44%local sep;
45%if &sysscp=WIN or &SYSSCP eq DNTHOST %then %let sep=\;
46%else %let sep=/;
47
48data &outds ;
49 modify &outds ;
50 retain sep "&sep";
51 rc=filename('tmp',fullpath);
52 dir_id=dopen('tmp');
53 dir = (dir_id ne 0) ;
54 if dir then dirname=fullpath;
55 else do;
56 filename=scan(fullpath,-1,sep) ;
57 dirname =substrn(fullpath,1,length(fullpath)-length(filename));
58 if index(filename,'.')>1 then ext=scan(filename,-1,'.');
59 end;
60 replace;
61 if dir then do;
62 do i=1 to dnum(dir_id);
63 fullpath=cats(dirname,sep,dread(dir_id,i));
64 output;
65 end;
66 rc=dclose(dir_id);
67 end;
68 rc=filename('tmp');
69run;
70
71%mend mp_tree;