PUT Statement
Prints the values of program variables, arrays, and constants to the SAS log.
| Category: | Local |
|---|
Table of Contents
Syntax
Without Arguments
PUT without arguments prints a blank line to the SAS log.
Arguments
_ALL_
prints the values of all variables, which includes predefined variables, to the SAS log.
'character-string'
specifies a string of text that is written to the SAS log.
identifier
names a variable whose value is written to the SAS log.
array-reference
specifies an array element. The subscript can be any SAS expression that resolves to an integer value when the PUT statement executes. Use the array subscript asterisk (*) to write all elements of the array.
this-expression
specifies a THIS expression.
| See | THIS Expression |
|---|
=
If an equal sign is added after a variable or array element, then the output is preceded by the variable or array element name and an equal sign.
:
enables you to specify a format that the PUT statement uses to write the variable value. All leading and trailing blanks are deleted, and each value is followed by a single blank.
| Restriction | If you use “:”, you must specify a format. |
|---|
format
specifies a format to use when the data value is written to the SAS log. If you use a colon modifier (:) with the format name, all leading and trailing blanks are deleted and each value is followed by a single blank. 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. |
|---|
Details
How to Use the PUT Statement
The PUT statement consists of the keyword PUT followed by a list of variables and constants. You list the names of the variables whose values you want written, or you specify a character string in quotation marks. If you do not specify a format, the PUT statement writes a variable value with the default format, inserts a single blank, and then writes the next value. If you specify a format, the output is written using the format width. Character values are left-aligned in the field; leading and trailing blanks are removed. Numeric values are right-aligned in the field.
How nonexistent data (SAS missing values or null values) are written depends on whether you are in ANSI mode or SAS mode. A period is generated for DOUBLE missing dot and null values when the default format, BEST32., is associated with the DOUBLE. For special missing values (.a-.z and ._), the character after the period is generated when using BEST32.. For example, if a variable held the value .a, A would be generated. INTEGER and other non-DOUBLE numeric types cannot be missing. However, they can be null in which case nothing is generated by the PUT statement when the value is null. For more information, see How DS2 Processes Nulls and SAS Missing Values in SAS DS2 Programmer’s Guide.
How List Output Is Spaced
List output uses different spacing methods when it writes variable values and character strings. When a variable is written with list output, SAS automatically inserts a blank space. The output pointer stops at the second column that follows the variable value.
dcl int a b;
dcl varchar c d;
area = 9924;
city = 1001;
ctry1='Peru';
ctry2='Bolivia';
put area city ctry1 ctry2;
These lines are written to the SAS log.
----+----1----+----2
9924 1001 Peru Bolivia
However, when a character string is written, SAS does not automatically insert a blank space. The output pointer stops at the column that immediately follows the last character in the string.
To avoid spacing problems when both character strings and variable values are written, you might want to use a blank space as the last character in a character string.
If you use a colon modifier (:) with the format name, SAS writes the value with the specified format, inserts a blank space, and moves the pointer to the next column.
dcl int a b;
dcl varchar c d;
area = 9924;
POP = 1000;
ctry1='Peru';
ctry2='Bolivia';
put area : octal10. pop : comma8.2 ctry1 ctry2;
These lines are written to the SAS log.
----+----1----+----2----+----3----+
0000023304 1,000.00 Peru Bolivia
Formatted Output
You can use a format to control how SAS prints the variable values. The PUT statement uses the format that follows the variable name to write each value. With formatted output, SAS does not automatically add blanks between values. Formatted output moves the pointer the length of the format, even if the value does not fill that length. The pointer moves to the next column; an intervening blank is not inserted. 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.
dcl int a b;
dcl varchar c d;
pop = 1000
area = 9924;
ctry1='Peru';
ctry2='Bolivia';
put area octal10. pop comma8.2 ctry1 ctry2;
These lines are written to the SAS log.
----+----1----+----2----+----3----+
000000233041,000.00Peru Bolivia
If no format is specified, the variable's default format is used. You can associate a format with a column by using a HAVING clause in the DECLARE statement. For more information, see DECLARE Statement.
Comparisons
The PUT statement and the PUT function have similar behavior. However, the PUT statement directs its results to the SAS log whereas the PUT function returns an NCHAR value containing the result of formatting its argument.
Examples
Example 1: PUT Statements
This example contains several PUT statements.
x = 1;
y = 2;
z = 3;
s = 'abc';
a[4] = 99;
put 'x = ' x;
put 'y = ' y;
put z s a[4];
put x= y= a[2]=;
put 'x=' x ' y=' y ' a[2]=' a[2];
Example 2: Using PUT to Write Arrays
This example writes the contents of both temporary and variable arrays.
data _null_;
declare double a[6] having format 4.2;
vararray double b[2, 3];
declare double c[0:1, 2:4, 5:5];
method init();
a := (3 6 9 12 15 18);
b := (3 6 9 12 15 18);
c := (3 6 9 12 15 18);
put a[*]=;
put b[*]=;
put c[*]=;
put 'array a with default format:' a[*];
put 'array a with best3. format:' a[*] best3.;
end;
enddata;
The following lines are written to the SAS log.
a[1]=3.00 a[2]=6.00 a[3]=9.00 a[4]=12.0 a[5]=15.0 a[6]=18.0 b[1,1]=3 b[1,2]=6 b[1,3]=9 b[2,1]=12 b[2,2]=15 b[2,3]=18 c[0,2,5]=3 c[0,3,5]=6 c[0,4,5]=9 c[1,2,5]=12 c[1,3,5]=15 c[1,4,5]=18 array a with default format: 3.00 6.00 9.00 12.0 15.0 18.0 array a with best3. format: 3 6 9 12 15 18
Example 3: Using the _ALL_ Argument
This example uses the _ALL_ argument in the PUT statement to print the values of all variables, including the predefined _N_ variable, to the SAS log.
data;
dcl double a b c;
method init();
a = 116;
b = 220;
c = 37;
put _all_;
end;
enddata;
run;
The following lines are written to the SAS log.
a=116 b=220 c=37 _n_=1
See Also
-
How to Write Array Content in SAS DS2 Programmer’s Guide
-
Functions: