Working with Packed Decimal and Zoned Decimal Data

Definitions

Packed decimal
specifies a method of encoding decimal numbers by using each byte to represent two decimal digits. Packed decimal representation stores decimal data with exact precision. The fractional part of the number is determined by the informat or format because there is no separate mantissa and exponent.
An advantage of using packed decimal data is that exact precision can be maintained. However, computations that involve decimal data might become inexact due to the lack of native instructions.
Zoned decimal
specifies a method of encoding decimal numbers in which each digit requires one byte of storage. The last byte contains the number's sign as well as the last digit. Zoned decimal data produces a printable representation.
Nibble
specifies 1/2 of a byte.

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 character. For example, the value 15 is stored in two nibbles, using the hexadecimal characters 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, the number is positive.
The following information applies to packed decimal data representation:
  • You can use the S370FPD format on all platforms to obtain the IBM mainframe configuration.
  • You can have unsigned packed data with no sign indicator. The packed decimal format and informat handle the representation. The representation is consistent between ASCII and EBCDIC platforms.
  • The S370FPDU format and informat expect to have an F in the last nibble. A packed decimal expects no sign nibble.

Zoned Decimal Data

The following information applies to zoned decimal data representation:
  • A zoned decimal representation stores a decimal digit in the low-order nibble of each byte. For all bytes except the byte that contains the sign, the high-order nibble is the numeric zone nibble (F on EBCDIC and 3 on ASCII).
  • 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 expect the sign to be merged into the last byte.
  • 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 character. 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.

Packed Julian Dates

The following information applies to packed Julian dates:
  • The two formats and informats that handle Julian dates in packed decimal representation are PDJULI and PDJULG. PDJULI uses the IBM mainframe year computation, whereas PDJULG uses the Gregorian computation.
  • The IBM mainframe computation considers 1900 to be the base year, and the year values in the data indicate the offset from 1900 (for example, 98 means 1998, 100 means 2000, and 102 means 2002). 1998 would mean 3898.
  • The Gregorian computation allows for two-digit or four-digit years. If you use two-digit years, SAS uses the setting of the YEARCUTOFF= system option to determine the true year.

Platforms That Support Packed Decimal and Zoned Decimal Data

Some platforms have native instructions to support packed and zoned decimal data. Other platforms must use software to emulate the computations. For example, the IBM mainframe has an Add Pack instruction to add packed decimal data, but the platforms that are based on Intel have no such instruction and must convert the decimal data to some other format.

Languages That Support Packed Decimal and Zoned Decimal Data

Several languages support packed decimal and zoned decimal data. The following table shows how COBOL picture clauses correspond to SAS formats and informats.
IBM versus COBOL II Clauses
Corresponding S370Fxxx Formats and Informats
PIC S9(X) PACKED-DECIMAL
S370FPDw.
PIC 9(X) PACKED-DECIMAL
S370FPDUw.
PIC S9(W) DISPLAY
S370FZDw.
PIC 9(W) DISPLAY
S370FZDUw.
PIC S9(W) DISPLAY SIGN LEADING
S370FZDLw.
PIC S9(W) DISPLAY SIGN LEADING SEPARATE
S370FZDSw.
PIC S9(W) DISPLAY SIGN TRAILING SEPARATE
S370FZDTw.
For the packed decimal representation listed in the preceding table, 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, 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.
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. Here is an excerpt from an assembly language list that shows the offset, the value, and the DC statement:
offset  value (in hex)      inst label     directive

+000000 00001C                2 PEX1       DC PL3'1' 
+000003 00001D                3 PEX2       DC PL3'-1'
+000006 F0F0C1                4 ZEX1       DC ZL3'1' 
+000009 F0F0D1                5 ZEX2       DC ZL3'-1'  
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 C languages.

Summary of Packed Decimal and Zoned Decimal Formats and Informats

SAS uses a group of formats and informats to handle packed and zoned decimal data. The following table lists the type of data representation for these formats and informats. The formats and informats that begin with S370 refer to IBM mainframe representation.
Format
Type of Data Representation
Corresponding Informat
Comments
PD
Packed decimal
PD
Local, signed packed decimal
PK
Packed decimal
PK
Unsigned packed decimal; not specific to your operating environment
ZD
Zoned decimal
ZD
Local zoned decimal
none
Zoned decimal
ZDB
Translates EBCDIC blank (hexadecimal 40) to EBCDIC zero (hexadecimal F0); corresponds to the informat as zoned decimal
none
Zoned decimal
ZDV
Zoned decimal notation other than IBM
S370FPD
Packed decimal
S370FPD
Last nibble C (positive) or D (negative)
S370FPDU
Packed decimal
S370FPDU
Last nibble always F (positive)
S370FZD
Zoned decimal
S370FZD
Last byte contains sign in upper nibble: C (positive) or D (negative)
S370FZDU
Zoned decimal
S370FZDU
Unsigned; sign nibble always F
S370FZDL
Zoned decimal
S370FZDL
Sign nibble in first byte in informat; separate leading sign byte of hexadecimal C0 (positive) or D0 (negative) in format
S370FZDS
Zoned decimal
S370FZDS
Leading sign of – (hexadecimal 60) or + (hexadecimal 4E)
S370FZDT
Zoned decimal
S370FZDT
Trailing sign of – (hexadecimal 60) or + (hexadecimal 4E)
PDJULI
Packed decimal
PDJULI
Julian date in packed representation - IBM computation
PDJULG
Packed decimal
PDJULG
Julian date in packed representation - Gregorian computation
none
Packed decimal
RMFDUR
Input layout is: mmsstttF
none
Packed decimal
SHRSTAMP
Input layout is: yyyydddFhhmmssth, where yyyydddF is the packed Julian date; yyyy is a zero-based year from 1900
none
Packed decimal
SMFSTAMP
Input layout is: xxxxxxxxyyyydddF, where yyyydddF is the packed Julian date; yyyy is a zero-based year from 1900
none
Packed decimal
PDTIME
Input layout is: 0hhmmssF
none
Packed decimal
RMFSTAMP
Input layout is: 0hhmmssFyyyydddF, where yyyydddF is the packed Julian date; yyyy is a zero-based year from 1900