![]() Chapter Contents |
![]() Previous |
![]() Next |
| PUT |
| Valid: | in a DATA step |
| Category: | File-handling |
| Type: | Executable |
Syntax |
| PUT <specification(s)><_ODS_><@|@@>; |
The PUT statement without arguments is called a null PUT statement.
| See: | Using Line-hold Specifiers |
| Featured in: | Holding and Releasing Output Lines |
| Arguments |
Note:
Beginning with Version 7, you can specify column-mapped
Output Delivery System variables in the PUT statement. This functionality
is described briefly here in _ODS_, but documented more completely in
PUT, _ODS_
. For more information, see The Complete Guide to the SAS Output Delivery
System. ![[cautend]](../common/images/cautend.gif)
| Requirement: | The (format-list) must follow the (variable-list). |
| See: | PUT, Formatted |
| Example: |
This statement writes
a line of 132 underscores.
put 132*'_'; |
| Featured in: | Underlining Text |
| See: | Column Pointer Controls and Line Pointer Controls |
| See: | Column Output |
| Featured in: | Moving the Pointer within a Page |
| See: | Formatted Output |
| Featured in: | Using Multiple Output Styles in One PUT Statement |
| Restriction: | The (format-list) must follow the (variable-list). |
| See: | PUT, Formatted |
| See: | Named Output |
| Range: | a positive integer |
| Example: |
@15 moves the pointer
to column 15 before the value of NAME is written:
put @15 name $10.; |
| Featured in: | Moving the Pointer within a Page and Underlining Text |
| Range: | a positive integer |
| Example: |
The #2 moves the pointer
to the second line before the value of ID is written in columns 3 and 4:
put @12 name $10. #2 id 3-4; |
| Range: | a positive integer. |
| Tip: | If the value of numeric-variable is not an integer, SAS truncates the decimal value and uses only the integer value. |
| Range: | Expression must result in a positive integer. |
| Tip: | If the value of expression is not an integer, SAS truncates the decimal value and uses only the integer value. |
| Details |
| Output Styles |
There are four ways to write variable values with the PUT statement:
A single PUT statement may contain any or all of the available output styles, depending on how you want to write lines.put name 6-15 age 17-19;These lines are written to the SAS log(footnote 1) :
----+----1----+----2----+
Peterson 21
Morgan 17
The PUT statement writes values for NAME and AGE
in the specified columns. See PUT, Column
for more information.
put name age;writes the values for NAME and AGE to the SAS log:
----+----1----+----2----+ Peterson 21 Morgan 17See PUT, List for more information.
put name $char10. age 2. +1 date mmddyy8.;writes the values for NAME, AGE, and DATE to the SAS log:
----+----1----+----2----+ Peterson 21 07/18/97 Morgan 17 11/12/97Using a pointer control of +1 inserts a blank space between the values of AGE and DATE. See PUT, Formatted for more information.
With named output, list the variable name followed by an equal sign. For example, this PUT statement
put name= age=;writes the values for NAME and AGE to the SAS log:
----+----1----+----2----+ name=Peterson age=21 name=Morgan age=17See PUT, Named for more information.
| Using Multiple Output Styles in a Single PUT Statement |
A PUT statement can combine any or all of the different output styles. For example,
put name 'on ' date mmddyy8. ' weighs '
startwght +(-1) '.' idno= 40-45;
See Using Multiple Output Styles in One PUT Statement for an explanation of the lines written
to the SAS log.
When you combine different output styles, it is important to understand the location of the output pointer after each value is written. For more information on the pointer location, see Pointer Location after a Value Is Written.
| Pointer Controls |
With column and line pointer controls, you can specify an absolute line number or column number to move the pointer or you can specify a column or line location that is relative to the current pointer position. Pointer Controls Available in the PUT Statement lists all pointer controls that are available with the PUT statement.
Note:
Always specify pointer controls before
the variable for which they apply.
See Pointer Location after a Value Is Written for more information on how SAS determines the pointer position.
| Using Line-hold Specifiers |
Line-hold specifiers keep the pointer on the current output line when
Using a trailing @ or double trailing @ can cause SAS to attempt to write past the current line length because the pointer value is unchanged when the next PUT statement executes. See When the Pointer Goes Past the End of a Line.
| Pointer Location after a Value Is Written |
When the output pointer is in the upper left corner of a page,
| When the Pointer Goes Past the End of a Line |
| Arrays |
PUT array-name{*}; With formatted output, the form of this statement is
PUT array-name{*}(format|format.list)The format in parentheses follows the array reference.
| Comparisons |
| Examples |
This example uses several output styles in a single PUT statement:
data club1;
input idno name $ startwght date : date7.;
put name 'on ' date mmddyy8. ' weighs '
startwght +(-1) '.' idno= 32-40;
datalines;
032 David 180 25nov97
049 Amelia 145 25nov97
219 Alan 210 12nov97
;
The types of output styles are
| The values for . . . | Are written with . . . | |
|---|---|---|
| NAME, STARTWGHT | list output | |
| DATE | formatted output | |
| IDNO | named output | |
The program writes the following lines to the SAS log:(footnote 2)
----+----1----+----2----+----3----+----4 David on 11/25/97 weighs 180. idno=1032 Amelia on 11/25/97 weighs 145. idno=1049 Alan on 11/12/97 weighs 210. idno=1219Blank spaces are inserted at the start and the end of the character strings to change the pointer position. These spaces separate the value of a variable from the character string. The +(-1) pointer control moves the pointer backward to remove the unwanted blank that occurs between the value of STARTWGHT and the period. For more information on how to position the pointer, see Pointer Location after a Value Is Written.
put @15 totalsales;This PUT statement moves the pointer to the value that is specified in COLUMN and writes the value of TOTALSALES with the COMMA6 format:
data _null_; set carsales; column=15; put @column totalsales comma6.; run;
data carsales; input item $10. jan : comma5. feb : comma5. mar : comma5.; saleqtr1=sum(jan,feb,mar); /* an expression moves pointer backward */ put '1st qtr sales for ' item 'is ' saleqtr1 : comma6. +(-1) '.'; /* a numeric variable with a negative value moves pointer backward. */ x=-1; put '1st qtr sales for ' item 'is ' saleqtr1 : comma5. +x '.'; datalines; trucks 1,382 2,789 3,556 vans 1,265 2,543 3,987 sedans 2,391 3,011 3,658 ;Because the value of SALEQTR1 is written with modified list output, the pointer moves automatically two spaces. For more information, see How Modified List Output and Formatted Output Differ. To remove the unwanted blank that occurs between the value and the period, move the pointer backward by one space.
The program writes the following lines to the SAS log:
----+----1----+----2----+----3----+----4 st qtr sales for trucks is 7,727. st qtr sales for trucks is 7,727. st qtr sales for vans is 7,795. st qtr sales for vans is 7,795. st qtr sales for sedans is 9,060. st qtr sales for sedans is 9,060.
data _null_; set carsales end=lastrec; totalsales+saleqtr1; if lastrec then put @2 'Total Sales for 1st Qtr' / totalsales 10-15; run;
----+----1----+----2----+----3 Total Sales for 1st Qtr 24582
options pagesize=24 linesize=64 nodate pageno=1;
title1;
data statepop;
input state $ cityp90 ncityp90 region @@;
label cityp90= '1990 metropolitan population
(million)'
ncityp90='1990 nonmetropolitan population
(million)'
region= 'Geographic region';
datalines;
ME .443 .785 1 NH .659 .450 1
VT .152 .411 1 MA 5.788 .229 1
RI .938 .065 1 CT 3.148 .140 1
NY 16.515 1.475 1 NJ 7.730 .A 1
PA 10.083 1.799 1 DE .553 .113 2
MD 4.439 .343 2 DC .607 . 2
VA 4.773 1.414 2 WV .748 1.045 2
NC 4.376 2.253 2 SC 2.423 1.064 2
GA 4.352 2.127 2 FL 12.023 .915 2
KY 1.780 1.906 2 TN 3.298 1.579 2
AL 2.710 1.331 2 MS .776 1.798 2
AR 1.040 1.311 2 LA 3.160 1.060 2
OK 1.870 1.276 2 TX 14.166 2.821 2
OH 8.826 2.021 3 IN 3.962 1.582 3
IL 9.574 1.857 3 MI 7.698 1.598 3
WI 3.331 1.561 3 MN 3.011 1.364 3
IA 1.200 1.577 3 MO 3.491 1.626 3
ND .257 .381 3 SD .221 .475 3
NE .787 .791 3 KS 1.333 1.145 3
MT .191 .608 4 ID .296 .711 4
WY .134 .319 4 CO 2.686 .608 4
NM .842 .673 4 AZ 3.106 .559 4
UT 1.336 .387 4 NV 1.014 .183 4
WA 4.036 .830 4 OR 1.985 .858 4
CA 28.799 .961 4 AK .226 .324 4
HI .836 .272 4
;
proc format;
value regfmt 1='Northeast'
2='South'
3='Midwest'
4='West';
run;
data _null_;
set statepop;
by region;
pop90=sum(cityp90,ncityp90);
file print;
put state 1-2 @5 pop90 7.3 ' million';
if first.region then
regioncitypop=0; /* new region */
regioncitypop+cityp90;
if last.region then
do;
put // '1990 US CENSUS for ' region regfmt.
/ 'Total Urban Population: '
regioncitypop' million' _page_;
end;
run;
PUT Statement Output for the Northeast Region
1 ME 1.228 million NH 1.109 million VT 0.563 million MA 6.017 million RI 1.003 million CT 3.288 million NY 17.990 million NJ 7.730 million PA 11.882 million 1990 US CENSUS for Northeast Total Urban Population: 45.456 million |
PUT _PAGE_ advances the pointer to line 1 of the new
page when the value of LAST.REGION is 1. The example prints a footer message
before exiting the
page.
This example uses OVERPRINT to underscore a value written by a previous PUT statement:
data _null_;
input idno name $ startwght;
file file-specification print;
put name 1-10 @15 startwght 3.;
if startwght > 200 then
put overprint @15 '___';
datalines;
032 David 180
049 Amelia 145
219 Alan 210
;
The second PUT statement underlines weights above 200 on the output
line the first PUT statement prints.
This PUT statement uses OVERPRINT with both a column pointer control and a line pointer control:
put @5 name $8. overprint @5 8*'_'
/ @20 address;
The PUT statement writes a NAME value, underlines
it by overprinting eight underscores, and moves the output pointer to the
next line to write an ADDRESS value.
This DATA step demonstrates how to hold and release an output line with a PUT statement:
data _null_;
input idno name $ startwght 3.;
put name @;
if startwght ne . then
put @15 startwght;
else put;
datalines;
032 David 180
049 Amelia 145
126 Monica
219 Alan 210
;
In this example,
The program writes the following lines to the SAS log:
----+----1----+----2 David 180 Amelia 145 Monica Alan 210
data team;
input id team $ score1 score2;
if id le 1000 then
do;
put _infile_;
delete;
end;
datalines;
032 red 180 165
049 yellow 145 124
219 red 210 192
;
The program writes the following lines to the SAS log:
----+----1----+----2 219 red 210 192
| See Also |
Statements:
| |||||
System
Options:
|
FOOTNOTE 2:
The ruled line is for illustrative
purposes
only; the PUT statement does not generate it.
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.