BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sathya66
Barite | Level 11

All,

trying to get list of files  and modified date in a dataset . list of files is working but not modified date.

filename myDir "/work" ;

data test (keep=filename modified);
	did=dopen("myDir") ;
	filecount=dnum(did) ;
	
	do i=1 to filecount ;
	fid = fopen('myDir');
		filename=dread(did,i) ;
		modified=finfo(fid,"Last Modified");
		put filename= ;
		output ;
			fid=fclose(fid);
	end ;
	rc=dclose(did) ;

run ;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Make your code well-behaving by cleaning up all references it creates:

data test (keep=fname modified);
length fref $8;
if filename(fref,"/work") = 0
then do;
  did = dopen(fref);
  if did ne 0
  then do;
    dnum = dnum(did);
    do i = 1 to dnum;
      fname = dread(did, i);
      fid = mopen(did, fname);
      if fid ne 0
      then do;
        modified = finfo(fid,'Last Modified');  
        output;
        fid = dclose(fid);
      end;
    end;
    did = dclose(did);
  end;
  rc = filename(fref);
end;
run;

as otherwise it will have "memory leaks" by accruing file references and file handles.

View solution in original post

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16

please try dictionary.tables as below from libname , you will see column memname with list and modate with modified date

 

libname adam '~path of datasets';

proc sql;
create table want as select * from dictionary.tables where libname='ADAM' ;
quit;

 

 

Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

Alternatively via data step

 

data want;
set sashelp.vtable;
where  libname='ADAM' ;
keep modate memname;
run;
Thanks,
Jag
sathya66
Barite | Level 11
Hi Thanks for this.
but I am looking for list of all files(ex: text,csv,datset,etc ) and their modified date in a directory to read them into a dateset.
Jagadishkatam
Amethyst | Level 16
I just sent another code, which will provide the output you are expecting
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

please try this code as well

 

data test (keep=filename modified);
length fref $8;
     rc=filename(fref,"/work");
 if rc = 0 then do;
 did = dopen(fref);
 rc = filename(fref);
 end;
dnum = dnum(did);
do i = 1 to dnum;
filename = dread(did, i);/* If this entry is a file, then output. */
fid = mopen(did, filename);
modified=finfo(fid,'Last Modified');  
if fid > 0 then output;
end; 
rc = dclose(did);
run;
Thanks,
Jag
Kurt_Bremser
Super User

Make your code well-behaving by cleaning up all references it creates:

data test (keep=fname modified);
length fref $8;
if filename(fref,"/work") = 0
then do;
  did = dopen(fref);
  if did ne 0
  then do;
    dnum = dnum(did);
    do i = 1 to dnum;
      fname = dread(did, i);
      fid = mopen(did, fname);
      if fid ne 0
      then do;
        modified = finfo(fid,'Last Modified');  
        output;
        fid = dclose(fid);
      end;
    end;
    did = dclose(did);
  end;
  rc = filename(fref);
end;
run;

as otherwise it will have "memory leaks" by accruing file references and file handles.

s_lassen
Meteorite | Level 14

Actually, I do not think "cleaning up" is strictly necessary in a data step. It seems that SAS takes case of cleaning up when the data step ends. For instance:

 

1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         filename x "%sysfunc(pathname(WORK))";
 74         
 75         data _null_;
 76           did=dopen('x');
 77           put _ALL_;
 78         run;
 
 did=1 _ERROR_=0 _N_=1
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 
 79         
 80         data _null_;
 81           did=dopen('x');
 82           put _ALL_;
 83         run;
 
 did=1 _ERROR_=0 _N_=1

So what we see is that the directory handle number (DID=1) get reused in the second step, because it becomes deallocated as the data step ends.

 

In macros, om the other hand, and probably also in SAS/SCL programming, cleanup is not automatic, and is good practice. 

Kurt_Bremser
Super User

Yes, but ...

On UNIX systems it is standard to set the number of available file handles for a user to something like 2000 per default, and that can quickly be used in a step that works through a "industrially populated" directory. Or once one starts to work with code that reads multiple directories in one step.

s_lassen
Meteorite | Level 14

Yes, of course! You can hit the ceiling in a single datastep if you do not close the handles. Thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 3367 views
  • 6 likes
  • 4 in conversation