FREWIND Function

Positions the file pointer to the start of the file.

Category: External Files



Required Argument


is a numeric variable that specifies the identifier that was assigned when the file was opened, generally by the FOPEN function.


FREWIND returns 0 if the operation was successful, ≠0 if it was not successful. FREWIND has no effect on a file opened with sequential access.


This example assigns the fileref MYFILE to an external file. Then it opens the file and reads the records until the end of the file is reached. The FREWIND function then repositions the pointer to the beginning of the file. The first record is read again and stored in the File Data Buffer (FDB). The first token is retrieved and stored in the macro variable VAL:
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf,
%let fid=%sysfunc(fopen(&filrf));
%let rc=0;
%do %while (&rc ne −1);
   /* Read a record. */
   %let rc=%sysfunc(fread(&fid));
   /* Reposition pointer to beginning of file. */
%if &rc = −1 %then
    %let rc=%sysfunc(frewind(&fid));
       /* Read first record. */
    %let rc=%sysfunc(fread(&fid));
       /* Read first token  */
       /* into macro variable VAL. */
    %let rc=%sysfunc(fget(&fid,val));
    %put val=&val;
  %put Error on fread=%sysfunc(sysmsg());
%let rc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(filrf));