Alpha-numeric Data Type


  • An alpha-numeric data type is used to declare the variables for processing alphanumeric strings.
  • It allows strings that are a combination of characters from "A" to "Z" or "a" to "z" characters or 0 to 9 numbers and other allowed special characters.
  • It uses the character "X" as a PICTURE symbol to declare variables.
  • Each character in the string has to be counted to calculate the length, which should be coded after the PICTURE character "X". For Example - Declaring a variable to store a 3-character string should have the declaration as XXX or X(3).
  • An alpha-numeric variable can store maximum of 255 characters.

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 - 9Numeric 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 the above example, it is WS-VAR.
  • var-length - specifies the length of the string to store in the variable. The maximum length is 255 characters. From the above example, it is 11.
  • input-string - specifies the string assigned to the variable during the declaration. From the above example, it is MAINFRAMES1.

Rules to Remember -

  • It should not code with any other data type.
  • USAGE DISPLAY is applicable for alphabetic data types. If no USAGE clause is coded, DISPLAY is applied. i.e., 1 character = 1 byte.

Alignment | Justification -


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

Practical Example -


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

Code -

----+----1----+----2----+----3----+----4----+----5----+
       ...
       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).
       ...
       PROCEDURE DIVISION.
           MOVE "MAINFRAME SYSTEMS"   TO  WS-ALPN-SVAR 
                                          WS-ALPN-LVAR.

           DISPLAY "WS-ALPN-SVAR (length - 10): -" WS-ALPN-SVAR "-".
           DISPLAY "WS-ALPN-LVAR (length - 20): -" WS-ALPN-LVAR "-".
		   ...

Output -

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