Data Types


A data type defines the kind of data a variable can hold, such as numeric values, alphabetic characters, or alphanumeric strings. It specifies the nature of the data (e.g., numbers or letters), its format (e.g., decimal places or date format), and the operations that can be performed on it. Data types are coded using the PICTURE (PIC) clause in the data description entry.

There are five data types in COBOL and those are -

  • Alphabetic
  • Alpha-numeric
  • Numeric
  • Sign
  • Decimal Point

Alphabetic Data Type


The alphabetic data type is used to declare the variables for processing the alphabetic strings.

  • PICTURE Symbol - A
  • Allowed Characters - Space * + - / = $ Comma(,) ; Decimal point(.) " ' ( ) > < : _ A-Z a-z

Each character in the string has to be counted and the length should be coded followed by PICTURE character "A". For Example - Declaring a variable to store 2-character string should have the declaration as AA or A(2).

 01 WS-ALP     PIC A(02).

Alpha-numeric Data Type


An alphanumeric data type is used to declare the variables for processing the alphanumeric strings.

  • PICTURE Symbol - X
  • Allowed Characters - Space * + - / = $ Comma(,) ; Decimal point(.) " ' ( ) > < : _ A-Z a-z 0-9

Each character in the string has to be counted and should be part of the length followed by the data type character "X". For Example - Declaring a variable to store a 5-character string should have the declaration as XXXXX or X(5).

 01 WS-ALPN     PIC X(5).

Numeric Data Type


Numeric data type is used to declare the variables to store the numeric decimal values.

  • PICTURE Symbol - 9
  • Allowed Characters - 0 to 9

Each digit in the number should have been counted and and the length should be coded followed by PICTURE character 9. For Example - Declaring a variable to store a 3-digit value should have the declaration as 999 or 9(3).

 01 WS-NUM     PIC 9(3).

Sign Data Type


Sign data type is used to declare the numeric variable with the sign to capture the negative values. i.e., sign data type always comes up with numeric data type.

  • PICTURE Symbol - S
  • Allowed Characters - - (minus) or + (plus)

Sign value doesn't require any additional byte to store, and it always overpunches on the last digit of the value if the SIGN clause is not coded. For Example - Declaring a variable with sign.

 01 WS-SALARY   PIC S9(5) VALUE +10000.

Output -

WS-SALARY: 1000{

The SIGN clause is used with SIGN data type to set the sign to be stored in an additional byte. For Example - Declaring two variables with sign separate using SIGN clause.

 01 WS-SIGN-LS-P      PIC S9(03) VALUE +256
        SIGN IS LEADING SEPARATE CHARACTER. 
 01 WS-SIGN-TS-P      PIC S9(03) VALUE +256
        SIGN IS TRAILING SEPARATE CHARACTER. 

Output -

WS-SIGN-LS-P: +256
WS-SIGN-TS-P: 256+

Decimal point Data Type


When an input to the program is decimal, a variable should be declared with a decimal point to handle it. Decimal point declares only with the combination of numeric data type. The Decimal point declarations are two types -

Real Decimal Point | Dot -

Dot | period (.) is used with the decimal variable declaration to display the decimal value along with dot. The Dot is counted as part of the variable length. For Example - Declaring a variable with real decimal to store the decimal value 1234.55

01 WS-DECIMAL-VAL    PIC 9(4).9(2) VALUE 1234.55.

WS-DECIMAL-VALUE contains 1234.55. When we display WS-DECIMAL-VAL, 1234.55 gets displayed and there is no change in the display.

Assumed Decimal Point | Implied Decimal Point - "V" is used with a decimal variable declaration to use the decimal value in arithmetic calculations. The "V" is not part of the data and do not count in the variable length. For Example - Declaring a variable with assumed decimal to store the decimal value 1234.55

01 WS-DECIMAL-VAL      PIC 9(4)V9(2) VALUE 1234.55.

WS-DECIMAL-VALUE contains 1234.55. When we display WS-DECIMAL-VAL, it displays as 123455 and the decimal point ignores in display. However, the value in the WS-DECIMAL-VAL is 1234.55.

Data Type Justifications -


By default, numeric data is right-justified, while alphabetic and alphanumeric data is left-justified. JUSTIFIED clause is used to override the default alignment for Alphabetic and Alphanumeric data types. For Example - Declaring two variables. one with JUSTIFIED clause and one without.

       ...
       DATA DIVISION. 
       WORKING-STORAGE SECTION.
       01  WS-VAR.
           05 ALPHA-J2          PIC A(50).
		   05 ALPHA-JR          PIC A(50) JUSTIFIED RIGHT.

       PROCEDURE DIVISION.
           MOVE "MAINFRAMES IS LEGACY SYSTEM"   TO ALPHA-J2
												   ALPHA-JR.
           DISPLAY 'ALPHA-J2:  -' ALPHA-J2 '-'.
		   DISPLAY 'ALPHA-JR:  -' ALPHA-JR '-'.

Output -

ALPHA-J2:  -MAINFRAMES IS LEGACY SYSTEM                       -
ALPHA-JR:  -                       MAINFRAMES IS LEGACY SYSTEM-