BookmarkSubscribeRSS Feed
Aashi07
Fluorite | Level 6

how can i check if a list of files is found in a folder and if found, access the date modified?

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hello @Aashi07,

 

This works on my Windows workstation:

/* Create sample data for demonstration */

data have;
input name :$200.;
cards;
test.sas7bdat
test0.lst
test1.log
test2.sas
;

/* Check existence and retrieve last modification date of the above files in a specified folder */

%let folder=C:\Temp\test;

data want;
length path $260 fref $8;
set have;
path="&folder\"||name;
exists=fileexist(path);
if exists then do;
  rc=filename(fref,path);
  fid=fopen(fref);
  lastmod=input(finfo(fid,'Last Modified'),nldate200.);
  rc=fclose(fid);
end;
format lastmod yymmdd10.;
keep path exists lastmod;
run;

Increase the lengths of variables NAME and PATH if necessary. Note that the "last modified" date is actually a datetime, so you could read it with the nldatm200. informat (in the INPUT function call) and use a datetime format such as e8601dt. in the FORMAT statement.

Aashi07
Fluorite | Level 6
I am using Unix
Kurt_Bremser
Super User

The code is pure SAS code and will work with minor modifications (path and FINFO item name/format) on UNIX.

 

Also see my WUSS presentation: Talking to Your Host for useful coding tips with regard to file information.

yabwon
Amethyst | Level 16

@Aashi07 , you can use %dirsAndFiles() macro from the BasePlus SAS package. It encapsulates @Kurt_Bremser 's great idea from the "Talking to Your Host" article inside a "user friendly" macro. 

 

Installation (do it only one the first time):

filename packages "/path/for/packages"; /* set a directory for packages */

filename SPFinit url "https://raw.githubusercontent.com/yabwon/SAS_PACKAGES/main/SPF/SPFinit.sas";
%include SPFinit; 

%installPackage(SPFinit baseplus) /* install SAS Packages Framework and BasePlu package*/

filename SPFinit clear;

Load package (at the beginning of new SAS session):

filename packages "/path/for/packages"; /* set a directory for packages */
%include packages (SPFinit.sas); 

%loadPackage(BasePlus)

 

Use of macro:

%dirsAndFiles(/path/you/wasnt/to/investigate, ODS=work.results, details=1)

Datat set work.results will have the directory information in it.

 

 

If you want to get help information printed in your log, just run:

%helpPackage(BasePlus,dirsAndFiles)

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



FreelanceReinh
Jade | Level 19

@Aashi07 wrote:
I am using Unix

This is not a problem as all functions used in my suggested code are available in Unix SAS, too. Of course, the path will contain forward slashes instead of backslashes and the "last modified" datetime format may be different. In a preliminary step, you could read that datetime into a character variable  like this

lastmod=finfo(fid,'Last Modified');

and then look at the character values in variable LASTMOD to find out the appropriate date or datetime informat to be used eventually.

 

See also Example 4 of the FINFO function documentation where the last modification datetime, among other file information items, is retrieved on a Unix system.

 

If you encounter difficulties, please provide details such as the complete log of the DATA step that you tried.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 514 views
  • 6 likes
  • 4 in conversation