Macros for SAS Application Developers
https://github.com/sasjs/core
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, provide a libds value as follows:
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  Which gives:
14 
15  > 2mb
16 
17  @param [in] fpath= Full path and filename. Provide this OR the libds value.
18  @param [in] libds= (0) Library.dataset value (assumes library is BASE engine)
19  @param [in] format= (NO) Set to yes to apply sizekmg. format
20 
21  @returns bytes
22 
23  @version 9.2
24  @author Allan Bowe
25 **/
26 
27 %macro mf_getfilesize(fpath=,libds=0,format=NO
28 )/*/STORE SOURCE*/;
29 
30  %local rc fid fref bytes dsid lib vnum;
31 
32  %if &libds ne 0 %then %do;
33  %let libds=%upcase(&libds);
34  %if %index(&libds,.)=0 %then %let lib=WORK;
35  %else %let lib=%scan(&libds,1,.);
36  %let dsid=%sysfunc(open(
37  sashelp.vtable(where=(libname="&lib" and memname="%scan(&libds,-1,.)")
38  keep=libname memname filesize
39  )
40  ));
41  %if (&dsid ^= 0) %then %do;
42  %let vnum=%sysfunc(varnum(&dsid,FILESIZE));
43  %let rc=%sysfunc(fetch(&dsid));
44  %let bytes=%sysfunc(getvarn(&dsid,&vnum));
45  %let rc= %sysfunc(close(&dsid));
46  %end;
47  %else %put &sysmacroname: &libds could not be opened! %sysfunc(sysmsg());
48  %end;
49  %else %do;
50  %let rc=%sysfunc(filename(fref,&fpath));
51  %let fid=%sysfunc(fopen(&fref));
52  %let bytes=%sysfunc(finfo(&fid,File Size (bytes)));
53  %let rc=%sysfunc(fclose(&fid));
54  %let rc=%sysfunc(filename(fref));
55  %end;
56 
57  %if &format=NO %then %do;
58  &bytes
59  %end;
60  %else %do;
61  %sysfunc(INPUTN(&bytes, best.),sizekmg.)
62  %end;
63 
64 %mend mf_getfilesize ;