SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3

Writes variable values with the specified format in the output line

Valid: in a DATA step

Category: File-handling

Type: Executable

Syntax

PUT < pointer-control > variable format. <@ @@>;

PUT < pointer-control > ( variable-list ) ( format-list )

Arguments

pointer-control

variable

( variable-list )

format.

( format-list )

Details

Using Formatted Output The Formatted output describes the output lines by listing the variable names and the formats to use to write the values. You can use a SAS format or a user-written format to control how SAS prints the variable values. For a complete description of the SAS formats, see Definition of Formats on page 71.

With formatted output, the PUT statement uses the format that follows the variable name to write each value. SAS does not automatically add blanks between values. If the value uses fewer columns than specified, character values are left-aligned and numeric values are right-aligned in the field that is specified by the format width.

Formatted output, combined with pointer controls, makes it possible to specify the exact line and column location to write each variable. For example, this PUT statement uses the dollar7.2 format and centers the value of X starting at column 12:

put @12 x dollar7.2-c;

How to Group Variables and Formats When you want to write values in a pattern on the output lines, use format lists to shorten your coding time. A format list consists of the corresponding formats separated by either blanks or commas and enclosed in parentheses. It must follow the names of the variables enclosed in parentheses.

For example, this statement uses a format list to write the five variables SCORE1 through SCORE5, one after another, using four columns for each value with no blanks in between:

put (score1-score5) (4. 4. 4. 4. 4.);

A shorter version of the previous statement is

put (score1-score5) (4.);

You can include any of the pointer controls (@, #, /, +, and OVERPRINT) in the list of formats, as well as n *, and a character string. You can use as many format lists as necessary in a PUT statement, but do not nest the format lists. After all the values in the variable list are written, the PUT statement ignores any directions that remain in the format list. For an example, see Example 3 on page 1363.

You can also specify a reference to all elements in an array as ( array-name {*}), followed by a list of formats. You cannot, however, specify the elements in a _TEMPORARY_ array in this way. This PUT statement specifies an array name and a format list:

put (array1{*}) (4.);

For more information on how to reference an array, see Arrays on page 1351.

Examples

Example 1: Writing a Character between Formatted Values

This example formats some values and writes a - (hyphen) between the values of variables BLDG and ROOM:

data _null_; input name & . bldg $ room; put name @20 (bldg room) (. "-" 3.); datalines; Bill Perkins J 126 Sydney Riley C 219 ;

These lines are written to the SAS log:

Bill Perkins J-126 Sydney Riley C-219

Example 2: Overriding the Default Alignment of Formatted Values

This example includes an alignment specification in the format:

data _null_; input name $ 1-12 score1 score2 score3; put name .-r +3 score1 3. score2 3. score3 4.; datalines; Joseph 11 32 76 Mitchel 13 29 82 Sue Ellen 14 27 74 ;

These lines are written to the log:

----+----1----+----2----+----3----+----4 Joseph 11 32 76 Mitchel 13 29 82 Sue Ellen 14 27 74

The value of the character variable NAME is right-aligned in the formatted field. (Left alignment is the default for character variables.)

Example 3: Including More Format Specifications Than Necessary

This format list includes more specifications than are necessary when the PUT statement executes:

data _null_; input x y z; put (x y z) (2.,+1); datalines; 2 24 36 0 20 30 ;

The PUT statement writes the value of X using the 2. format. Then, the +1 column pointer control moves the pointer forward one column. Next, the value of Y is written with the 2. format. Again, the +1 column pointer moves the pointer forward one column. Then, the value of Z is written with the 2. format. For the third iteration, the PUT statement ignores the +1 pointer control.

These lines are written to the SAS log: [ *]

----+----1----+ 2 24 36 0 20 30

See Also

Statement:

[ *] The ruled line is for illustrative purposes only; the PUT statement does not generate it.

Категории