Production Ready Macros for SAS Application Developers
mf_getfilesize.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns the size of a file in bytes.
4  @details Provide full path/filename.extension to the file, eg:
5 
6  %put %mf_getfilesize(fpath=C:\temp\myfile.txt);
7 
8  or
9 
10  data x;do x=1 to 100000;y=x;output;end;run;
11  %put %mf_getfilesize(libds=work.x,format=yes);
12 
13  gives:
14 
15  2mb
16 
17  @param fpath= full path and filename. Provide this OR the libds value.
18  @param libds= library.dataset value (assumes library is BASE engine)
19  @param format= set to yes to apply sizekmg. format
20  @returns bytes
21 
22  @version 9.2
23  @author Allan Bowe
24 **/
25 
26 %macro mf_getfilesize(fpath=,libds=0,format=NO
27 )/*/STORE SOURCE*/;
28 
29  %if &libds ne 0 %then %do;
30  %let fpath=%sysfunc(pathname(%scan(&libds,1,.)))/%scan(&libds,2,.).sas7bdat;
31  %end;
32 
33  %local rc fid fref bytes;
34  %let rc=%sysfunc(filename(fref,&fpath));
35  %let fid=%sysfunc(fopen(&fref));
36  %let bytes=%sysfunc(finfo(&fid,File Size (bytes)));
37  %let rc=%sysfunc(fclose(&fid));
38  %let rc=%sysfunc(filename(fref));
39 
40  %if &format=NO %then %do;
41  &bytes
42  %end;
43  %else %do;
44  %sysfunc(INPUTN(&bytes, best.),sizekmg.)
45  %end;
46 
47 %mend ;