Specifies the current output file for PUT statements.
| Valid in: | DATA step |
|---|---|
| Categories: | CAS |
| File-Handling | |
| Type: | Executable |
| Restriction: | When SAS is in a locked-down state, the FILENAME statement is not available for files that are not in the locked-down path list. For more information, see SAS Processing Restrictions for Servers in a Locked-Down State in SAS Language Reference: Concepts. |
| See: | FILE Statement under Windows, UNIX, and z/OS |
Table of Contents
identifies an external file that the DATA step uses to write output from a PUT statement. File-specification can have these forms:
specifies the physical name of an external file, which is enclosed in quotation marks. The physical name is the name by which the operating environment recognizes the file.
specifies the fileref of an external file.
| Range | 1 to 8 bytes |
|---|---|
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
| Requirement | You must have associated fileref with an external file in a FILENAME statement or function in a previous step or in an appropriate operating environment command. The only way to assign fileref at run time is to use the FILEVAR= option in the FILE statement. |
| Note | If your fileref is associated with the EMAIL access method, additional options are available for the FILE statement. For a complete list of options, see FILENAME Statement: EMAIL (SMTP) Access Method. |
| See | FILENAME Statement |
specifies a fileref that is previously assigned to an external file that is an aggregate grouping of files. Follow the fileref with the name of a file or member, which is enclosed in parentheses.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Requirement | You must have associated fileref with an external file in a FILENAME statement or function in a previous step or in an appropriate operating environment command. |
| Operating environment | Different operating environments call an aggregate grouping of files by different names such as a directory, a MACLIB, or a partitioned data set. For more information, see the SAS documentation for your operating environment. |
| Note | A file that is located in an aggregate storage location and has a name that is not a valid SAS name must have its name enclosed in quotation marks. |
| See | FILENAME Statement |
is a reserved fileref that directs the output that is produced by any PUT statements to the SAS log.
At the beginning of each execution of a DATA step, the fileref that indicates where the PUT statements write is automatically set to LOG. Therefore, the first PUT statement in a DATA step always writes to the SAS log, unless the PUT statement is preceded by a FILE statement that specifies otherwise.
| Tip | Because output lines are by default written to the SAS log, use a FILE LOG statement to restore the default action or to specify additional FILE statement options. |
|---|
is a reserved fileref that directs the output that is produced by any PUT statements to the same file as the output that is produced by SAS procedures.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | When you write to a file, the value of the N= option must be either 1 or PAGESIZE. |
| Operating environment | The carriage-control characters that are written to a file can be specific to the operating environment. For more information, see the SAS documentation for your operating environment. |
| Tip | When PRINT is the fileref, SAS uses carriage-control characters and writes the output with the characteristics of a print file. |
| See | A complete discussion of print files in SAS Language Reference: Concepts |
| Tip | If the file does not exist in the directory that you specify for file-specification, SAS creates the file. If the directory that is specified in file-specification does not exist, SAS sets the SYSERR macro variable, which can be checked if the ERRORCHECK option is set to STRICT. |
|---|
specifies the type of device or the access method that is used if the fileref points to an input or output device or a location that is not a physical file.
specifies an access method that enables you to access an ActiveMQ messaging broker.
| Restriction | This device type is not supported in SAS Viya. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | FILENAME Statement: ACTIVEMQ Access Method in Application Messaging with SAS |
specifies the CATALOG access method.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | For a complete list of options that are available with the CATALOG access method, see FILENAME Statement: CATALOG Access Method. |
specifies the CLIPBOARD access method.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | For a complete list of options that are available with the CLIPBOARD access method, see FILENAME Statement: CLIPBOARD Access Method. |
specifies that the device is a disk drive.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Tip | When you assign a fileref to a file on disk, you are not required to specify DISK. |
specifies that the output to the file is discarded.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the FILEVAR= option is used in conjunction with DUMMY, no files are written to the names that are listed in the FILEVAR= option. |
| Tip | Specifying DUMMY can be useful for testing. |
specifies the FTP access method.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | For a complete list of options that are available with the FTP access method, see FILENAME Statement: FTP Access Method. |
| Example |
|
specifies the Hadoop access method.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | For a complete list of options that are available with the Hadoop access method, see FILENAME Statement: Hadoop Access Method. |
indicates that the output device type is a graphics device that receives graphics data.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|
specifies a Java Message Service (JMS) destination.
| Restriction | This device type is not supported in SAS Viya. |
|---|
specifies an unnamed pipe.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Operating environment | Some operating environments do not support pipes. |
specifies an unbuffered graphics output device.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|
specifies a printer or printer spool file.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|
specifies the SFTP access method.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | For a complete list of options that are available with the SFTP access method, see FILENAME Statement: SFTP Access Method. |
specifies the SOCKET access method.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | For a complete list of options that are available with the SOCKET access method, see FILENAME Statement: SOCKET Access Method. |
specifies a tape drive.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|
specifies the user's terminal.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|
specifies a Universal Printer definition name.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Tip | If you do not specify the printer name in the FILENAME statement, the PRINTERPATH options control which Universal Printer is used and the destination of the output. |
specifies the WEBDAV access method.
| Restriction | This argument is not supported in a DATA step that runs in CAS. |
|---|---|
| Interaction | If the DATA step does not recognize the access method option, the DATA step passes the option to the access method for handling. |
| See | For a complete list of options that are available with the WEBDAV access method, see FILENAME Statement: WebDAV Access Method. |
| Alias | DEVICE=device-type |
|---|---|
| Default | DISK |
| Requirement | device-type or DEVICE=device-type must immediately follow file-specification in the statement. |
| Operating environment | Additional specifications might be required when you specify some devices. See the SAS documentation for your operating environment before specifying a value other than DISK. Values in addition to the ones listed here might be available in some operating environments. |
specifies the block size of the output file.
| Default | Dependent on your operating environment. For more information, see the FILE Statement in the SAS documentation for your operating environment. |
|---|
specifies a variable that SAS automatically sets to the current column location of the pointer. This variable, like automatic variables, is not written to the data set.
| Alias | COL= |
|---|---|
| See | LINE= |
specifies an alternate delimiter (other than blank) to be used for LIST output, where delimiter can be one of these items:
specifies one or more characters to write as delimiters.
| Requirement | Enclose the list of characters in quotation marks. |
|---|
specifies a character variable whose value becomes the delimiter.
| Alias | DLM= |
|---|---|
| Default | blank space |
| Restriction | Even though a character string or character variable is accepted, only the first character of the string or variable is used as the output delimiter. The FILE DLM= processing differs from INFILE DELIMITER= processing. |
| Interaction | Output that contains embedded delimiters requires the delimiter sensitive data (DSD) option. |
| Tips | DELIMITER= can be used with the colon (:) modifier (modified LIST output). |
| The delimiter is case sensitive. | |
| See | DLMSTR= delimiter and DSD (delimiter sensitive data) |
specifies a parsing option for the DLMSTR=T option that removes trailing blanks of the string delimiter.
| Requirement | The DLMSOPT=T option has an effect only when used with the DLMSTR= option. |
|---|---|
| Tip | The DLMSOPT=T option is useful when you use a variable as the delimiter string. |
| See | DLMSTR= |
specifies a character string as an alternate delimiter (other than a blank) to be used for LIST output, where delimiter can be one of these items:
specifies a character string to write as a delimiter.
| Requirement | Enclose the string in quotation marks. |
|---|
specifies a character variable whose value becomes the delimiter.
| Default | blank space |
|---|---|
| Interactions | If you specify more than one DLMSTR= option in the FILE statement, the DLMSTR= option that is specified last is used. If you specify both the DELIMITER= and DLMSTR= options, the option that is specified last is used. |
| If you specify RECFM=N, ensure that the LRECL= option is large enough to hold the largest input item. Otherwise, it is possible for the delimiter to be split across the record boundary. | |
| See | DELIMITER=, DLMSOPT=, and DSD |
discards data items that exceed the output line length (as specified by the LINESIZE= option or LRECL= option in the FILE statement).
By default, data that exceeds the current line length is written on a new line. When you specify DROPOVER, SAS drops (or ignores) an entire item when there is not enough space in the current line to write it. When an entire item is dropped, the column pointer remains positioned after the last value that is written in the current line. Thus, the PUT statement might write other items in the current output line if they fit in the space that remains or if the column pointer is repositioned. When a data item is dropped, the DATA step continues normal execution (_ERROR_=0). At the end of the DATA step, a message is printed for each file from which data was lost.
| Default | FLOWOVER |
|---|---|
| Tip | Use DROPOVER when you want the DATA step to continue executing if the PUT statement attempts to write past the current line length, but you do not want the data item that exceeds the line length to be written on a new line. |
| See | FLOWOVER and STOPOVER |
specifies that data values that contain embedded delimiters, such as tabs or commas, be enclosed in quotation marks. The DSD option enables you to write data values that contain embedded delimiters to LIST output. This option is ignored for other types of output (for example, formatted, column, and named). Any double quotation marks that are included in the data value are repeated. When a variable value contains the delimiter and DSD is used in the FILE statement, the variable value is enclosed in double quotation marks when the output is generated. For example:
data _null_;
file log dsd;
x='"lions, tigers, and bears"';
put x ' "Oh, my!"';
run;
The program writes this line to the SAS log:
"""lions, tigers, and bears""", "Oh, my!"
If a quoted (text) string contains the delimiter and DSD is used in the FILE statement, then the quoted string is not enclosed in double quotation marks when used in a PUT statement. For example:
data _null_;
file log dsd;
put 'lions, tigers, and bears';
run;
The program writes this line to the SAS log:
lions, tigers, and bears
| Interaction | If you specify DSD, the default delimiter is assumed to be the comma (,). Specify the DELIMITER= option or DLMSTR= option if you want to use a different delimiter. |
|---|---|
| Tip | By default, data values that do not contain the delimiter that you specify are not enclosed in quotation marks. However, you can use the tilde (~) modifier to force any data value, including missing values, to be enclosed in quotation marks, even if the data value contains no embedded delimiter. |
| See | DELIMITER= and DLMSTR= |
specifies the encoding to use when writing to the output file. The value for ENCODING= indicates that the output file has a different encoding from the current session encoding.
When you write data to the output file, SAS transcodes the data from the session encoding to the specified encoding.
| Default | SAS uses the current session encoding. |
|---|---|
| See | Encoding Values in SAS Language Elements in SAS National Language Support (NLS): Reference Guide |
| Example | Specifying an Encoding When Writing to an Output File |
defines a character variable, whose name you supply, that SAS sets to the value of the physical name of the file currently open for PUT statement output. The physical name is how the operating environment recognizes the file.
| Tips | This variable, like automatic variables, is not written to the data set. |
|---|---|
| Use a LENGTH statement to make the variable length long enough to contain the value of the physical file name if the variable length is longer than eight bytes (the default length of a character variable). | |
| See | FILEVAR= |
| Example | Identifying the Current Output File |
defines a variable whose change in value causes the FILE statement to close the current output file and open a new one the next time the FILE statement executes. The next PUT statement that executes writes to the new file that is specified as the value of the FILEVAR= variable.
| Restriction | The value of a FILEVAR= variable is expressed as a character string that contains a physical file name. |
|---|---|
| Interaction | When you use the FILEVAR= option, the file-specification is just a placeholder, not an actual file name or a fileref that has been previously assigned to a file. SAS uses this placeholder for reporting processing information to the SAS log. The placeholder must conform to the same rules as a fileref. |
| Tips | This variable, like automatic variables, is not written to the data set. |
| If any of the physical file names are longer than eight bytes (the default length of a character variable), assign the FILEVAR= variable a longer length with another statement, such as a LENGTH statement or an INPUT statement. | |
| See | FILENAME= |
| Example | Dynamically Changing the Current Output File |
causes data that exceeds the current line length to be written on a new line. When a PUT statement attempts to write beyond the maximum allowed line length (as specified by the LINESIZE= option in the FILE statement), the current output line is written to the file and the data item that exceeds the current line length is written to a new line.
| Default | FLOWOVER |
|---|---|
| Interaction | If the PUT statement contains a trailing @, the pointer is positioned after the data item on the new line and the next PUT statement writes to that line. This process continues until the end of the input data is reached or until a PUT statement without a trailing @ causes the current line to be written to the file. |
| See | DROPOVER and STOPOVER |
controls whether currently defined footnotes are printed.
| Alias | FOOTNOTE | NOFOOTNOTE |
|---|---|
| Default | NOFOOTNOTES |
| Requirement | In order to print footnotes in a DATA step report, you must set the FOOTNOTE option in the FILE statement. |
defines a statement label that identifies a group of SAS statements that you want to execute each time SAS begins a new output page.
| Restrictions | The first statement after the label must be an executable statement. Thereafter, you can use any SAS statement. |
|---|---|
| Use the HEADER= option only when you write to print files and when you include the PRINT= option. | |
| Tip | To prevent the statements in this group from executing with each iteration of the DATA step, use two RETURN statements: one statement precedes the label and the other appears as the last statement in the group. |
| Example | Executing Statements When Beginning a New Page |
defines a variable whose value is the current relative line number within the group of lines available to the output pointer. You supply the variable name and SAS automatically assigns the value.
| Range | 1 to the value that is specified by the N= option or with the #n line pointer control. If neither value is specified, the LINE= variable has a value of 1. |
|---|---|
| Tips | This variable, like automatic variables, is not written to the data set. |
| The value of the LINE= variable is set at the end of PUT statement execution to the number of the next available line. |
sets the maximum number of columns per line for reports and the maximum record length for data files.
| Alias | LS= |
|---|---|
| Default | The default LINESIZE= value is determined by one of two options: 1) the LINESIZE= system option when you write to a file that contains carriage-control characters or to the SAS log or 2) the LRECL= option in the FILE statement when you write to a file. |
| Range | From 64 to the maximum logical record length that is allowed in your operating environment. |
| Interaction | If a PUT statement tries to write a line that is longer than the value that is specified by the LINESIZE= option, the action that is taken is determined by whether FLOWOVER, DROPOVER, or STOPOVER is in effect. By default (FLOWOVER), SAS writes the line as two or more separate records. |
| Operating environment | The highest value allowed for LINESIZE= is dependent on your operating environment. For more information, see the SAS documentation for your operating environment. |
| Note | LINESIZE= tells SAS how much of the line to use. LRECL= specifies the physical record length of the file. |
| See | LRECL=, DROPOVER, FLOWOVER, and STOPOVER |
| Example | When the Output Line Exceeds the Line Length of the Output File |
defines a variable whose value is the number of lines left on the current page. You supply the variable name and SAS assigns the value of the number of lines left on the current page to that variable. The value of the LINESLEFT= variable is set at the end of PUT statement execution.
| Alias | LL= |
|---|---|
| Tip | This variable, like automatic variables, is not written to the data set. |
| Example | Determining New Page by Lines Left on the Current Page |
specifies the logical record length of the output file.
| Default | If you omit the LRECL= option, SAS chooses a value based on the operating environment's file characteristics. |
|---|---|
| Interaction | Alternatively, you can specify a global logical record length by using the LRECL system option. In SAS 9.4, the default value for the global LRECL system option is 32767. If you are using fixed-length records (RECFM=F), the default value for LRECL is 256. |
| Operating environment | Values for logical-record-length are dependent on the operating environment. For more information, see the SAS documentation for your operating environment. |
| Note | LINESIZE= tells SAS how much of the line to use; LRECL= specifies the physical line length of the file. |
| See | LINESIZE=, PAD, and PAGESIZE= |
specifies a file to open. When a directory fileref or physical name is provided in the FILE statement, the file that is referred to in the MEMVAR= variable is opened. When the value of the MEMVAR= variable changes, the current file is closed and the new file is opened.
| Restriction | The value of a MEMVAR= variable is expressed as a character string that contains a physical file name. |
|---|---|
| Tips | This variable, like automatic variables, is not written to the data set. |
| If any of the physical file names are longer than eight bytes (the default length of a character variable), assign the MEMVAR= variable a longer length with another statement, such as a LENGTH statement or an INPUT statement. | |
| See | FILENAME= |
writes the output lines after any existing lines in the file.
| Default | OLD |
|---|---|
| Restrictions | MOD is not accepted under all operating environments. For more information, see the SAS documentation for your operating environment. |
| Do not use the MOD option with any ODS destination other than the Listing destination. Otherwise, you might receive unexpected output. | |
| See | OLD |
specifies the number of lines that you want available to the output pointer in the current iteration of the DATA step. Available-lines can be expressed as a number (n) or as the keyword PAGESIZE or PS.
specifies the number of lines that are available to the output pointer. The system can move back and forth between the number of lines that are specified while composing them before moving on to the next set.
specifies that the entire page is available to the output pointer.
| Alias | PS |
|---|---|
| Restrictions | N=PAGESIZE is valid only when output is printed. |
| If the current output file is a file that is to be printed, available-lines must have a value of either 1 or PAGESIZE. | |
| Interactions | In addition to using in the N= option to control the number of lines available to the output pointer, you can also use the #n line pointer control in a PUT statement. |
| If you omit the N= option and no # pointer controls are used, one line is available. That is, by default, N=1. If N= is not used but there are # pointer controls, N= is assigned the highest value that is specified for a # pointer control in any PUT statement in the current DATA step. | |
| Tip | Setting N=PAGESIZE enables you to compose a page of multiple columns one column at a time. |
| Example | Arranging the Contents of an Entire Page |
specifies to use the Output Delivery System to format the output from a DATA step. This option defines the structure of the data component and holds the results of the DATA step and binds that component to a table definition to produce an output object. ODS sends this object to all open ODS destinations, each of which formats the output appropriately. For information about the ODS-suboptions and the Output Delivery System, see FILE Statement: ODS in SAS Output Delivery System: User’s Guide.
| Default | If you omit the ODS suboptions, the DATA step uses a default table definition (base.datastep.table) that is stored in the SASHELP.TMPLMST template store. This definition defines two generic columns: one column for character variables and one column for numeric variables. ODS associates each variable in the DATA step with one of these columns and displays the variables in the order in which they are defined in the DATA step. |
|---|---|
| Without suboptions, the default table definition uses the variable's label as its column heading. If no label exists, the definition uses the variable's name as the column heading. | |
| Restriction | You cannot use _FILE_=, FILEVAR=, HEADER=, or PAD with the ODS option. |
| Requirement | The ODS option is valid only when you use the fileref PRINT in the FILE statement. |
| Interaction | The DELIMITER= and DSD options have no effect on the ODS option. The FOOTNOTES|NOFOOTNOTES, LINESIZE, PAGESIZE, and TITLES | NOTITLES options have an effect only on the LISTING destination. |
replaces the previous contents of the file.
| Default | OLD |
|---|---|
| Restriction | OLD is not accepted under all operating environments. For more information, see the SAS documentation for your operating environment. |
| See | MOD |
controls whether records written to an external file are padded with blanks to the length that is specified in the LRECL= option.
| Default | NOPAD is the default when writing to a variable-length file; PAD is the default when writing to a fixed-length file. |
|---|---|
| Tip | PAD provides a quick way to create fixed-length records in a variable-length file. |
| See | LRECL= |
sets the number of lines per page for your reports.
| Alias | PS= |
|---|---|
| Default | The value of the PAGESIZE= system option |
| Range | The value can range from 15 to 32767. |
| Interaction | If any TITLE statements are currently defined, the lines that they occupy are included in counting the number of lines for each page. |
| Tips | After the value of the PAGESIZE= option is reached, the output pointer advances to line 1 of a new page. |
| If you specify FILE LOG, the number of lines that are written on the first page is reduced by the number of lines in the SAS start-up notes. For example, if PAGESIZE=20 and there are nine lines of SAS start-up notes, only 11 lines are available for output on the first page. | |
| See | PAGESIZE= System Option in SAS System Options: Reference |
controls whether carriage-control characters are placed in the output lines.
| Restriction | When you write to a file, the value of the N= option must be either 1 or PAGESIZE. |
|---|---|
| Operating environment | The carriage-control characters that are written to a file can be specific to the operating environment. For more information, see the SAS documentation for your operating environment. |
| Tips | The PRINT option is not necessary if you are using fileref PRINT. |
| If you specify FILE PRINT in an interactive SAS session, then the Output window interprets the form-feed control characters as page breaks, and blank lines that are written before the form feed are removed from the output. Writing the results from the Output window to a flat file produces a file without page break characters. If a file needs to contain the form-feed characters, then the FILE statement should include a physical file location and the PRINT option. |
specifies the record format of the output file.
| Range | Values are dependent on the operating environment. For more information, see the SAS documentation for your operating environment. |
|---|---|
| Interaction | In SAS 9.4, the default value for the global LRECL system option is 32767. If you are using fixed-length records (RECFM=F), the default value for LRECL is 256. |
stops processing the DATA step immediately if a PUT statement attempts to write a data item that exceeds the current line length. In such a case, SAS discards the data item that exceeds the current line length, writes the portion of the line that was built before the error occurred, and issues an error message.
| Default | FLOWOVER |
|---|---|
| See | FLOWOVER and DROPOVER |
controls the printing of the current title lines on the pages of files. When NOTITLES is omitted or when TITLES is specified, SAS prints any titles that are currently defined.
| Alias | TITLE | NOTITLE |
|---|---|
| Default | TITLES |
names a character variable that references the current output buffer of this FILE statement. You can use the variable in the same way as any other variable, even as the target of an assignment. The variable is automatically retained and initialized to blanks. Like automatic variables, the _FILE_= variable is not written to the data set.
| Restriction | variable cannot be a previously defined variable. Ensure that the _FILE_= specification is the first occurrence of this variable in the DATA step. Do not set or change the length of _FILE_= variable with the LENGTH statement or ATTRIB statement. However, you can attach a format to this variable with the ATTRIB statement or FORMAT statement. |
|---|---|
| Interaction | The maximum length of this character variable is the logical record length (LRECL) for the specified FILE statement. However, SAS does not open the file to know the LRECL until before the execution phase. Therefore, the designated size for this variable during the compilation phase is 32767 bytes. |
| Tips | Modification of this variable directly modifies the FILE statement's current output buffer. Any subsequent PUT statement for this FILE statement writes the contents of the modified buffer. The _FILE_= variable accesses only the current output buffer of the specified FILE statement even if you use the N= option to specify multiple output buffers. |
| To access the contents of the output buffer in another statement without using the _FILE_= option, use the automatic variable _FILE_. | |
| See | Updating the _FILE_ Variable |
For descriptions of operating-environment-specific options in the FILE statement, see the SAS documentation for your operating environment.
By default, PUT statement output is written to the SAS log. Use the FILE statement to route this output to either the same external file to which procedure output is written or to a different external file. You can indicate whether carriage-control characters should be added to the file. See PRINT | NOPRINT option.
You can use the FILE statement in conditional (IF-THEN) processing because it is executable. You can also use multiple FILE statements to write to more than one external file in a single DATA step.
You can use the Output Delivery System with the FILE statement to write DATA step results. For more information, see FILE Statement: ODS in SAS Output Delivery System: User’s Guide.
You can use the FILE statement with the INFILE and PUT statements to update an external file in place, updating either an entire record or only selected fields within a record. Follow these guidelines:
In addition to the _FILE_= variable, you can use the automatic _FILE_ variable to reference the contents of the current output buffer for the most recent execution of the FILE statement. This character variable is automatically retained and initialized to blanks. Like other automatic variables, _FILE_ is not written to the data set.
When you specify the _FILE_= option in a FILE statement, this variable is also indirectly referenced by the automatic _FILE_ variable. If the automatic _FILE_ variable is present and you omit _FILE_= in a particular FILE statement, SAS creates an internal _FILE_= variable for that FILE statement. Otherwise, SAS does not create the _FILE_= variable for a particular FILE.
During execution and at the point of reference, the maximum length of this character variable is the maximum length of the current _FILE_ variable. However, because _FILE_ references only other variables whose lengths are not known until before the execution phase, the designated length is 32,767 bytes during the compilation phase. For example, if you assign _FILE_ to a new variable whose length is undefined, the default length of the new variable is 32,767 bytes. You cannot use the LENGTH statement and the ATTRIB statement to set or override the length of _FILE_. You can use the FORMAT statement and the ATTRIB statement to assign a format to _FILE_.
Like other SAS variables, you can update the _FILE_ variable. These two methods are available:
You can update the _FILE_ variable by using an assignment statement that has this form:
The assignment statement updates the contents of the current output buffer and sets the buffer length to the length of 'string-in-quotation-marks' or character-expression. However, using an assignment statement does not affect the current column pointer of the PUT statement. The next PUT statement for this FILE statement begins to update the buffer at column 1 or at the last known location when you use the trailing @ in the PUT statement.
In this example, the assignment statement updates the contents of the current output buffer. The column pointer of the PUT statement is not affected.
file print;
_file_ = '_FILE_';
put 'This is PUT';
SAS creates this output: This
is PUT
In this example, the PUT statement updates the contents of the current output buffer beginning at column 14.
file print;
_file_ = 'This is from FILE, sir.';
put @14 'both';
SAS creates this output: This
is from both, sir.
You can also update the _FILE_ variable by using a PUT statement. The PUT statement updates the _FILE_ variable because the PUT statement formats data in the output buffer and _FILE_ points to that buffer. However, by default SAS clears the output buffers after a PUT statement executes and writes the current record (or N= block of records). Therefore, if you want to examine or further modify the contents of _FILE_ before it is written, include a trailing @ or @@ in any PUT statement (when N=1). For other values of N=, use a trailing @ or @@ in any PUT statement where the last line pointer location is on the last record of the record block.
In this example, when
N=1, the first PUT statement writes out Something and
the trailing @ holds the line and sets _FILE_ to Something.
Y is then assigned Something is here.
file ABC;
put 'Something' @;
Y = _file_||' is here';
file ABC;
put 'Nothing';
Y = _file_||' is here';
In the second FILE statement,
the PUT statement writes out Nothing and
the current output buffer is cleared. Because there is no trailing
@ on the second PUT statement, Y is assigned is here.
Any modification of _FILE_ directly modifies the current output buffer for the current FILE statement. The execution of any subsequent PUT statements for this FILE statement writes the contents of the modified buffer.
_FILE_ accesses only the contents of the current output buffer for a FILE statement, even when you use the N= option to specify multiple buffers. You can access all the N= buffers, but you must use a PUT statement with the # line pointer control to make the desired buffer the current output buffer.
This DATA step illustrates how to use the HEADER= option.
data _null_;
set sprint;
by dept;
file print header=newpage;
if first.dept then put _page_;
put @22 salesrep @34 salesamt;
return;
newpage:
put @20 'Sales for 1989' /
@20 dept=;
return;
run;
This DATA step demonstrates using the LINESLEFT= option to determine where the page break should occur, according to the number of lines left on the current page.
data _null_;
set info;
file print linesleft=remain pagesize=20;
put @5 name @30 phone
@35 bldg @37 room;
if remain<7 then put _page_ ;
run;
This example shows how to use N=PAGESIZE in a DATA step to produce a two-column telephone book listing. Each column contains a name and a phone number.
data _null_;
file 'external-file' print n=pagesize;
do col=1, 40;
do line=1 to 20;
set info;
put #line @col name $20. +1 phone 4.;
end;
end;
put _page_;
run;
This DATA step causes a file identification message to be printed in the SAS log and assigns the value of the current output file to the variable MYOUT. The PUT statement, demonstrating the assignment of the proper value to MYOUT, writes the value of that variable to the output file.
data _null_;
length myout $ 200;
file file-specification filename=myout;
put myout=;
stop;
run;
The PUT statement writes a line to the current output file that contains the physical name of the file.
MYOUT=your-output-file
This DATA step uses the FILEVAR= option to dynamically change the currently opened output file to a new physical file.
data _null_;
length name $ 200;
input name $;
file file-specification filevar=name mod;
date = date();
put 'records updated ' date date.;
datalines;
external-file-1
external-file-2
external-file-3
;
Because the combined lengths of the variables are longer than the output line (80 characters), this PUT statement automatically writes three separate records.
file file-specification linesize=80;
put name $ 1-50 city $ 51-90 state $ 91-104;
The value of NAME appears in the first record, CITY begins in the first column of the second record, and STATE in the first column of the third record.
This example shows reading raw data from a file through a TCP/IP socket. The NBYTE= option is used in the INFILE statement.
/* Start this first as the server */
filename serve socket ':5205' server
recfm=s
lrecl=25 blocksize=2500;
data _null_;
nb=25;
infile serve nbyte=nb;
input text $char25.;
put _all_;
run;
This example shows writing text to a file through a TCP/IP socket.
/* While the server test is running,*/
/*continue with this as the client. */
filename client socket "&hstname:5205"
recfm=s
lrecl=25 blocksize=2500;
data _null_;
file client;
put 'Some text to length 25...';
run;
This example creates an external file from a SAS data set. The current session encoding is Wlatin1, but the external file's encoding needs to be UTF-8. By default, SAS writes the external file using the current session encoding.
To tell SAS what encoding to use when writing data to the external file, specify the ENCODING= option. When you tell SAS that the external file is to be in UTF-8 encoding, SAS then transcodes the data from Wlatin1 to the specified UTF-8 encoding when writing to the external file.
libname myfiles 'SAS-library';
filename outfile 'external-file';
data _null_;
set myfiles.cars;
file outfile encoding="utf-8";
put Make Model Year;
run;
This example uses the FTP access method and the FILEVAR option to write data to several Microsoft Excel worksheets.
data _null_;
do i = 1 to 3;
sheet = cats('excel|[test-sheet.xlsx]Sheet', i, '!r1c1:r10c2');
file area ftp filevar=sheet;
do x = 1 to 10;
y = 2*x;
put x y;
end;
end;
run;