I don't seem to be able to figure this out, but I'd like to pull into a dataset a listing of filenames and modification dates from a network folder. I am thinking this is simple, but I don't seem be able find a solution that works. Any thoughts would be appreciated.
You can use the %findfiles macro from my public GitHub sas-macros repository to do this pretty easily.
/* Get the macros straight from GitHub */
/* Supporting macro utilities */
filename attrib "fileattribs.sas";
filename exist "exist.sas";
filename trans "translate.sas";
/* The main FindFiles macro */
filename find "findfiles.sas";
proc http
url="https://raw.githubusercontent.com/SASJedi/sas-macros/master/findfiles.sas"
out=find;
run;
proc http
url="https://raw.githubusercontent.com/SASJedi/sas-macros/master/exist.sas"
out=exist;
run;
proc http
url="https://raw.githubusercontent.com/SASJedi/sas-macros/master/fileattribs.sas"
out=attrib;
run;
proc http
url="https://raw.githubusercontent.com/SASJedi/sas-macros/master/translate.sas"
out=trans;
run;
/* Review the contents of the macro programs before running them! */
data _null_;
infile attrib;
file print;
input;
put _infile_;
run;
data _null_;
infile exist;
file print;
input;
put _infile_;
run;
data _null_;
infile trans;
file print;
input;
put _infile_;
run;
data _null_;
infile find;
file print;
input;
put _infile_;
run;
/* Run the macro programs to compile the macros */
%include attrib;
%include exist;
%include trans;
%include find;
/* Get syntax help for FindFiles in the SAS log */
%FindFiles(?)
From the log:
NOTE: *FINDFILES Documentation ******************************* Produces a list of files with a specified extension in the log or optionally writes them to a 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) Example: %FindFiles(c:\temp, csv) %FindFiles(\\server\folder\, xls xlsx xlsm, work.myfiles) %FindFiles(s:/workshop,sas,work.pgm_files,N) *************************************************************
Now, use the macro to produce a dataset named work.files containing information for all the files in \\localhost\c$\temp and all subfolders:
%FindFiles(\\localhost\c$\temp,,work.Files)
proc print data=work.files(obs=3);
run;
A sample of the results:
Obs | Item | Path | Filename | Size | CRDate | CRTime | ModDate | ModTime |
---|---|---|---|---|---|---|---|---|
1 | 1 | //localhost/c$/temp/subfolder | another_solution.sas | 878 | 11/17/2023 | 8:43:59 | 11/10/2023 | 16:42:43 |
2 | 1 | //localhost/c$/temp | have.csv | 208 | 10/13/2023 | 8:29:05 | 10/13/2023 | 8:29:05 |
3 | 2 | //localhost/c$/temp | my.html | 943 | 07/11/2023 | 16:38:10 | 07/11/2023 | 16:38:43 |
May the SAS be with you!
Mark
Sorry, but I cannot seem to get that macro to work.
I am able to get this code to work, but only for the filename. I am not exactly familiar with the loop and how to insert the date modified into this process. Any thoughts are appreciated.
data filenames;
length fref $8 fname $200 ;
did = filename(fref,"\\My Folder");
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
output;
end;
did = dclose(did);
did = filename(fref);
keep fname;
run;
I think that macro is calling other macros. So you will need to download and compile those also.
Or you could use this one which does not depend on other macros. https://github.com/sasutils/macros/blob/master/dirtree.sas
Essentially you need to also use the FILENAME(), FOPEN(), FCLOSE() and FINFO() and perhaps FOPTNAME() functions to make a fileref pointing to each file and then open the file and retrieve information about the file, like the last modified date.
If the XCMD option is enabled you could also just use a PIPE and read the output of the DIR command.
So something like:
data want;
infile "dir \\My Folder" pipe truncover;
input .... ;
run;
I think the following code is pretty dangerous:
/* Get the macro straight from GitHub */
filename code url "https://raw.githubusercontent.com/SASJedi/sas-macros/master/findfiles.sas";
%include code;
and should never be accepted in a serious working environment. I assume (but that is only an assumption) that you are a nice guy that would never put malicious code into your programs on github, but if you yourself are equally careless about running code that you have not even looked at, there is a serious risk that somebody else will hack your computer and plant malicious code in your programs.
Imported code should be revisited before running, and should be copied to a local file to secure against unauthorized change.
That is a very valid observation, @s_lassen. I trust this code because I wrote and maintain it, but others should review before execution.
I'll modify the solution to use PROC HTTP to first download the file, giving the user the opportunity to review the code before running it. Thanks for keeping me honest!
Mark
This is what I was looking for:
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.