FPOS Function

Sets the position of the column pointer in the File Data Buffer (FDB).

Category: External Files

Syntax

FPOS(file-id,nval)

Required Arguments

file-id

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

nval

is a numeric constant, variable, or expression that specifies the column at which to set the pointer.

Details

FPOS returns 0 if the operation was successful, ≠0 if it was not successful. If you open a file in output mode and the specified position is past the end of the current record, the size of the record is increased appropriately. However, in a fixed block or VBA file, if you specify a column position beyond the end of the record, the record size does not change and the text string is not written to the file.
If you open a file in update mode and the specified position is not past the end of the current record, then SAS writes the record to the file. If the specified position is past the end of the current record, then SAS returns an error message and does not write the new record:
ERROR: Cannot increase record length in update mode.
Note: If you use the update mode with the FOPEN function, then you must execute FREAD before you execute FWRITE functions.

Example

This example assigns the fileref MYFILE to an external file and opens the file in update mode. If the file is opened successfully, indicated by a positive value in the variable FID, SAS reads a record and places data into the file's buffer at column 12. If the resulting record length is less than or equal to the original record length, then SAS writes the record and closes the file. If the resulting record length is greater than the original record length, then SAS writes an error message to the log.
%macro ptest;
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf,test.txt));
%let fid=%sysfunc(fopen(&filrf,o));
%let rc=%sysfunc(fread(&fid));
%put &fid;
%if (&fid > 0) %then
   %do;
      %let dataline=This is some data.;
         /* Position at column 12 in the FDB. */
      %let rc=%sysfunc(fpos(&fid,12));
      %put &rc one;
         /* Put the data in the FDB. */
      %let rc=%sysfunc(fput(&fid,&dataline));
      %put &rc two;
      %if (&rc ne 0) %then
         %do;
            %put %sysfunc(sysmsg());
         %end;
      %else %do; 
         /* Write the record. */
      %let rc=%sysfunc(fwrite(&fid));
      %if (&rc ne 0) %then
         %do;
            %put write fails &rc;
         %end;
      %end;
         /* Close the file. */
      %let rc=%sysfunc(fclose(&fid));
   %end;
%let rc=%sysfunc(filename(filrf));
%mend;
%ptest;
Output from the FPOS Function
1
0 one
0 two