Wednesday, February 17, 2010

2. Working with data

Hierarchical representation of data
Level numbers
The record description specifies the format of a record . Record description entries indicate :
The items or fields to appear in the record
The order in which the fields appear
How these fields are related to one another

Data is grouped in COBOL using the concept of a level. Records are considered the highest level of data in a file, and are coded on the 01 level. There can be only one data-name at the 01 level for each record, as it is the all-inclusive data-name. A field of data within the record is coded on a level subordinate to 01, i.e 02, 03 and so on. Any level number between 02 and 49 may be used to describe fields within a record.

Elementary and Group Items
An elementary item has no subordinate parts. A group item may consist of one or more group items. In the code below :
FIRST-NAME, ZIP-CODE are elementary items
CUSTOMER-ADDRESS, STREET are group items

Eg 2.1:

01 CUSTOMER-ADDRESS
02 NAME
03 FIRST-NAME
03 LAST-NAME
02 STREET
03 S-NUMBER
03 STREET-NAME
02 ZIP-CODE

PICTURE Clauses
Group items are defined by a level number and a name, which is followed by a period. Elementary items must be described with a PICTURE (or PIC, for short) clause.

Functions of the PICTURE Clause
Specifies the type of data contained within an elementary item.
Indicates the size of the field.





Types of data fields

Type
Picture clause
Explanation
Alphabetic
A
A field that may contain only letters or blanks. E.g. a name or item description field.
Alphanumeric
X
A field that may contain any character i.e. letters, digits, blanks and/or special characters. E.g. an address field.
Numeric
9
Any signed or unsigned field that will contain only digits is considered numeric.

Size of data fields
The size of the field is denoted by the number of A’s, X’s, or 9’s used in the PICTURE.

Eg 2.2:
05 AMT PIC 99999.

AMT is an elementary field consisting of five positions of numeric data. Alternatively, the same can be coded as follows :

Eg 2.3:
05 AMT PIC 9(5).

At least one space must follow the word PIC. All A’s, X’s, or 9’s should appear consecutively with no spaces between the characters. Similarly, if parentheses are used to denote the size of a field, no spaces should appear within the parentheses.

Eg 2.4:
01 CUSTOMER-ADDRESS.
02 NAME.
03 FIRST-NAME PIC X(10).
03 LAST-NAME PIC X(15).
02 STREET.
03 S-NUMBER PIC 9(3).
03 STREET-NAME PIC X(20).
02 ZIP-CODE PIC 9(6).



WORKING-STORAGE SECTION
Defines and describes all data storage fields required for Processing (Including constants).
These fields are not part of any input/output file.





Can be used for storing
Intermediate results
Counters, Flags
Input/Output Records
Tables etc.



Types of data
Variable
Data entered by the user at run-time.

Constant
Data required for processing that is not dependent on the input to the system. There are 3 types of literals.

1. Numeric Literals
A numeric literal is a constant used primarily for arithmetic operations.

Rules for forming numeric literals
a) 1 to 18 digits
b) A + or – sign may be used, but it must appear to the left of the number.
c) A decimal point is permitted within the literal. The decimal point, however may not be the rightmost character of the literal.

Eg 2.5:
+34
-8.6
.008



2. Nonnumeric Literals
A nonnumeric literal is a constant that is used in the PROCEDURE DIVISION for all operations except arithmetic.

Rules for forming nonnumeric literals
a) The literal must be enclosed in quotation marks.
b) A maximum of 120 characters are permitted.
c) Any character permitted in the COBOL character set may be used except the quotation mark.

Eg 2.6:
‘CODE’
‘$ 123’



3. Figurative Constants
A figurative constant is a COBOL reserved word that has special significance to the compiler.

The figurative constants are
ZERO, ZEROS, ZEROES - references the value of zeros
SPACE, SPACES - reference the value of blank
QUOTE, QUOTES -references the quotation mark
-used in nonnumeric literals to include a
quote.
e.g ‘D’Quote’Souza will store the value D’Souza.
LOW-VALUE , LOW-VALUES - references the lowest value in the collating
sequence for the particular computer system.
HIGH-VALUE, HIGH_VALUES - references the highest value in the collating
sequence for the particular computer system.
ALL - references one or more occurrences of the
single character nonnumeric literal.
e.g. MOVE ALL “A” TO HEAD-1, will
result in the field HEAD-1 being filled with A’s.

Special Characters (Numeric Field) :
Implied decimal point
The symbol V denotes an implied decimal point, which does not occupy a storage position. In the code below, AMOUNT occupies 5 positions.

Eg 2.7:
05 AMOUNT PIC 999V99.

Eg 2.8:
PIC Description Value Stored as
99V9 38.50 385
9(4)V99 452.39 045239
999 550 550

Signed numbers
If a numeric field can have negative contents, then it must have an S in its PIC clause. It must be the leftmost character. It does not take any storage space.

Eg 2.9:
02 BALANCE PIC S999.

Edited fields

The purpose of editing is to make data more suitable for human reading. Thus editing in its most common use is associated with printing data on the printer. For example, we may suppress leading zeros, we may use commas to make long numeric values more legible, we may insert a dollar sign in front of a value, etc.

The editing characters are Z * $ - + CR DB . , B 0 /

Z - Suppressing leading zeros


The Z PICTURE character is used to replace leading zeros by blanks and thus performs a function identical to that of the floating . Zero suppression terminates when the first nonzero digit or the . character is encountered, whichever occurs first. When Z’s have been designated for all positions in a field and the value to be inserted in that field is zero, in that case the entire field is blanked. Z’s can appear before as well as after the decimal point. The number of Z’s representing integers in the report-item should be equal to the number of integers or 9’s in the sending field.

Eg 2.10:
PIC Description Value Printed as
Z99 25 b25
ZZZV99 25 b2500
ZZZV99 0.10 bbb10
ZZZVZZ 0.052 bbb05
ZZZVZZ 0.00 bbbbb

* - Check Protection
The * character is referred to as a check-protect character and is normally used to protect dollar amounts written on checks or other negotiable documents. Asterisks are zero-suppression characters that replace each non-significant zero and comma with * instead of a space.

Eg 2.11:
PIC Description Value Printed as
**999 04678 *4678
***99 00052 ***52

$ - Dollar Sign
By use of the $ PICTURE character the dollar sign is written in the position in which it is to appear in the output. Since the $ sign is counted in the size of the field, the field should be assigned at least one more position than the maximum number of significant digits expected. The $ may also be floated, by which we mean that it will be entered to the left of the first significant digit in the field and be preceded by blanks. The $ float stops when wither the first nonzero digit or the . or V is encountered. When the $ sign appears in all positions and the value is zero, the effect is to blank the entire field (including any . and ,).

Eg 2.12:
PIC Description Value Printed as
$999V99 125.13 $12513
$9(5)V99 100.00 $0010000
$$99V99 12.49 b$1249
$$$$9V99 150.10 $15010
$$$$V99 0.15 bbb$15
$$$$V$$ 0.0 bbbbb



. - (Decimal Point)
The . (decimal) PICTURE character indicates the position of the decimal point and serves to align the actual decimal values in the field, only one such character may appear in a field. Further, a field cannot contain both a V and a . PICTURE character. It cannot be the rightmost character. If all the digits before and after the decimal point are zero, the resulting field will be blank.

Eg 2.13:
PIC Description Value Printed as
$9,999.99 2,350.22 $2,350.22
$9,999.99 150.31 $0,150.31
$$,999.99 150.31 bb$150.31
$$,$$$.99 24.40 bbb$25.40
$$,$$$.999 0.019 bbbbb$.019
$$$$$.$$$ 0.0 bbbbbbbbb



, - (Comma)
The comma is placed in the positions in which it is desired. A field may include more than one , (comma) PICTURE character if the size of the field warrants it. A comma will be appropriately suppressed if no significant digit precedes it. It cannot appear as the leftmost or rightmost character.

Eg 2.14:
PIC Description Value Printed as
$9,999.99 2,350.22 $2,350.22
$9,999.99 150.31 $0,150.31
$$,999.99 150.31 bb$150.31
$$,$$$.99 24.40 bbb$25.40
$$,$$$.999 0.019 bbbbb$.019


$$,$$$.$$$ 0.009 bbbbb$.009
$$,$$$.$$$ 0.0 bbbbbbbbbb
$$,$$9.999 2,210.2 $2,210.200
$$,999.9 2,210.2 $2,210.2
$$,999.9 2,210.256 $2,210.2
$9,999.9999 23 $0,023.0000

- (Minus) and + (Plus)
The – PICTURE character prints a minus sign only if the quantity is negative and omits a sign for all other quantities. It can appear as the leftmost or rightmost character. The – PICTURE insertion character differs from the S character in that the use of the S character identifies a field as a signed one for computational purposes, but the sign does not occupy a position. Use of the – PICTURE character leads to a field in which the sign occupies a character position.

The + PICTURE character is used to print wither a plus sign or a minus sign for all values. A + sign will be generated for positive or unsigned quantities, and a – sign will be generated for negative quantities. The sending field should have an $ in its PICTURE clause for it to be interpreted as a signed number.

The + or – PICTURE character can appear as the leftmost or rightmost character. They can also be floated, similar to the $ character. However , the +, -, and $ are mutually exclusive as floating characters. If we want to have both $ float and + or – sign representation, we write the + or – to the right of the field.

Eg 2.15:
PIC Description Value Printed as
+999.9 35.2 +035.2
999.9+ 35.2 035.2+
999.9+ -35.2 035.2-
++9.9 -001.3 b-1.3
+++9.99 .05 bb+0.05
+++9.99 -.05 bb-0.05
++++.++ .01 bbb+.01




----.-- 0.0 bbbbbbb
--99.99 -10.25 b-10.25
--999.99 100.25 b100.25
999.9- -10.2 010.2-

DB/CR
In accounting applications there is often need to identify values that represent debits or credits. The COBOL language facilitates such differentiation by means of the DB (debit) and CR (CR) editing characters. The DB or CR symbol is written only to the right of a field in the PICTURE clause, and in both cases it is represented in storage for the purpose of subsequent output only when the value is negative.



Summary for use of + - DB CR
PIC Storage when Storage when
Character data positive data negative

+ + -
- Blank -
DB Blank DB
CR Blank CR

Eg 2.16:
PIC Description Value Printed as
$999.99DB 135.26 $135.26bb
$999.99DB -135.26 $135.26DB
$,$$99.99CR -10.50 bb$10.50CR

B – (blank)
The B insertion editing character results in blanks being entered in the designated positions.

Eg 2.17:
05 NAME PIC ABABA(10) VALUE ‘RBSMITH’.
NAME = RbBbSMITHbbbbb

0 - (Zero)
The zero insertion character causes zeros to be inserted in the positions in which it appears.

Eg 2.18:
05 AMOUNT PIC 9(4)000 VALUE 1365.
AMOUNT = 1365000

/ - (stroke)
Each / (stroke) in the PICTURE character string represents a character position into which the stroke character will be inserted.

Eg 2.19:
05 PRINT-DATE PIC 99/99/99 VALUE 040798.
PRINT-DATE = 04/07/98


VALUE Clause
In addition to defining storage fields using the PICTURE clause it is often desirable to assign initial values to elementary items in the WORKING-STORAGE SECTION. Such a value may remain unchanged throughout the program, or it may change in the course of program execution. Such initial values are generally not assigned to FILE SECTION items, since such fields either receive their data from the external medium or from other storage location as the result of program execution.

Eg 2.20:
02 PAGE-TITLE PIC A(14) VALUE “SAMPLE PROGRAM”.
02 TAX-RATE PIC V99 VALUE IS 0.03.

Continuation of Literals from one line to the next
A nonnumeric literal may contain up to 120 characters. The same applies to a nonnumeric literal in a VALUE clause. Sometimes a nonnumeric literal may not fit on a single line, hence it is necessary to continue this literal.

Rules for continuation of nonnumeric literals
Begin the literal in the VALUE clause with a quotation mark.
Continue the literal until position 72, the end of the line, is reached. Do not end with a quotation mark on this line.
Place a hyphen on the next line in the position beginning in Area B of the second line. Begin with a quotation mark.
End the literal with a quotation mark.

Eg 2.21:
01 PAGE-HEADING PIC X(36) VALUE “MONTHLY TRANSACTIONS FOR AP
- “RIL 2000”.

USAGE clause
Numeric data in a computer may be represented in one of two basic modes. They may be represented as character data or as numeric data. The arithmetic registers of computers perform arithmetic with numeric data that is in numeric, not character mode. If numeric data is represented in character mode, it must first
be converted to numeric mode before arithmetic computations can be performed.
In COBOL, data in character mode is described in DISPLAY mode, while data in numeric mode is described as being COMPUTATIONAL MODE. All data items are assumed to be in DISPLAY mode unless they are declared to be COMPUTATIONAL. The declaration is done in the DATA DIVISION with the USAGE clause.

Eg 2.22:
02 AMOUNT-1 PIC 99.
02 AMOUNT-2 PIC 99 USAGE DISPLAY.
02 AMOUNT-3 PIC 99 USAGE COMPUTATIONAL.
02 AMOUNT-4 PIC 99 USAGE COMP.

REDEFINES Clause
The REDEFINES clause can be used to allow the same storage location to be referenced by different data-names or to allow a regrouping or different description of the data in a particular storage location.

Restrictions

The REDEFINES clause cannot be used
- at the 01 level in the FILE SECTION.
- when the levels of data-name-1 and data-name-2 are different.
- When the level number is 66 or 88.

Eg 2.23:
01 SAMPLE.
02 RECEIVABLE.
03 CUSTOMER-NUMBER PIC 9(8).
03 CUSTOMER-NAME PIC X(11).
03 AMOUNT PIC 9(4)V99.
02 PAYABLE REDEFINES RECEIVABLE.
03 VENDOR-NUMBER PIC 9(6).
03 VENDOR-NAME PIC X(12).
03 VENDOR-OWED-AMT PIC 9(5)V99.

RENAMES Clause
The RENAMES clause provides the programmer with the capability of regrouping elementary data items. It resembles the REDEFINES clause, except that it can form a new grouping of data items which combines several items. Use of the RENAMES clause is always signaled by the special 66 level number.

Eg 2.24:
01 TAX-RECORD.
02 SOC-SEC-NUMBER PIC X(9).
02 NAME.
03 FIRST-NAME PIC X(10).
03 LAST-NAME PIC X(15).
02 TOTAL-YTD.
03 GROSS-PAY PIC 9(8)V99.
03 NET-PAY PIC 9(8)V99.
03 TAX PIC 9(5)V99.
66 LAST-GROSS RENAMES LAST-NAME THRU NET-PAY.

Qualification of names
A data name that is not unique must be qualified in COBOL verbs.


Eg 2.25:

01 IN-REC
05 NAME PIC X(10).
05 AGE PIC 99.
01 OUT-REC.
05 NAME PIC X(10).
05 B-DATE PIC 9(6).


MOVE NAME OF IN-REC TO NAME OF OUT-REC.

Back to COBOL Index

No comments: