April 2005 Technical Tip – Using stripped packed fields in COBOL
Most COBOL programmers would not consider stripped packed decimal, or packed decimal fields without the sign bits, to be a valid data type. Nevertheless, they are used often enough in the mainframe world to be recognized by some of the more popular software packages, including SAS and Easytrieve. It is worthwhile, therefore, for the COBOL programmer to know how to recognize and manipulate fields of this type.
Consider a traditional packed decimal field. If FLDA is defined as PIC S9(5) COMP-3 and one were to MOVE +12345 TO FLDA, the result would be X'12345C'. The rightmost four bits, or nibble, indicate the sign, with X'C' indicating a positive number, X'D' indicating a negative number, and X'F' indicating an unsigned number. But in a stripped packed field, the sign bits are omitted. (Of course, such fields are then unsigned.) The number 12345 would be represented as X'012345'. But this is not a recognized COBOL format and any attempt to reference such a field regardless of its PIC clause (other than PIC X), will cause a data exception (S0C7) abend.
So what if you are a COBOL programmer tasked with reading a file created by someone else (using perhaps SAS, Easytrieve, or even Assembler) which contains stripped packed fields? Take heart, it can be done! All that is required is an understanding of data representation and some clever use of the REDEFINES clause.
Assume my input data is as shown above: X'012345'. I would define this field as PIC X(3). Since a three byte stripped packed field can hold up to six digits, the equivalent packed decimal field would be four bytes long, or PIC S9(7) COMP-3. I would include the following definitions in the WORKING-STORAGE SECTION of my program:
...and I would include the following instructions in the PROCEDURE DIVISION:
After the first move, PACKED-DECIMAL-DATA contains X'012345??'. After the second move, PACKED-DECIMAL-DATA contains X'0123450C', or decimal +123450. Finally, after the divide, PACKED-DECIMAL-DATA contains X'0012345C', or decimal +12345. Success!
We hope you will consider Caliber Data Training when searching for a COBOL training provider! We've got more tricks up our collective sleeves. Give us a call!