SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
febyinkasid
Calcite | Level 5

Hi all,

I have this logic to retrieve all file inside a folder, I want to add last modified or created date of the file inside.

Where can I add and what procedure to use? Thanks

/*FIND PATH PARENT*/
%let pathbase=G:\Shared drives\;
data filebase;
length fref $8 fname $200;
did = filename(fref,"&pathbase.");
did = dopen(fref);
do i = 1 to dnum(did);
	fname = dread(did,i);
output;
end;
did = dclose(did);
did = filename(fref);
keep fname i;
run;

febyinkasid_0-1726493825070.png

 

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hi @febyinkasid,

 

You can use the MOPEN, FINFO and FCLOSE functions:

/*FIND PATH PARENT*/
%let pathbase=G:\Shared drives\;
data filebase;
length fref $8 fname $200;
did = filename(fref,"&pathbase.");
did = dopen(fref);
do i = 1 to dnum(did);
  fname = dread(did,i);
  fid=mopen(did, fname);
  created=input(finfo(fid,'Create Time'),nldatm200.);
  lastmod=input(finfo(fid,'Last Modified'),nldatm200.);
  rc=fclose(fid);
output;
end;
did = dclose(did);
did = filename(fref);
format created lastmod e8601dt.;
keep fname i created lastmod;
run;
febyinkasid
Calcite | Level 5

Hi, Thank You...

I cant retrieve the date, though there is no error in log

febyinkasid_0-1726499501500.png

 

FreelanceReinh
Jade | Level 19

It worked with a local directory on my Windows workstation. What does the PUT statement write to the log if you add it like in the modified code below?

data filebase;
length fref $8 fname created lastmod $200;
did = filename(fref,"&pathbase.");
did = dopen(fref);
do i = 1 to dnum(did);
  fname = dread(did,i);
  fid=mopen(did, fname);
  put fid=;
  created=finfo(fid,'Create Time');
  lastmod=finfo(fid,'Last Modified');
  rc=fclose(fid);
output;
end;
did = dclose(did);
did = filename(fref);
keep fname i created lastmod;
run;

I've also simplified the assignment statements for the two new variables by using their default (character!) format. Note the deletion of the FORMAT statement.

febyinkasid
Calcite | Level 5

Oooh it workss! I think I wrote a path that consists of folder thats why.

Thank You so much

Tom
Super User Tom
Super User

Use this macro:  https://github.com/sasutils/macros/blob/master/dirtree.sas

 

Of just read the macro to see how it is finding the date for each file. Basically you have to open each file so that you can use the FINFO() function. 

 

Note that FINFO() wants the NAME of the option passed to it, but the name it wants depends on the language settings of your session, so use the FOPTNAME() function to find the right name to use.  Also the string returned depends on the language setting, so use the right informat to convert to a value datetime value.

      lastmod = input(finfo(fid,foptname(fid, 5)), nldatm100.);

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 905 views
  • 5 likes
  • 3 in conversation