Date: Mon, 30 Jan 2006 11:04:16 -0500
Reply-To: "Richard A. DeVenezia" <rdevenezia@WILDBLUE.NET>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Richard A. DeVenezia" <rdevenezia@WILDBLUE.NET>
Subject: Re: Random access with FOPEN(), FREAD(), FPOS() and FGET()
Friar Broccoli wrote:
> Hello World;
>
> After literally years of trying off and on, I finally figured
> out how to perform true random access file reading in SAS using
> FOPEN(), FREAD(), FPOS() and FGET().
>
> The following code (which has been modified slightly so I don't
> get fired for releasing confidential info.) reads a binary file.
> That file uses pointers at the beginning of each record to point
> to the offset of the beginning of the next record and so on till
> the end of the file. The code shown skips from record to record,
> collecting nBillNmb and pEmployee at each location.
>
> The following code works fine, but I am bothered by the fact
> that I need to open the file into a buffer that is much to large
> (here &ValTooBig), in order to be certain that I am reading the
> entire file. Is there some way of avoiding this uglyness, say
> by determining the file size somehow (I use windowsXP), or by
> using a smaller value of &ValTooBig and then using FREAD() to
> read just what is needed (plus a bit more and then backing up),
> or something else I have not thought of.
You might get cleaner gas mileage by using RECFM=N and the INPUT @ modifier.
Should be good for files upto 2GB in size.
------------------------
filename FOO TEMP;
* create an example datafile containing advancement pointers;
data _null_;
file foo recfm=N;
offset = 0;
do elements = 1 to 1000;
offsetToNextElement = 6 + floor(20 * ranuni(123));
put @offset offsetToNextElement pib2. @;
put elements pib2. @;
put 'ffff'x @;
offset = offset + offsetToNextElement;
end;
stop;
run;
* Read the simulated data that contains offset advancement directives;
data elements;
infile FOO recfm=N end=EOF;
offset = 0;
do until (EOF);
input
@offset
advance pib2.
element pib2.
twochar $char2.
;
output;
offset + advance;
end;
run;
------------------------
Richard A. DeVenezia
http://www.devenezia.com/