For a head start, I have a ready-made macro (%findfiles) that can traverse directories and produce either a list of files with a specified extension in the log or write them to a SAS dataset. SYNTAX: %FindFiles(dir,<ext,dsn,sub>) dir=fully qualified directory path ext=Space delimited list of file extensions (Optional, default is ALL) dsn=name of data set to store filenames (Optional, otherwise writes to log.) sub=look in subfolders? (Y|N default is Y)
Examples: %FindFiles(c:\temp, csv) %FindFiles(\\server\folder\, xls xlsx xlsm, work.myfiles) %FindFiles(s:/workshop,sas,work.pgm_files,N)
This macro depends on three utility macros ( %exist, %fileattribs, and %translate). You can download the files from my GitHub sas-macros repository, or use this SAS code to download them:
%let path=<your custom macro storage location>;
filename maccode "&path/exists.sas";
proc http url="https://github.com/SASJedi/sas-macros/raw/master/exist.sas" out=maccode;
run;
filename maccode2 "&path/fileattribs.sas";
proc http url="https://github.com/SASJedi/sas-macros/raw/master/fileattribs.sas" out=maccode2;
run;
filename maccode3 "&path/translate.sas";
proc http url="https://github.com/SASJedi/sas-macros/raw/master/translate.sas" out=maccode3;
run;
filename maccode4 "&path/findfiles.sas";
proc http url="https://github.com/SASJedi/sas-macros/raw/master/findfiles.sas" out=maccode4;
run;
Examine the files you downloaded to see how the code works and to ensure there's nothing nefarious going on there, if you wish.
/* After examining the code you downloaded, run the macro programs to compile the macors */
%include maccode;
%include maccode2;
%include maccode3;
%include maccode4;
filename maccode clear;
filename maccode2 clear;
filename maccode3 clear;
filename maccode4 clear;
/*Get syntax help in the log for any macro using ? as the only parameter value:*/
%findfiles(?)
You can then use the dataset you created to drive the rest of your process...
For example, I ran this on my computer:
%FindFiles(D:/Courses/PG1V2, xls xlsx xlsm, work.myfiles)
proc print noobs; run;
And this was the result:
Item
Path
Filename
Size
CRDate
CRTime
ModDate
ModTime
1
D:/Courses/PG1V2/data
class.xlsx
10,000
06/12/2023
7:51:04
12/08/2017
12:13:00
2
D:/Courses/PG1V2/data
eu_sport_trade.xlsx
234,100
06/12/2023
7:51:04
12/19/2017
14:48:36
3
D:/Courses/PG1V2/data
np_info.xlsx
7,234,320
06/12/2023
7:51:04
01/30/2018
13:26:40
4
D:/Courses/PG1V2/data
storm.xlsx
6,859,276
06/12/2023
7:51:04
12/05/2019
12:51:42
Hope that helps.
... View more