Alpha-numeric Data Type


An alphanumeric data type is used to declare the variables for processing the alphanumeric strings. Alphanumeric strings are the combination of "A" to "Z" or "a" to "z" characters or 0 to 9 numbers and other allowed special characters.

Alphanumeric data type uses character "X" with PICTURE clause to declare alphanumeric variables. 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 3-character string should have the declaration as XXX or X(3).

The list of allowed characters is as follows -

Character(s) Description
Space
+Plus sign
-Minus sign or hyphen
*Asterisk
/Forward slash or solidus
=Equal sign
$Currency sign
,Comma
;Semicolon
.Decimal point or period
"Quotation mark
Character(s) Description
'Apostrophe
(Left parenthesis
)Right parenthesis
>Greater than
<Less than
:Colon
_Underscore
A - ZAlphabet (uppercase)
a - zAlphabet (lowercase)
0 - 0Numeric values

Syntax -

----+----1----+----2----+----3----+----4----+----5----+
       01 ws-variable      PIC X(var-length) 
	                      [VALUE "input-string"].
Note! All statements coded in [ ] are optional.

Example -

01 WS-VAR      PIC X(11) VALUE "MAINFRAMES1".
  • ws-variable - specifies the variable name. From example, it is WS-VAR.
  • var-length - specifies the length of the string to store in the variable. The maximum length is 256 characters. From example, it is 11.
  • input-string - specifies the string assigned to the variable during the declaration. From example, it is MAINFRAMES1.

Rules -

  • Alphanumeric data type should not be coded with any other data types.
  • USAGE DISPLAY is only applicable for alphanumeric data type. If no USAGE clause coded during the declaration, DISPLAY is applied. i.e., 1 character = 1 byte.

Alignment | Justification -


  • The alphanumeric data type is left justified by default, and the data in all alphanumeric variables are left justified automatically.
  • The rightmost characters are truncated if the input data is larger than the variable size. For example – the variable declared with X(4) and the input data is "MAINFRAME1", the variable contains only "MAIN".
  • If the input data is smaller than the receiving variable, the unused character positions at the right are filled with spaces.
  • JUSTIFIED | JUST overrides the default justification of alphanumeric data type. Refer JUSTIFIED Clause for more information.

Practical Example -


Scenario - Example describes the alphanumeric variable declaration, justification, and truncation.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. ALPHANDT.
       AUTHOR. MTH.
 
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
	  * Variable with shorter length than the passing input data
	      05 WS-ALPN-SVAR  PIC X(10).
	  * Variable with larger length than the passing input data
          05 WS-ALPN-LVAR  PIC X(20).
	  * Variable with larger length with JUSTIFIED clause
          05 WS-ALPN-RVAR  PIC X(20) JUSTIFIED RIGHT.

       PROCEDURE DIVISION.
           MOVE "MAINFRAME SYSTEMS"   TO  WS-ALPN-SVAR 
                                          WS-ALPN-LVAR 
                                          WS-ALPN-RVAR.

           DISPLAY "WS-ALPN-SVAR: -" WS-ALPN-SVAR "-".
           DISPLAY "WS-ALPN-LVAR: -" WS-ALPN-LVAR "-".
           DISPLAY "WS-ALPN-RVAR: -" WS-ALPN-RVAR "-".

           STOP RUN.

Output -

WS-ALPN-SVAR:  -MAINFRAME -
WS-ALPN-LVAR:  -MAINFRAME SYSTEMS   -
WS-ALPN-RVAR:  -   MAINFRAME SYSTEMS-
Note! In the above example, '-' is used to show the boundaries of the data.