Format
Format
Format
CHAPTER
5
Formats
Definition 27
Syntax 27
Using Formats 28
Ways to Specify Formats 28
PUT Statement 29
PUT Function 29
%SYSFUNC 29
FORMAT Statement 29
ATTRIB Statement 30
Permanent versus Temporary Association 30
User-Defined Formats 30
Byte Ordering on Big Endian and Little Endian Platforms 31
Definitions 31
How Bytes are Ordered Differently 31
Writing Data Generated on Big Endian or Little Endian Platforms 31
Integer Binary Notation and Different Programming Languages 32
Working with Packed Decimal and Zoned Decimal Data 33
Definitions 33
Types of Data 33
Packed Decimal Data 33
Zoned Decimal Data 33
Packed Julian Dates 34
Platforms Supporting Packed Decimal and Zoned Decimal Data 34
Languages Supporting Packed Decimal and Zoned Decimal Data 34
Summary of Packed Decimal and Zoned Decimal Formats and Informats
Formats by Category 36
35
Definition
A format is an instruction that SAS uses to write data values. You use formats to
control the written appearance of data values, or, in some cases, to group data values
together for analysis. For example, the WORDS22. format, which converts numeric
values to their equivalent in words, writes the numeric value 692 as six hundred
ninety-two.
Syntax
SAS formats have the following form:
28
Using Formats
Chapter 5
<$>format<w>.< d>
where
$
indicates a character format; its absence indicates a numeric format.
format
names the format. The format is a SAS format or a user-defined format that was
previously defined with the VALUE statement in PROC FORMAT. For more
information on user-defined formats, see the FORMAT procedure in the SAS
Procedures Guide.
w
specifies the format width, which for most formats is the number of columns in the
output data.
d
specifies an optional decimal scaling factor in the numeric formats.
Formats always contain a period (.) as a part of the name. If you omit the w and the d
values from the format, SAS uses default values. The d value that you specify with a
format tells SAS to display that many decimal places, regardless of how many decimal
places are in the data. Formats never change or truncate the internally stored data
values.
For example, in DOLLAR10.2, the w value of 10 specifies a maximum of 10 columns
for the value. The d value of 2 specifies that two of these columns are for the decimal
part of the value, which leaves eight columns for all the remaining characters in the
value. This includes the decimal point, the remaining numeric value, a minus sign if
the value is negative, the dollar sign, and commas, if any.
If the format width is too narrow to represent a value, SAS tries to squeeze the value
into the space available. Character formats truncate values on the right. Numeric
formats sometimes revert to the BESTw.d format. SAS prints asterisks if you do not
specify an adequate width. In the following example, the result is x=**.
x=123;
put x=2.;
If you use an incompatible format, such as using a numeric format to write character
values, SAS first attempts to use an analogous format of the other type. If this is not
feasible, an error message that describes the problem appears in the SAS log.
Using Formats
Ways to Specify Formats
You can use formats in the following ways:
3 in a PUT statement
3 with the PUT, PUTC, or PUTN functions
3 with the %SYSFUNC macro function
3 in a FORMAT statement in a DATA step or a PROC step
3 in an ATTRIB statement in a DATA step or a PROC step.
Formats
29
PUT Statement
The PUT statement with a format after the variable name uses a format to write
data values in a DATA step. For example, this PUT statement uses the DOLLAR.
format to write the numeric value for AMOUNT as a dollar amount:
amount=1145.32;
put amount dollar10.2;
For more information, see the PUT statement in SAS Language Reference: Dictionary.
PUT Function
The PUT function writes a numeric variable, a character variable, or a constant with
any valid format and returns the resulting character value. For example, the following
statement converts the values of a numeric variable into a two-character hexadecimal
representation:
num=15;
char=put(num,hex2.);
The PUT function creates a character variable named CHAR that has a value of 0F.
The PUT function is useful for converting a numeric value to a character value. For
more information, see the PUT function in SAS Language Reference: Dictionary.
%SYSFUNC
The %SYSFUNC (or %QSYSFUNC) macro function executes SAS functions or
user-defined functions and applies an optional format to the result of the function
outside a DATA step. For example, the following program writes a numeric value in a
macro variable as a dollar amount.
%macro tst(amount);
%put %sysfunc(putn(&amount,dollar10.2));
%mend tst;
%tst (1154.23);
FORMAT Statement
The FORMAT statement permanently associates a format with a variable. SAS uses
the format to write the values of the variable that you specify. For example, the
following statement in a DATA step associates the COMMAw.d numeric format with
the variables SALES1 through SALES3:
format sales1-sales3 comma10.2;
Because the FORMAT statement permanently associates a format with a variable, any
subsequent DATA step or PROC step uses COMMA10.2 to write the values of SALES1,
SALES2, and SALES3. For more information, see the FORMAT statement in SAS
Language Reference: Dictionary.
Note: Formats that you specify in a PUT statement behave differently from those
that you associate with a variable in a FORMAT statement. The major difference is
that formats that are specified in the PUT statement will preserve leading blanks. If
30
Chapter 5
you assign formats with a FORMAT statement prior to a PUT statement, all leading
blanks are trimmed. The result is the same as if you used the colon (:) format modifier.
For details about using the colon (:) format modifier, see the PUT, List statement in
SAS Language Reference: Dictionary. 4
ATTRIB Statement
The ATTRIB statement can also associate a format, as well as other attributes, with
one or more variables. For example, in the following statement the ATTRIB statement
permanently associates the COMMAw.d format with the variables SALES1 through
SALES3:
attrib sales1-sales3 format=comma10.2;
Because the ATTRIB statement permanently associates a format with a variable, any
subsequent DATA step or PROC step uses COMMA10.2 to write the values of SALES1,
SALES2, and SALES3. For more information, see the ATTRIB statement in SAS
Language Reference: Dictionary.
User-Defined Formats
In addition to the formats that are supplied with base SAS software, you can create
your own formats. In base SAS software, PROC FORMAT allows you to create your
own formats for both character and numeric variables. For more information, see the
FORMAT procedure in the SAS Procedures Guide.
When you execute a SAS program that uses user-defined formats, these formats
should be available. The two ways to make these formats available are
3 to create permanent, not temporary, formats with PROC FORMAT
3 to store the source code that creates the formats (the PROC FORMAT step) with
the SAS program that uses them.
To create permanent SAS formats, see the FORMAT procedure in the SAS Procedures
Guide.
If you execute a program that cannot locate a user-defined format, the result depends
on the setting of the FMTERR system option. If the user-defined format is not found,
then these system options produce these results:
Formats
System Options
Results
FMTERR
NOFMTERR
31
Although using NOFMTERR enables SAS to process a variable, you lose the
information that the user-defined format supplies.
To avoid problems, make sure that your program has access to all user-defined
formats that are used.
32
Chapter 5
Data created
for ...
Data written
by ...
Sign?
Format
big endian
big endian
yes
IB or S370FIB
big endian
big endian
no
PIB, S370FPIB,
S370FIBU
big endian
little endian
yes
S370FIB
big endian
little endian
no
S370FPIB
little endian
big endian
yes
IBR
little endian
big endian
no
PIBR
little endian
little endian
yes
IB or IBR
little endian
little endian
no
PIB or PIBR
big endian
either
yes
S370FIB
big endian
either
no
S370FPIB
little endian
either
yes
IBR
little endian
either
no
PIBR
Language
2 Bytes
4 Bytes
SAS
PL/I
FIXED BIN(15)
FIXED BIN(31)
FORTRAN
INTEGER*2
INTEGER*4
COBOL
IBM assembler
short
long
Formats
Types of Data
33
Zoned decimal
Nibble
Types of Data
Packed Decimal Data
A packed decimal representation stores decimal digits in each nibble of a byte.
Each byte has two nibbles, and each nibble is indicated by a hexadecimal digit. For
example, the value 15 is stored in two nibbles, using the hexadecimal digits 1 and 5.
The sign indication is dependent on your operating environment. On IBM
mainframes, the sign is indicated by the last nibble. With formats, C indicates a
positive value, and D indicates a negative value. With informats, A, C, E, and F
indicate positive values, and B and D indicate negative values. Any other nibble is
invalid for signed packed decimal data. In all other operating environments, the sign is
indicated in its own byte. If the high-order bit is 1, then the number is negative.
Otherwise, it is positive.
The following applies to packed decimal data representation:
3 You can use the S370FPD format on all platforms to obtain the IBM mainframe
configuration.
3 You can have unsigned packed data with no sign indicator. The packed decimal
format and informat handles the representation. It is consistent between ASCII
and EBCDIC platforms.
3 Note that the S370FPDU format and informat expects to have an F in the last
nibble, while packed decimal expects no sign nibble.
3 A zoned decimal representation stores a decimal digit in the low order nibble of
each byte. For all but the byte containing the sign, the high-order nibble is the
numeric zone nibble (F on EBCDIC and 3 on ASCII).
34
Chapter 5
3 The sign can be merged into a byte with a digit, or it can be separate, depending
on the representation. But the standard zoned decimal format and informat
expects the sign to be merged into the last byte.
3 The EBCDIC and ASCII zoned decimal formats produce the same printable
representation of numbers. There are two nibbles per byte, each indicated by a
hexadecimal digit. For example, the value 15 is stored in two bytes. The first byte
contains the hexadecimal value F1 and the second byte contains the hexadecimal
value C5.
Corresponding S370Fxxx
formats/informats
S370FPDw.
S370FPDUw.
S370FZDw.
S370FZDUw.
S370FZDLw.
S370FZDSw.
S370FZDTw.
For the packed decimal representation listed above, X indicates the number of digits
represented, and W is the number of bytes. For PIC S9(X) PACKED-DECIMAL, W is
ceil((x+1)/2). For PIC 9(X) PACKED-DECIMAL, W is ceil (x/2). For example,
Formats
35
PIC S9(5) PACKED-DECIMAL represents five digits. If a sign is included, six nibbles
are needed. ceil((5+1)/2) has a length of three bytes, and the value of W is 3.
Note that you can substitute COMP-3 for PACKED-DECIMAL.
In IBM assembly language, the P directive indicates packed decimal, and the Z
directive indicates zoned decimal. The following shows an excerpt from an assembly
language listing, showing the offset, the value, and the DC statement:
offset
+000000
+000003
+000006
+000009
00001C
00001D
F0F0C1
F0F0D1
inst label
2
3
4
5
PEX1
PEX2
ZEX1
ZEX2
directive
DC
DC
DC
DC
PL31
PL3-1
ZL31
ZL31
In PL/I, the FIXED DECIMAL attribute is used in conjunction with packed decimal
data. You must use the PICTURE specification to represent zoned decimal data. There
is no standardized representation of decimal data for the FORTRAN or the C languages.
Type of data
representation
Corresponding
informat
Comments
PD
Packed decimal
PD
PK
Packed decimal
PK
ZD
Zoned decimal
ZD
none
Zoned decimal
ZDB
none
Zoned decimal
ZDV
S370FPD
Packed decimal
S370FPD
S370FPDU
Packed decimal
S370FPDU
S370FZD
Zoned decimal
S370FZD
S370FZDU
Zoned decimal
S370FZDU
36
Formats by Category
Chapter 5
Format
Type of data
representation
Corresponding
informat
Comments
S370FZDL
Zoned decimal
S370FZDL
S370FZDS
Zoned decimal
S370FZDS
S370FZDT
Zoned decimal
S370FZDT
PDJULI
Packed decimal
PDJULI
PDJULG
Packed decimal
PDJULG
none
Packed decimal
RMFDUR
none
Packed decimal
SHRSTAMP
none
Packed decimal
SMFSTAMP
none
Packed decimal
PDTIME
none
Packed decimal
RMFSTAMP
Formats by Category
There are four categories of formats in SAS:
Category
Description
CHARACTER
instructs SAS to write data values from variables that represent dates,
times, and datetimes.
DBCS
Formats
Formats by Category
Category
Description
NUMERIC
USER-DEFINED
instructs SAS to write data values by using a format that is created with
PROC FORMAT.
37
Category
Format
Description
Character
$ASCIIw.
$BINARYw.
$CHARw.
$EBCDICw.
$HEXw.
$MSGCASEw.
$OCTALw.
$QUOTEw.
$REVERJw.
$REVERSw.
$UPCASEw.
$VARYINGw.
$w.
$KANJIw.
$KANJIXw.
DATEw.
DATEAMPMw.d
DATETIMEw.d
DAYw.
DDMMYYw.
DBCS
38
Formats by Category
Category
Chapter 5
Format
Description
DDMMYYxw.
DOWNAMEw.
EURDFDDw.
EURDFDEw.
EURDFDNw.
EURDFDTw.d
EURDFDWNw.
EURDFMNw.
EURDFMYw.
EURDFWDXw.
EURDFWKXw.
HHMMw.d
HOURw.d
JULDAYw.
JULIANw.
MINGUOw.
MMDDYYw.
MMDDYYxw.
MMSSw.d
MMYYxw.
MONNAMEw.
MONTHw.
MONYYw
NENGOw.
Formats
Category
Numeric
Formats by Category
Format
Description
PDJULGw.
PDJULIw.
QTRw.
QTRRw.
TIMEw.
TIMEAMPMw.d
TODw.d
WEEKDATEw.
Writes date values as the day of the week and the date
in the form day-of-week, month-name dd, yy (or yyyy)
WEEKDATXw.
WEEKDAYw.
WORDDATEw.
WORDDATXw.
YEARw.
YYMMxw.
YYMMDDw.
YYMMDDxw.
YYMONw.
YYQxw.
YYQRxw.
BESTw.
BINARYw.
COMMAw.d
COMMAXw.d
Dw.s
DOLLARw.d
39
40
Formats by Category
Category
Chapter 5
Format
Description
DOLLARXw.d
Ew.
FLOATw.d
FRACTw.
HEXw.
IBw.d
IBRw.d
IEEEw.d
NEGPARENw.d
NUMXw.d
OCTALw.
PDw.
PERCENTw.d
PIBw.d
PIBRw.d
PKw.d
PVALUEw.d
Writes p-values
RBw.d
ROMANw.
SSNw.
S370FFw.d
S370FIBw.d
S370FIBUw.d
S370FPDw.
S370FPDUw.
S370FPIBw.d
Formats
Category
Formats by Category
Format
Description
S370FRBw.d
S370FZDw.d
S370FZDLw.d
S370FZDSw.d
S370FZDTw.d
S370FZDUw.d
w.d
WORDFw.
WORDSw.
YENw.d
Zw.d
ZDw.d
41
42
Formats by Category
Chapter 5
The correct bibliographic citation for this manual is as follows: SAS Institute Inc., SAS
Language Reference: Concepts, Cary, NC: SAS Institute Inc., 1999. 554 pages.
SAS Language Reference: Concepts
Copyright 1999 SAS Institute Inc., Cary, NC, USA.
ISBN 1580254411
All rights reserved. Printed in the United States of America. No part of this publication
may be reproduced, stored in a retrieval system, or transmitted, by any form or by any
means, electronic, mechanical, photocopying, or otherwise, without the prior written
permission of the publisher, SAS Institute, Inc.
U.S. Government Restricted Rights Notice. Use, duplication, or disclosure of the
software by the government is subject to restrictions as set forth in FAR 52.22719
Commercial Computer Software-Restricted Rights (June 1987).
SAS Institute Inc., SAS Campus Drive, Cary, North Carolina 27513.
1st printing, November 1999
SAS and all other SAS Institute Inc. product or service names are registered trademarks
or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA
registration.
IBM, ACF/VTAM, AIX, APPN, MVS/ESA, OS/2, OS/390, VM/ESA, and VTAM are
registered trademarks or trademarks of International Business Machines Corporation.
indicates USA registration.
Other brand and product names are registered trademarks or trademarks of their
respective companies.
The Institute is a private company devoted to the support and further development of its
software and related services.