DATA Step, Macro, Functions and more

Capture filesize of a file on Windows system

Reply
Occasional Contributor
Posts: 6

Capture filesize of a file on Windows system

Hi,

I am looking to find out how to pull a value for filesize from a file on Windows system via SAS program. I have this chunk of sas code below which I cant figure out how to use the value it generates beyond a "put" statement for the log. (This could be a bigger issue of my understanding of how to declare variables and easily reuse the values, which currently seems very counter intutive from my .net coding experience). But here is the code I am working with:

---------------------------------------------------------------

filename _in "C:\Somefile.ext";

data _null_; recfm=F end=last;

ctr is virtually gone once it puts...(caput??) I want to use this value... how do I do this? Seems like I should be able to do something like %LET filesize = ctr, but %LET apparently does not work like a declare Smiley Sad

If someone would please let me know how to declare a variable, assign it the "ctr" value from the data block calculation and then use this value attached to my variable later in the program this will be very helpful.

infile _in lrecl=1

input;

ctr+1;

if last then

put ctr;

run;

-------------------------------

Respected Advisor
Posts: 3,799

Re: Capture filesize of a file on Windows system

Posted in reply to bbpatterson

Here is one possibility using the "External File" functions.

data info(keep=infoSmiley Happy;

   length filename $8 infoname $32 infoval $128;

   do rc = filename(filename,'file path/name here') while(rc eq 0);

      do fid=fopen(filename) while(fid ne 0);

         do i = 1 to foptnum(fid);

            infoname=foptname(fid,i);

            infoval=finfo(fid,infoname);

            output;

            end;

         fid=fclose(fid);

         end;

      rc = filename(filename,'CLEAR');

      end;

   run;

proc print;

   run;

Super User
Posts: 10,035

Re: Capture filesize of a file on Windows system

Posted in reply to bbpatterson

If you can use pipe ability,then it will be easy.

filename fsize pipe 'dir c:\temp\';
data _null_ ;
 infile fsize ;
 input;
 put _infile_;
run;

Ksharp

Valued Guide
Posts: 2,177

Capture filesize of a file on Windows system

Posted in reply to bbpatterson

instead of

if last then

put ctr;

use

if last then

CALL SYMputX( 'CTR', ctr );

then later code will be able to use &ctr to refer to the value that was in data set variable CTR

Like

title "&ctr items in the report" ;

Frequent Contributor
Posts: 75

Capture filesize of a file on Windows system

Here is a command that we can use it for checking the file size in windows environment( through DOS prompt)

for %I in (testfile.txt) do @echo %~ZI

We can execute windows command inside SAS using 'X' Command statement or CALL SYSTEM routine.

Please try this.

Thanks

Dhanasekaran R

SAS Employee
Posts: 104

Capture filesize of a file on Windows system

Posted in reply to bbpatterson

The code you originally posted looks like it was designed to deterime the number of records in the text file, not the file size in bytes.  You'll have a bit of trouble with this code under windows for a couple of reasons:

1.  Your code specifies RECFM=F for the input file.  This indicates that the text file has fixed length records, i.e. every line in the text file is exactly the same number of characters long.  Windows doesn't produce fixed record files - windows text file are variable records length format (RECFM=V). So unless the file was created on another system (probably a mainframe) you should use RECFM=V instead of RECFM=F.

2.  The default input buffer size on Windows is 256 bytes.  If any individual record in the ext file is more than 256 bytes long, the record will be truncated in the input buffer. Specifying an input buffer size LARGER than the largest record has no ill effect other than consuming a few bytes more of memory.  You can specify the input buffer size (Logical RECord Length) with the LRECL= option.

3.  If, during a DATA step, you want to put a value into a macro variable for use in downstream program steps, you need a data step function like CALL SYMPUTX().

Try something like this:

%let File=C:\Somefile.ext;
filename _in "&file" lrecl=32767;
 
data _null_; 
   infile _in end=last;
   input;
   ctr+1;
   if last then call symputx('ctr',ctr);
run;

filename _in clear;

%PUT NOTE: &file contains &ctr records.;

Ask a Question
Discussion stats
  • 5 replies
  • 261 views
  • 1 like
  • 6 in conversation