Chapter Contents

Previous

Next
PUT, Formatted

PUT, Formatted



Writes variable values with the specified format in the output line

Valid: in a DATA step
Category: File-handling
Type: Executable


Syntax
Arguments
Details
Using Formatted Output
How to Group Variables and Formats
Examples
Example 1: Writing a Character between Formatted Values
Example 2: Overriding the Default Alignment of Formatted Values
See Also

Syntax

PUT <pointer-control> variable format. <@ | @@>;
PUT <pointer-control> (variable-list) (format-list)
<@ | @@>;

Arguments

pointer-control
moves the output pointer to a specified line or column.
See: Column Pointer Controls and Line Pointer Controls
Featured in: Writing a Character between Formatted Values

variable
names the variable whose value is written.

(variable-list)
specifies a list of variables whose values are written.
Requirement: The (format-list) must follow the (variable-list).
See: How to Group Variables and Formats
Featured in: Writing a Character between Formatted Values

format.
specifies a format to use when the variable values are written. To override the default alignment, you can add an alignment specification to a format:
-L left aligns the value.
-C centers the value.
-R right aligns the value.
Tip: Ensure that the format width provides enough space to write the value and any commas, dollar signs, decimal points, or other special characters that the format includes.
Example: This PUT statement uses the format dollar7.2 to write the value of X:
put x dollar7.2;
When X is 100, the formatted value uses seven columns:
$100.00
Featured in: Overriding the Default Alignment of Formatted Values

(format-list)
specifies a list of formats to use when the values of the preceding list of variables are written. In a PUT statement, a format-list can include

format.
specifies the format to use to write the variable values.
Tip: You can specify either a SAS format or a user-written format. See Formats.

pointer-control
specifies one of these pointer controls to use to position a value: @, #, /, +, and OVERPRINT.
Example: Writing a Character between Formatted Values

character-string
specifies one or more characters to place between formatted values.
Example: This statement places a hyphen between the formatted values of CODE1, CODE2, and CODE3:
put bldg $ (code1 code2 code3) (3. '-');
See: Writing a Character between Formatted Values

n*
specifies to repeat n times the next format in a format list.
Example: This statement uses the 7.2 format to write GRADES1, GRADES2, and GRADES3 and the 5.2 format to write GRADES4 and GRADES5:
put (grades1-grades5) (3*7.2, 2*5.2);
Restriction: The (format-list) must follow (variable-list).
See Also: How to Group Variables and Formats

@| @@
holds an output line for the execution of the next PUT statement even across iterations of the DATA step. These line-hold specifiers are called trailing @ and double trailing @.
Restriction: The trailing @ or double trailing @ must be the last item in the PUT statement.
See: Using Line-hold Specifiers


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 .

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 example, this format list includes more specifications than are necessary when the PUT statement writes the last variable:

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:(footnote 1)

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

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.


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 & $15. bldg $ room;
   put name @20 (bldg room) ($1. "-" 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 $12.-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.)

See Also

Statement:

PUT


FOOTNOTE 1:  The ruled line is for illustrative purposes only; the PUT statement does not generate it. [arrow]


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.