FGET Function

Copies data from the File Data Buffer (FDB) into a variable.

Category: External Files

Syntax

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.

variable

in a DATA step, specifies a character variable to hold the data. In a macro, specifies a macro variable to hold the data. If variable is a macro variable and it does not exist, it is created.

Optional Argument

length

specifies the number of characters to retrieve from the FDB. If length is specified, only the specified number of characters is retrieved (or the number of characters remaining in the buffer if that number is less than length). If length is omitted, all characters in the FDB from the current column position to the next delimiter are returned. The default delimiter is a blank. The delimiter is not retrieved.

See The FSEP Function for more information about delimiters.

Details

FGET returns 0 if the operation was successful, or −1 if the end of the FDB was reached or no more tokens were available.
After FGET is executed, the column pointer moves to the next read position in the FDB.

Example

This example assigns the fileref MYFILE to an external file and attempts to open the file. If the file is opened successfully, it reads the first record into the File Data Buffer, retrieves the first token of the record and stores i t in the variable MYSTRING, and then closes the file. Note that in a macro statement that you do not enclose character strings in quotation marks.
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf,
   physical-filename));
%let fid=%sysfunc(fopen(&filrf));
%if &fid > 0 %then
   %do;
      %let rc=%sysfunc(fread(&fid));
      %let rc=%sysfunc(fget(&fid,mystring));
      %put &mystring;
      %let rc=%sysfunc(fclose(&fid));
   %end;
%let rc=%sysfunc(filename(filrf));