FWRITE Function

Writes a record to an external file.

Category: External Files


FWRITE(file-id<,cc> )

Required Argument


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

Optional Argument


is a character constant, variable, or expression that specifies a carriage-control character:

blank starts the record on a new line.
0 skips one blank line before a new line.
- skips two blank lines before a new line.
1 starts the line on a new page.
+ overstrikes the line on a previous line.
P interprets the line as a computer prompt.
= interprets the line as carriage control information.
all else starts the line record on a new line.


FWRITE returns 0 if the operation was successful, ≠0 if it was not successful. FWRITE moves text from the File Data Buffer (FDB) to the external file. In order to use the carriage-control characters, you must open the file with a record format of P (print format) in FOPEN.
Note: When you use the update mode, you must execute FREAD before you execute FWRITE. You cannot write a new record in place of the current record if the new record has a length that is greater than the current record.


This example assigns the fileref MYFILE to an external file and attempts to open the file. If the file is opened successfully, it writes the numbers 1 to 50 to the external file, skipping two blank lines. Note that in a macro statement that you do not enclose character strings in quotation marks.
%let filrf=myfile;
%let rc=%sysfunc(filename(filrf,
%let fid=%sysfunc(fopen(&filrf,o,0,P));
%do i=1 %to 50;
   %let rc=%sysfunc(fput(&fid,
   %if (%sysfunc(fwrite(&fid,-)) ne 0) %then
     %put %sysfunc(sysmsg());
%let rc=%sysfunc(fclose(&fid));