The COBOL-85 Tutorial, Chapter 2

Data definitions in COBOL, part 1.

Copyright © 1998-2017

Kim Kjærsulf.


This chapter (TOC)

Variable names.
An introduction to level numbers.
An introduction to data types.
Symbolic constant name.
A more complete example.
REDEFINES.
Exercises

Variable names.

[This chapter] [TOC] [Tutorial]


Variables is declared in the working-storage section of the data division. Most of the rules apply equally when declaring records in the file section and in the linkage section.

Each entry in a DATA DIVISION starts with a level number, followed by a variable name, followed by a description of the data type, e.g.

	77  CUSTOMER-NAME    PIC X(30).
The variable is called CUSTOMER-NAME and is an alphanumeric field containing 30 characters. Note the hyphen in the name: COBOL does not usually allow you to use an underline "_". An alphanumeric variable will always contain the number of characters it has been defined with, i.e. not the actual length of the variable as in Pascal or C++. Level number 77 here means that the variable is not connected to other variables.


An introduction to level numbers.

[This chapter] [TOC] [Tutorial]


A level number is placed in front of every variable. The level number is used to specify the variables relation to surrounding variables.

Numbers 1-49 are used when defining records and will be dealt with in a later chapter: "Data definitions in COBOL (2)".

All subsequent numbers are to be placed in Column A and are used to define variables that are not connected to other variables.
66 Used when redefining the data type of a prior variable.

77 Variable.

78 A Micro Focus speciality. The same as 77, but with the added element that the field is a named constant which cannot alter its value while the program is being run.

88 Used to define symbolic conditions. More on this later.



An introduction to data types.

[This chapter] [TOC] [Tutorial]


See the manual for actual selection of data types. However, the following are sure to be among the options available:

X(n)
An alphanumeric field with space for n characters.

S9(n1)V9(n2)
A calculation field with sign, space for n1 characters before the decimal point and n2 characters after. Note that the field does not contain a decimal point. The decimal point is represented by the "V".

--.---.--9,99
--.---.--9vbb99
A numeric edited field which fills 13 print positions.

The "V" represents the position of the decimal point here too (for example, on giro payment slips). "b" is used to indicate space characters in the output. If the figure is negative there will be a minus sign in front of the first significant digit; leading zeros are replaced by blanks. Commas for thousands, etc., will only be displayed if the figure is larger than (+/-)999 or (+/-)999999.

After this PICTURE mask, it is possible to add an internal format, e.g. DISPLAY or COMP. Alphanumeric fields or numeric-edited fields always use the DISPLAY format.

An example:

	DATA DIVISION.
	WORKING-STORAGE SECTION.
	77  ENTERED-VALUE	  PIC 999.
	77  INTERNAL-VALUE  PIC S999 COMP.
	..

	PROCEDURE DIVISION.
	...
	     MOVE ENTERED-VALUE TO INTERNAL-VALUE.
	...
An explanation of the example:

The two variables are, respectively, a text field (that may only contain three-character figures) and a binary numeric variable (value set: -999 to 999). The MOVE statement copies the contents of ENTERED-VALUE to INTERNAL-VALUE; at the same time the contents are converted from ASCII to binary.



Symbolic constant name.

[This chapter] [TOC] [Tutorial]


COBOL contains a number of constants with well-defined names. These constants are "intelligent", inasmuch as the compiler assigns them an optimal size and data type, depending on the situation in which they are to be used.

ZERO(S)(ES)
One or more zeroes, either as an ASCII string (or EBCDIC) or a numerical zero - depending how it is used.
SPACE(S)
A series of blank spaces.
HIGH-VALUE(S)
The highest value in the code page.
LOW-VALUE(S)
The lowest value in the code page.
QUOTE(S)
Inverted commas. Used so as not to confuse the '"' sign in the syntax.
ALL literal
Same as SPACE, but here the programmer decides which character is to be used as SPACE.

Example 1.

		77   ADDRESS	PIC X(25) VALUE SPACE.
The variable ADDRESS may contain 25 characters which are immediately initiated as being blank characters.

Example 2.

77  INTERNAL-VALUE	PIC 999 COMP.
		...

		MOVE ZERO TO INTERNAL-VALUE.
The variable INTERNAL-VALUE is a binary field that may contain values within the interval 0-999. The compiler ensures that it creates the optimal constant. Another alternative is:

		MOVE "0" TO INTERNAL-VALUE
but this creates a character constant with the value "0", which therefore has to be converted to a binary format every time the instruction is carried out.



A more complete example.

[This chapter] [TOC] [Tutorial]


This is a ready to compile example showing the declaration of some different variables.

000000* A sample program showing the use of some different variables
       identification division.
      *  ------------ identifying the program.
       program-id.    variables.
       author.        kik.
       environment division.
      *  ------------ program environment
       configuration section.
       special-names.
           console is crt
           decimal-point is comma.
      *  ------------ Variables
       data division.
       working-storage section.
      * Level number
      * |  Name
      * |  |               Picture mask
      * |  |               |                 Internal format
      * |  |               |                 |    Initial value
      * |  |               |                 |    |
       77  enter-field     pic 9999.
       77  calc-field      pic s9999         comp value zero.
       77  customer-name   pic x(30)              value space.
       77  price           pic s9(6)v99      comp value zero.
       77  ed-price        pic ----.--9,99.
       77  bar             pic x(80)              value all "--*".

       procedure division.
      *  ------------ The instructions
       main section.
           display space.

           display bar line 3 position 1.
           display "Enter a figure        :" line 5 position 10.
           accept enter-field line 5 position 34.
           display "Enter a customer name :" line 6 position 10.
           accept customer-name line 6 position 34.

           if enter-field numeric then 
              move enter-field to calc-field.
           add calc-field to price.
           move price to ed-price.
           display "price : " line 7 position 10.
           display ed-price line 7.

           exit program.
The above program creates this screen during execution:-
--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--

         Enter a figure        : 1000
         Enter a customer name : Kim Kjærsulf
         price :    1.000,00


REDEFINES.

[This chapter] [TOC] [Tutorial]


REDEFINES make it possible to set up different definitions of the same variable - typically a record. The facility is best illustrated via an example:

	01  cprnumber   pic x(10).
	01  cpr-data redefines cprnumber.
	    03  day     pic 99.
	    03  mth     pic 99.
	    03  yr      pic 99.
	    03  cpr-no.
	        05  lbno    pic 999.
	        05  check   pic 9.
Here it is possible to refer to a CPR number (Danish civil registration number) as a text string, to each element in the number as figures, and finally, to the last four characters as text.


Exercise 1.

[This chapter] [TOC] [Tutorial]


Enter, compile and run the examples in this chapter.


Copyright © 1998-2017

Kim Kjærsulf


Last Updated october 3, 2003
For more information contact: Kim Kjærsulf