MainframeMaster

COBOL Tutorial

COBOL ALPHANUMERIC-EDITED Data Type

The ALPHANUMERIC-EDITED data type in COBOL represents a sophisticated formatting mechanism for character-based data that requires structured presentation, insertion of special characters, and automatic formatting for display purposes. This data type is essential for creating professional reports, formatted output, user interfaces, and data presentation that meets business requirements for readability, standardization, and visual appeal in enterprise applications.

ALPHANUMERIC-EDITED fields combine the flexibility of alphanumeric data handling with powerful editing capabilities that automatically insert formatting characters such as slashes, hyphens, spaces, and special symbols based on predefined picture patterns. This functionality is particularly valuable for formatting dates, phone numbers, social security numbers, account numbers, and other structured data that requires consistent presentation across different parts of an application or organization.

Understanding ALPHANUMERIC-EDITED

ALPHANUMERIC-EDITED data type differs from standard ALPHANUMERIC in that it includes editing symbols in the PICTURE clause that define how data should be formatted when displayed or moved to the field. The editing characters are automatically inserted by the COBOL runtime system based on the pattern specified in the PICTURE clause, creating formatted output without requiring additional programming logic.

This data type is particularly powerful because it separates data storage from data presentation, allowing the same underlying data to be formatted differently for various purposes. For example, a date stored as "20240315" can be automatically formatted as "03/15/2024", "15-Mar-2024", or "March 15, 2024" depending on the PICTURE clause used in the receiving ALPHANUMERIC-EDITED field.

Key Characteristics:

  • Automatic Formatting: Inserts editing characters automatically based on PICTURE pattern
  • Data Presentation: Optimized for display and output formatting rather than computation
  • Pattern-Based: Uses PICTURE symbols to define formatting rules and structure
  • Read-Only Nature: Typically used for output formatting rather than input processing
  • Business Friendly: Creates human-readable formats that meet business presentation standards

PICTURE Clause Editing Symbols

Common Editing Symbols

SymbolPurposeExampleResult
XAlphanumeric character positionXXX-XX-XXXX123-45-6789
9Numeric character position99/99/999903/15/2024
AAlphabetic character positionAAA-XXXABC-123
/Insert slash characterXX/XX/XX01/15/24
-Insert hyphen characterXXX-XX-XXXX555-12-3456
BInsert blank spaceXXXBXXXBXXXXABC DEF GHIJ
0Insert zero characterXX0XX0XXXXAB0CD0EFGH

Practical Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
DATA DIVISION. WORKING-STORAGE SECTION. *> Date formatting examples 01 RAW-DATE PIC 9(8) VALUE 20240315. 01 FORMATTED-DATE-1 PIC 99/99/9999. 01 FORMATTED-DATE-2 PIC XX-XXX-XXXX. 01 FORMATTED-DATE-3 PIC X(2)BX(3)BX(4). *> Phone number formatting 01 RAW-PHONE PIC 9(10) VALUE 5551234567. 01 FORMATTED-PHONE-1 PIC (999)B999-9999. 01 FORMATTED-PHONE-2 PIC 999-999-9999. 01 FORMATTED-PHONE-3 PIC 999.999.9999. *> Social Security Number formatting 01 RAW-SSN PIC 9(9) VALUE 123456789. 01 FORMATTED-SSN PIC 999-99-9999. *> Account number formatting 01 RAW-ACCOUNT PIC X(12) VALUE "ACCT12345678". 01 FORMATTED-ACCOUNT PIC XXXX-XXXX-XXXX. *> Currency and financial formatting 01 RAW-AMOUNT PIC 9(7)V99 VALUE 1234567.89. 01 FORMATTED-AMOUNT PIC $9,999,999.99. PROCEDURE DIVISION. DEMONSTRATE-FORMATTING. *> Date formatting demonstrations MOVE RAW-DATE TO FORMATTED-DATE-1. DISPLAY "Standard Date: " FORMATTED-DATE-1. *> Result: 03/15/2024 MOVE "15MAR2024" TO FORMATTED-DATE-2. DISPLAY "Alternate Date: " FORMATTED-DATE-2. *> Result: 15-MAR-2024 MOVE "15 MAR 2024" TO FORMATTED-DATE-3. DISPLAY "Spaced Date: " FORMATTED-DATE-3. *> Result: 15 MAR 2024 *> Phone number formatting MOVE RAW-PHONE TO FORMATTED-PHONE-1. DISPLAY "Phone Format 1: " FORMATTED-PHONE-1. *> Result: (555) 123-4567 MOVE RAW-PHONE TO FORMATTED-PHONE-2. DISPLAY "Phone Format 2: " FORMATTED-PHONE-2. *> Result: 555-123-4567 MOVE RAW-PHONE TO FORMATTED-PHONE-3. DISPLAY "Phone Format 3: " FORMATTED-PHONE-3. *> Result: 555.123.4567 *> SSN formatting MOVE RAW-SSN TO FORMATTED-SSN. DISPLAY "SSN: " FORMATTED-SSN. *> Result: 123-45-6789 *> Account formatting MOVE RAW-ACCOUNT TO FORMATTED-ACCOUNT. DISPLAY "Account: " FORMATTED-ACCOUNT. *> Result: ACCT-1234-5678

Advanced Formatting Techniques

Complex Pattern Matching

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
DATA DIVISION. WORKING-STORAGE SECTION. *> Complex formatting patterns 01 CREDIT-CARD-DATA. 05 RAW-CC-NUMBER PIC 9(16) VALUE 1234567890123456. 05 FORMATTED-CC PIC 9999-9999-9999-9999. 05 MASKED-CC PIC XXXX-XXXX-XXXX-9999. 01 EMPLOYEE-DATA. 05 EMP-ID-RAW PIC 9(8) VALUE 12345678. 05 EMP-ID-FORMATTED PIC XX-XX-XXXX. 05 EMP-NAME-RAW PIC X(20) VALUE "JOHNSMITH". 05 EMP-NAME-FORMATTED PIC X(4)BX(16). 01 FINANCIAL-DATA. 05 BUDGET-AMOUNT PIC 9(10)V99 VALUE 1234567890.12. 05 BUDGET-FORMATTED PIC $999,999,999.99. 05 PERCENTAGE PIC 99V99 VALUE 15.75. 05 PERCENT-FORMATTED PIC 99.99%. PROCEDURE DIVISION. ADVANCED-FORMATTING-DEMO. *> Credit card formatting MOVE RAW-CC-NUMBER TO FORMATTED-CC. DISPLAY "Full CC: " FORMATTED-CC. *> Create masked version for security MOVE "****-****-****-" TO MASKED-CC (1:15). MOVE FORMATTED-CC (16:4) TO MASKED-CC (16:4). DISPLAY "Masked CC: " MASKED-CC. *> Employee ID formatting MOVE EMP-ID-RAW TO EMP-ID-FORMATTED. DISPLAY "Employee ID: " EMP-ID-FORMATTED. *> Employee name with space MOVE "JOHN SMITH" TO EMP-NAME-FORMATTED. DISPLAY "Employee: " EMP-NAME-FORMATTED. *> Financial formatting MOVE BUDGET-AMOUNT TO BUDGET-FORMATTED. DISPLAY "Budget: " BUDGET-FORMATTED. MOVE PERCENTAGE TO PERCENT-FORMATTED. DISPLAY "Rate: " PERCENT-FORMATTED.

Report Formatting Applications

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
DATA DIVISION. WORKING-STORAGE SECTION. 01 REPORT-LINE-FORMATS. *> Header formatting 05 REPORT-TITLE PIC X(50) VALUE "MONTHLY SALES REPORT". 05 REPORT-DATE PIC X(20). 05 PAGE-NUMBER PIC ZZ9. 05 PAGE-HEADER PIC X(20) VALUE "PAGE: ". *> Detail line formatting 05 DETAIL-LINES. 10 CUSTOMER-CODE PIC XXX-XXXX. 10 CUSTOMER-NAME PIC X(25). 10 SALES-AMOUNT PIC $ZZZ,ZZ9.99. 10 COMMISSION PIC ZZ9.99%. 10 REGION-CODE PIC XX-XX. 01 RAW-DATA. 05 RAW-CUST-CODE PIC X(7) VALUE "ABC1234". 05 RAW-CUST-NAME PIC X(25) VALUE "ACME CORPORATION". 05 RAW-SALES PIC 9(7)V99 VALUE 125000.50. 05 RAW-COMM-RATE PIC 99V99 VALUE 05.25. 05 RAW-REGION PIC X(4) VALUE "NE01". PROCEDURE DIVISION. GENERATE-FORMATTED-REPORT. *> Format current date for header ACCEPT WS-CURRENT-DATE FROM DATE YYYYMMDD. MOVE WS-CURRENT-DATE TO REPORT-DATE. *> Format page number MOVE 1 TO PAGE-NUMBER. *> Display formatted header DISPLAY REPORT-TITLE. DISPLAY "Date: " REPORT-DATE. DISPLAY PAGE-HEADER PAGE-NUMBER. DISPLAY " ". *> Format detail data MOVE RAW-CUST-CODE TO CUSTOMER-CODE. MOVE RAW-CUST-NAME TO CUSTOMER-NAME. MOVE RAW-SALES TO SALES-AMOUNT. MOVE RAW-COMM-RATE TO COMMISSION. MOVE RAW-REGION TO REGION-CODE. *> Display formatted detail line DISPLAY CUSTOMER-CODE " " CUSTOMER-NAME " " SALES-AMOUNT " " COMMISSION " " REGION-CODE.

Data Validation and Error Handling

Input Validation for Formatted Fields

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
DATA DIVISION. WORKING-STORAGE SECTION. 01 VALIDATION-FIELDS. 05 INPUT-DATE PIC X(10). 05 FORMATTED-DATE PIC 99/99/9999. 05 VALIDATION-FLAGS. 10 DATE-VALID PIC X VALUE 'N'. 10 FORMAT-VALID PIC X VALUE 'N'. 10 RANGE-VALID PIC X VALUE 'N'. 01 ERROR-MESSAGES. 05 ERR-INVALID-FORMAT PIC X(50) VALUE "ERROR: Invalid date format. Use MM/DD/YYYY". 05 ERR-INVALID-DATE PIC X(50) VALUE "ERROR: Invalid date value". 05 ERR-OUT-OF-RANGE PIC X(50) VALUE "ERROR: Date out of acceptable range". PROCEDURE DIVISION. VALIDATE-FORMATTED-INPUT. *> Initialize validation flags MOVE 'N' TO DATE-VALID, FORMAT-VALID, RANGE-VALID. *> Accept user input DISPLAY "Enter date (MM/DD/YYYY): " WITH NO ADVANCING. ACCEPT INPUT-DATE. *> Validate format pattern IF INPUT-DATE (3:1) = "/" AND INPUT-DATE (6:1) = "/" AND INPUT-DATE (1:2) NUMERIC AND INPUT-DATE (4:2) NUMERIC AND INPUT-DATE (7:4) NUMERIC MOVE 'Y' TO FORMAT-VALID ELSE DISPLAY ERR-INVALID-FORMAT GOBACK END-IF. *> If format is valid, validate date values IF FORMAT-VALID = 'Y' PERFORM CHECK-DATE-VALUES IF DATE-VALID = 'Y' MOVE INPUT-DATE TO FORMATTED-DATE DISPLAY "Formatted date: " FORMATTED-DATE END-IF END-IF. CHECK-DATE-VALUES. *> Extract and validate month IF INPUT-DATE (1:2) >= "01" AND INPUT-DATE (1:2) <= "12" CONTINUE ELSE DISPLAY ERR-INVALID-DATE EXIT PARAGRAPH END-IF. *> Extract and validate day IF INPUT-DATE (4:2) >= "01" AND INPUT-DATE (4:2) <= "31" CONTINUE ELSE DISPLAY ERR-INVALID-DATE EXIT PARAGRAPH END-IF. *> Extract and validate year IF INPUT-DATE (7:4) >= "1900" AND INPUT-DATE (7:4) <= "2100" MOVE 'Y' TO DATE-VALID ELSE DISPLAY ERR-OUT-OF-RANGE END-IF.

Performance and Best Practices

Optimization Guidelines

ALPHANUMERIC-EDITED fields should be used primarily for output formatting rather than computational operations. Store data in its raw format and format only when needed for display or output to minimize processing overhead.

Consider creating reusable COPY members for common formatting patterns to ensure consistency across applications and reduce maintenance effort.

For high-volume processing, evaluate whether formatting can be deferred to presentation layers or performed in batch rather than real-time to optimize performance.

Frequently Asked Questions

Q: Can ALPHANUMERIC-EDITED fields be used in calculations?

No, ALPHANUMERIC-EDITED fields are designed for presentation and cannot be used directly in arithmetic operations. Use them for output formatting only.

Q: How do I handle variable-length input with fixed editing patterns?

Use separate validation and formatting steps. Validate the input first, then move to an appropriately sized ALPHANUMERIC-EDITED field for formatting.

Q: Can I modify the contents of an ALPHANUMERIC-EDITED field after formatting?

While technically possible, it's not recommended. The editing characters become part of the field content, making subsequent processing complex and error-prone.

Practice Exercises

Exercise 1: Phone Number Formatter

Create a program that accepts a 10-digit phone number and formats it in three different ways: (xxx) xxx-xxxx, xxx-xxx-xxxx, and xxx.xxx.xxxx.

Exercise 2: Date Format Converter

Write a program that converts dates between different formats: YYYYMMDD to MM/DD/YYYY, DD-MON-YYYY, and MON DD, YYYY.

Exercise 3: Financial Report Formatter

Create a formatted financial report with currency symbols, comma separators, and percentage displays using ALPHANUMERIC-EDITED fields.