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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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