BookmarkSubscribeRSS Feed
derekx
Calcite | Level 5

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;

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

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;

 

ChrisNZ
Tourmaline | Level 20

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. 

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 2 replies
  • 2883 views
  • 0 likes
  • 2 in conversation