below code (TOM'S) run very well on a computer. While on my computer isize' and 'dte' columns returns blanks.
Anyone can help me? Thank you.
%macro getfilesizes(directory,extension,dataset) ;
data &dataSet ;
length file $256 dte 8 size 8 directory $256;
format dte datetime.;
keep file dte size directory ;
length filrf filrfb $8 ;
directory="&directory";
rc1=filename(filrf,directory);
did=dopen(filrf);
memcount=dnum(did);
do i=1 to memcount;
file = dread(did,i);
if scan(file,-1,'.') = "&extension" or "&extension" = "all" then do;
rc2=filename(filrfb,catx('/',directory,file));
fid=fopen(filrfb);
dte=input(finfo(fid,'Last Modified'),datetime.);
size=input(finfo(fid,'File Size (bytes)'),32.);
output;
rc3=fclose(fid);
rc4=filename(filrfb);
end;
end;
rc5=dclose(did);
run;
%mend getfilesizes;
%getfilesizes(directory=D:\SASworkdata\workdata,extension=all,dataset=xxx)
proc print width=min;
run;
1. Use {i} to post code
2. Does this work better?
%macro getfilesizes(directory, extension, dataset) ;
data &dataset. ;
length FILE $256 DTE 8 SIZE 8 DIRECTORY $256 FILRF FILRFB $8 ;
format DTE datetime.;
keep FILE DTE SIZE DIRECTORY ;
DIRECTORY= "&directory";
RC1 = filename(FILRF,DIRECTORY);
DID = dopen(FILRF);
MEMCOUNT = dnum(DID);
do I=1 to MEMCOUNT;
FILE = dread(DID,I);
if "&extension" = scan(FILE,-1,'.') or "&extension" = "all" then do;
RC2 =filename(FILRFB, catx('/', DIRECTORY, FILE) );
FID =fopen(FILRFB);
if FID then do;
DTE =input(finfo(FID,'Last Modified'),anydtdtm.);
SIZE=input(finfo(FID,'File Size (bytes)'),32.);
end;
else call missing(DTE,SIZE);
output;
RC3 =fclose(FID);
RC4 =filename(FILRFB);
end;
end;
RC5=dclose(DID); RC6=filename(FILRF);
run;
%mend getfilesizes;
%getfilesizes(directory=%sysfunc(pathname(WORK)), extension=all, dataset=XXX)
proc print width=min;
run;
Also add FID in the kept variables.
If FID=0 then you are not allowed to open the files, so can't read the date and size with this method.
There are other methods available if you have access to the system prompt.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.