FILE Statement


Specifies the current output file for PUT statements

Valid: in a DATA step

Category: File-handling

Type: Executable

See: FILE Statement in the documentation for your operating environment.

Syntax

FILE file-specification < options > < operating-environment-options >;

Arguments

file-specification

  • identifies an external file that the DATA step uses to write output from a PUT statement. File-specification can have these forms:

    • external-file

      • 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.

    • fileref

      • specifies the fileref of an external file.

      • Requirement: You must have previously associated fileref with an external file in a FILENAME statement or function, or in an appropriate operating environment command. There is only one exception to this rule: when you use the FILEVAR= option, the fileref is simply a placeholder.

      • See Also: FILENAME Statement on page 1169

    • fileref(file)

      • 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.

      • Requirement: You must previously associate fileref with an external file in a FILENAME statement or function, or in an appropriate operating environment command.

      • See Also: FILENAME Statement on page 1169

      • Operating Environment Information: Different operating environments call an aggregate grouping of files by different names , such as a directory, a MACLIB, or a partitioned data set. For details, see the SAS documentation for your operating environment.

    • LOG

      • 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 it 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.

    • PRINT

      • 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.

      • Interaction: When you write to a file, the value of the N= option must be either 1 or PAGESIZE.

      • Tip: When PRINT is the fileref, SAS uses carriage control characters and writes the output with the characteristics of a print file.

      • See Also: A complete discussion of print files in SAS Language Reference: Concepts

      • Operating Environment Information: The carriage control characters that are written to a file can be specific to the operating environment. For details, see the SAS documentation for your operating environment.

Options

BLKSIZE= block- size

  • specifies the block size of the output file.

  • Default: Dependent on your operating environment.

    Operating Environment Information: For details, see the SAS documentation for your operating environment.

COLUMN= variable

  • 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=

DELIMITER = string-in-quotation-marks character-variable

  • specifies an alternate delimiter (other than blank) to be used for LIST output. This option is ignored for other types of output (formatted, column, named).

  • 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. This differs from INFILE DELIMITER= processing.

  • Interaction: Output that contains embedded delimiters requires the delimiter sensitive data (DSD) option.

  • Tip: DELIMITER= can be used with the colon (:) modifier (modified LIST output).

  • See Also: DSD (delimiter sensitive data) option on page 1157

DROPOVER

  • discards data items that exceed the output line length (as specified by the LINESIZE= or LRECL= options in the FILE statement).

  • Default: FLOWOVER

  • Explanation: 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 this occurs, 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.

  • 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 Also: FLOWOVER on page 1159 and STOPOVER on page 1162

DSD (delimiter sensitive data)

  • 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 (formatted, column, named). Any double quotation marks that are included in the data value are promoted (repeated). When a variable value contains the delimiter and DSD is used in the FILE statement, the variable value will be enclosed in double quotation marks when the output is generated. For example, the following code

     DATA _NULL_;    FILE log dsd;    x='"lions, tigers, and bears"';    put x ' "Oh, my!"';  run; 
  • will result in the following output:

     """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 will not be enclosed in double quotation marks when used in a PUT statement. For example, the following code

     DATA _NULL_;    FILE log dsd;    PUT 'lions, tigers, and bears';  run; 
  • will result in the following output:

     lions, tigers, and bears 
  • Interaction: If you specify DSD, the default delimiter is assumed to be the comma (,). Specify the DELIMITER= 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 it contains no embedded delimiter.

  • See Also: DELIMITER= on page 1157

ENCODING= encoding-value

  • 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.

    For valid encoding values, see Encoding Values in SAS Language Elements in SAS National Language Support (NLS): User s Guide .

  • Default: SAS uses the current session encoding.

  • Featured in: Example 8 on page 1168

FILENAME= variable

  • 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 the name by which the operating environment recognizes the file.

  • Tip: This variable, like automatic variables, is not written to the data set.

  • Tip: Use a LENGTH statement to make the variable length long enough to contain the value of the physical filename if it is longer than eight characters (the default length of a character variable).

  • See Also: FILEVAR= on page 1158

  • Featured in: Example 4 on page 1167

FILEVAR= variable

  • 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 filename.

  • Interaction: When you use the FILEVAR= option, the file-specification is just a placeholder, not an actual filename or a fileref that has been previously-assigned to a file. SAS uses this placeholder for reporting processing information to the SAS log. It must conform to the same rules as a fileref.

  • Tip: This variable, like automatic variables, is not written to the data set.

  • Tip: If any of the physical filenames is longer than eight characters (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 Also: FILENAME= on page 1158

  • Featured in: Example 5 on page 1167

FLOWOVER

  • 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 Also: DROPOVER on page 1157 and STOPOVER on page 1162

FOOTNOTES NOFOOTNOTES

  • controls whether currently defined footnotes are printed.

  • Alias: FOOTNOTE NOFOOTNOTE

  • Requirement: In order to print footnotes in a DATA step report, you must set the FOOTNOTE option in the FILE statement.

  • Default: NOFOOTNOTES

HEADER= label

  • defines a statement label that identifies a group of SAS statements that you want to execute each time SAS begins a new output page.

  • Restriction: The first statement after the label must be an executable statement. Thereafter you can use any SAS statement.

  • Restriction: Use the HEADER= option only when you write to print files.

  • Tip: To prevent the statements in this group from executing with each iteration of the DATA step, use two RETURN statements: one precedes the label and the other appears as the last statement in the group.

  • Featured in: Example 1 on page 1165

LINE= variable

  • 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; 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 is specified, the LINE= variable has a value of 1.

  • Tip: This variable, like automatic variables, is not written to the data set.

  • Tip: The value of the LINE= variable is set at the end of PUT statement execution to the number of the next available line.

LINESIZE= line-size

  • 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:

    • the LINESIZE= system option when you write to a file that contains carriage control characters or to the SAS log.

    • 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 for a specific file in your operating environment. For details, see the SAS documentation for your operating environment.

    Operating Environment Information: The highest value allowed for LINESIZE= is dependent on 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.

  • Comparisons: LINESIZE= tells SAS how much of the line to use. LRECL= specifies the physical record length of the file.

  • See Also: LRECL= on page 1160, DROPOVER on page 1157, FLOWOVER on page 1159, and STOPOVER on page 1162

  • Featured in: Example 6 on page 1168

LINESLEFT= variable

  • defines a variable whose value is the number of lines left on the current page. You supply the variable name; SAS assigns that variable the value of the number of lines left on the current page. 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.

  • Featured in: Example 2 on page 1166

LRECL= logical-record-length

  • specifies the logical record length of the output file.

  • Operating Environment Information: Values for logical-record-length are dependent on the operating environment. For details, see the SAS documentation for your operating environment.

  • Default: If you omit the LRECL= option, SAS chooses a value based on the operating environment s file characteristics.

  • Comparisons: LINESIZE= tells SAS how much of the line to use; LRECL= specifies the physical line length of the file.

  • See Also: LINESIZE= on page 1159, PAD on page 1161, and PAGESIZE= on page 1162

MOD

  • writes the output lines after any existing lines in the file.

  • Default: OLD

  • Restriction: MOD is not accepted under all operating environments.

    Operating Environment Information: For details, see the SAS documentation for your operating environment.

  • See Also: OLD on page 1161

N= available-lines

  • 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.

    • n

      • 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.

    • PAGESIZE

      • specifies that the entire page is available to the output pointer.

      • Alias: PS

      • Restriction: N=PAGESIZE is valid only when output is printed.

      • Restriction: 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: There are two ways to control the number of lines available to the output pointer:

        • the N= option

        • the # n line pointer control in a PUT statement.

      • Interaction: 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.

      • Featured in: Example 3 on page 1166

ODS < = ( ODS-suboptions ) >

  • specifies to use the Output Delivery System to format the output from a DATA step. It 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 , see the FILE Statement for ODS . For general information about the Output Delivery System, see 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 for character variables, and one 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 header. If no label exists, the definition uses the variable s name as the column header.

  • Requirement: The ODS option is valid only when you use the fileref PRINT in the FILE statement.

  • Restriction: You cannot use _FILE_=, FILEVAR=, HEADER=, and PAD with the ODS option.

  • Interaction: The DELIMITER= and DSD options have no effect on the ODS option. The FOOTNOTESNOFOOTNOTES, LINESIZE, PAGESIZE, and TITLES NOTITLES options have an effect only on the LISTING destination.

OLD

  • replaces the previous contents of the file.

  • Default: OLD

  • Restriction: OLD is not accepted under all operating environments.

    Operating Environment Information: For details, see the SAS documentation for your operating environment.

  • See Also: MOD on page 1160

PAD NOPAD

  • 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 Also: LRECL= on page 1160

PAGESIZE= value

  • sets the number of lines per page for your reports.

  • Alias: PS=

  • Default: the value of the PAGESIZE= system option.

  • Range: The value may range from 15 to 32767.

  • Interaction: If any TITLE statements are currently defined, the lines they occupy are included in counting the number of lines for each page.

  • Tip: After the value of the PAGESIZE= option is reached, the output pointer advances to line 1 of a new page.

  • See Also: PAGESIZE= System Option on page 1580

PRINT NOPRINT

  • controls whether carriage control characters are placed in the output lines.

  • Operating Environment Information: The carriage control characters that are written to a file can be specific to the operating environment. For details, see the SAS documentation for your operating environment.

  • Restriction: When you write to a file, the value of the N= option must be either 1 or PAGESIZE.

  • Tip: The PRINT option is not necessary if you are using fileref PRINT.

RECFM= record-format

  • specifies the record format of the output file.

  • Range: Values are dependent on the operating environment.

    Operating Environment Information: For details, see the SAS documentation for your operating environment.

STOPOVER

  • 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 Also: FLOWOVER on page 1159 and DROPOVER on page 1157

TITLES NOTITLES

  • 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

_FILE_= variable

  • 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. Make sure 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 or ATTRIB statements. However, you can attach a format to this variable with the ATTRIB 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 prior to the execution phase. Therefore, the designated size for this variable during the compilation phase is 32,767.

  • Tip: Modification of this variable directly modifies the FILE statement s current output buffer. Any subsequent PUT statement for this FILE statement outputs 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.

  • Tip: To access the contents of the output buffer in another statement without using the _FILE_= option, use the automatic variable _FILE_.

  • Main Discussion: Updating the _FILE_ Variable on page 1164

Operating Environment Options

Operating Environment Information: For descriptions of operating- environment-specific options on the FILE statement, see the SAS documentation for your operating environment.

Details

Overview 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 or not carriage control characters should be added to the file. See the PRINT NOPRINT option on page 1162.

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.

Operating Environment Information: Using the FILE statement requires operating-environment-specific information. See the SAS documentation for your operating environment before you use this statement.

You can now use the Output Delivery System with the FILE statement to write DATA step results. This functionality is briefly discussed here. For details, see the FILE Statement for ODS in SAS Output Delivery System: User s Guide .

Updating an External File in Place 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:

  • Always place the INFILE statement first.

  • Specify the same fileref or physical filename in the INFILE and FILE statements.

  • Use options that are common to both the INFILE and FILE statements in the INFILE statement. (Any such options that are used in the FILE statement are ignored.)

  • Use the SHAREBUFFERS option in the INFILE statement to allow the INFILE and FILE statements to use the same buffer, which saves CPU time and enables you to update individual fields instead of entire records.

Accessing the Contents of the Output Buffer 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, then 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_ merely references other variables whose lengths are not known until prior to the execution phase, the designated length is 32,767 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. You can not 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_.

Updating the _FILE_ Variable Like other SAS variables, you can update the _FILE_ variable. The following two methods are available:

  • Use _FILE_ in an assignment statement.

  • Use a PUT statement.

You can update the _FILE_ variable by using an assignment statement that has the following form.

_FILE_ = < string-in-quotation-marks character-expression >

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, this 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 the following 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 the following output:

  This is PUT  

In this example,

 file print;  _file_ = 'This is from FILE, sir.';  put @14 'both'; 

SAS creates the following 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 outputs the current record (or N= block of records). Therefore, if you want to examine or further modify the contents of _FILE_ before it is output, 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 the following example, when N=1

 file ABC;  put 'Something' @;  Y = _file_' is here';  file ABC;  put 'Nothing' ;  Y = _file_' is here'; 

Y is first assigned Something is here then 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 will output the contents of the modified buffer.

_FILE_ only accesses 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.

Comparisons

  • The FILE statement specifies the output file for PUT statements. The INFILE statement specifies the input file for INPUT statements.

  • Both the FILE and INFILE statements allow you to use options that provide SAS with additional information about the external file being used.

  • In the Program Editor, Log, and Output windows , the FILE command specifies an external file and writes the contents of the window to the file.

Examples

Example 1: Executing Statements When Beginning a New Page

This DATA step illustrates how to use the HEADER= option:

  • Write a report. Use DATA _NULL_ to write a report rather than create a data set.

     data _null_;     set sprint;     by dept; 
  • Route output to the SAS output window. Point to the header information. The PRINT fileref routes output to the same location as procedure output. HEADER= points to the label that precedes the statements that create the header for each page:

     file print header=newpage; 
  • Start a new page for each department:

     if first.dept then put _page_;     put @22 salesrep @34 salesamt; 
  • Write a header on each page. These statements execute each time a new page is begun. RETURN is necessary before the label and as the final statement in a labeled group:

     return;     newpage:        put @20 'Sales for 1989' /            @20 dept=;        return;  run; 

Example 2: Determining New Page by Lines Left on the Current Page

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.

  • Write a report. Use DATA _NULL_ to write a report rather than create a data set:

     data _null_;     set info; 
  • Route output to the standard SAS output window. The PRINT fileref routes output to the same location as procedure output. LINESLEFT indicates that the variable REMAIN contains the number of lines left on the current page:

     file print linesleft=remain pagesize=20;     put @5 name @30 phone         @35 bldg @37 room; 
  • Begin a new page when there are fewer than 7 lines left on the current page. Under this condition, PUT _PAGE_ begins a new page and positions the pointer at line 1:

     if remain<7 then put _page_ ;  run; 

Example 3: Arranging the Contents of an Entire Page

This example shows how to use N=PAGESIZE in a DATA step to produce a two-column telephone book listing, each column containing a name and a phone number:

  • Create a report and write it to a SAS output window. Use DATA _NULL_ to write a report rather than create a data set. PRINT is the fileref. SAS uses carriage control characters to write the output with the characteristics of a print file. N=PAGESIZE makes the entire page available to the output pointer:

     data _null_;     file '  external-file  ' print n=pagesize; 
  • Specify the columns for the report. This DO loop iterates twice on each DATA step iteration. The COL value is 1 on the first iteration and 40 on the second:

     do col=1, 40; 
  • Write 20 lines of data. This DO loop iterates 20 times to write 20 lines in column 1. When finished, the outer loop sets COL equal to 40, and this DO loop iterates 20 times again, writing 20 lines of data in the second column. The values of LINE and COL, which are set and incremented by the DO statements, control where the PUT statement writes the values of NAME and PHONE on the page:

     do line=1 to 20;     set info;     put #line @col name . +1 phone 4.;  end; 
  • After composing two columns of data, write the page. This END statement ends the outer DO loop. The PUT _PAGE_ writes the current page and moves the pointer to the top of a new page:

     end;     put _page_;  run; 

Example 4: Identifying the Current Output File

This DATA step causes a file identification message to print in the 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  

Example 5: Dynamically Changing the Current Output File

This DATA step uses the FILEVAR= option to dynamically change the currently opened output file to a new physical file.

  • Write a report. Create a long character variable. Use DATA _NULL_ to write a report rather than create a data set. The LENGTH statement creates a variable with length long enough to contain the name of an external file:

     data _null_;     length name $ 200; 
  • Read an in-stream data line and assign a value to the NAME variable:

     input name $; 
  • Close the current output file and open a new one when the NAME variable changes. The file-specification is just a place holder; it can be any valid SAS name:

     file  file-specification  filevar=name mod;     date = date(); 
  • Append a log record to currently open output file:

     put records updated  date date.; 
  • Supply the names of the external files:

     datalines;  external-file-1   external-file-2   external-file-3  ; 

Example 6: When the Output Line Exceeds the Line Length of the Output File

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 $ 71-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.

Example 7: Reading Data and Writing Text through a TCP/IP Socket

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; 

Example 8: Specifying an Encoding When Writing to an Output File

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-data-library  ';  filename outfile '  external-file  ';  data _null_;     set myfiles.cars;     file outfile   encoding="utf-8"   ;     put Make Model Year;  run; 

See Also

Statements:

  • FILE Statement for ODS in SAS Output Delivery System: User s Guide

  • FILENAME Statement on page 1169

  • INFILE Statement on page 1222

  • LABEL Statement on page 1277

  • PUT Statement on page 1342

  • RETURN Statement on page 1386

  • TITLE Statement on page 1407




SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 704

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net