|SAS Companion for the CMS Environment|
You can use the FILE statement or the FILE command to write to an external file.
|Using the FILE Statement|
The FILE statement specifies the current output file for PUT statements in the DATA step. (See SAS Language Reference: Dictionary for a complete description of the PUT statement.)
The specified output file must be an external file, not a SAS data library. It cannot be a CMS MACLIB or an OS/390 data set. If no FILE statement is specified, then any PUT statements in your SAS program write to the SAS log. The FILE statement is executable; therefore, you can use it in conditional processing (in an IF/THEN statement, for example).
When multiple FILE statements are present, the PUT statement builds and writes output lines to the file that was specified in the most recent FILE statement.
For complete information about the FILE statement, see FILE.
The syntax of the FILE statement is
|FILE file-specification <options>;|
|filename filetype filemode (or SFS directory)||
|fileref (filename filetype)||
See Identifying an External File for details.
You can use options to do the following:
Use the FILEVAR= option in the FILE statement to dynamically change output files in the middle of a DATA step. For example:
data _null_; length x $20; x='old file a'; file cc filevar=x ; put 'line one'; x='new file b'; file cc filevar=x ; put 'line two'; run;
These statements place 'line one' in OLD FILE A while NEW FILE B contains 'line two'.
|Using the FILE Command|
The FILE command writes the entire contents of the current window to an external file without removing text from the window. You can specify a previously assigned fileref or an external file.
The form of the FILE command is
|FILE <file-specification> <options>|
The file-specification argument is in one of the forms that is given in Identifying an External File . The file specification cannot be applied to a CMS MACLIB or OS/390 data set. For information about the available options, see the help for base SAS or see SAS Language Reference: Dictionary.
For example, suppose you specify this FILENAME statement:
filename sasfile 'myfile saspgm b';
The following command-line command will copy the text from the Program Editor window to the disk file MYFILE SASPGM B:
If you do not give a value for file-specification, the file from the previous FILE or INCLUDE command is used. If you have not issued previous FILE or INCLUDE commands, then an error message tells you that no default file exists.
The FILE command does not create a PRINT file, even if A you specify the RECFM= option. In order to create a PRINT file from any window, use the PRINT command instead.
Aggregate external files can also be used in a FILE statement. Suppose that a FILENAME statement assigns a fileref to an SFS directory as an aggregate external file. To illustrate a further point, suppose that the FILENAME statement consists of an SFS directory specification only, as follows:
filename mydir 'fpool:myuser.dir';When the filemode is not specified, the FILE statement assumes a filetype of SAS. In the following FILE statement, the contents of the Program Editor window are copied to PGM1 SAS FPOOL:MYUSER.DIR:
Note that aggregate syntax can be applied only to SFS directories and CMS minidisks.
|Writing to Print Files|
When you write SAS output to external files, you need to know the differences between print files and nonprint files.
A print file contains carriage-control information (also called ASA control characters) in column 1 of each line. These characters (blank, 0, - , +, and 1) control the operation of a printer: skipping lines, beginning a new page, and so on. They do not normally appear on a printout. If you do not expect to print the external file, you do not need to write to a print file.
When you write to a print file in a DATA step, SAS shifts
all column specifications in the PUT statement one column to the right to
accommodate the carriage-control characters in column 1.
You can declare an external file as a print file if you specify the PRINT option in the FILE or INFILE statement. For example, the following SAS program writes one line to MYFILE1 FILE, which is declared to be a print file by the FILE statement:
filename out1 'myfile1 file a'; data _null_; file out1 print; put 'line to myfile1'; run;
You can declare a file to be a print file if you include an A (for ASA carriage control) in the RECFM= option in the FILENAME statement. The following SAS program shows an example of this second method:
filename out2 'myfile2 file a' recfm=va; data _null_; file out2; put 'line to myfile2'; run;
Note: You can use either of these techniques
to read from or write to a file with carriage-control characters. If a print
file is being read, the first byte is stripped off and is not returned as
part of the data. If you wish to include the carriage-control bytes as part
of the data, do not declare the file as a print file.
|Writing to Nonprint Files|
A nonprint file that is written by SAS does not contain any characters to control printer operation. The NOPRINT option declares the file as a nonprint file, even if an A is specified in the RECFM= option. For example, the following SAS program writes a nonprint file to MYFILE3 FILE, even though an A has been included in the RECFM= option.
filename out3 'myfile3 file a' recfm=va; data _null_; file out3 noprint; put 'line to myfile3'; run;
Whether you create a print file or a nonprint file, SAS provides default values for most characteristics of the file. FILENAME lists the default file characteristics for print and nonprint files.
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.