MainframeMaster

COBOL Tutorial

COBOL AS Keyword

The AS keyword in COBOL serves as a powerful mechanism for creating alternative character representations, symbolic constants, and literal definitions that enhance code readability, maintainability, and portability. This keyword is primarily used in conjunction with VALUE clauses, symbolic constants definitions, and character set specifications to provide meaningful names for commonly used values, special characters, and system-dependent literals throughout COBOL programs.

Understanding the AS keyword is essential for writing maintainable COBOL code that can adapt to different environments, character sets, and business requirements without requiring extensive modifications. The AS keyword enables developers to create more readable and self-documenting code by replacing cryptic character codes or frequently used literal values with meaningful symbolic names that clearly indicate their purpose and usage within the application context.

Understanding the AS Keyword

The AS keyword provides a way to create symbolic references to literal values, making code more readable and maintainable. When used with VALUE clauses, it allows developers to define meaningful names for constants that would otherwise be represented as cryptic numeric codes, special characters, or complex literal strings.

This functionality is particularly valuable when dealing with special characters, control codes, or business-specific constants that appear multiple times throughout a program. By using the AS keyword to create symbolic names, developers can ensure consistency, improve code readability, and make future maintenance tasks more straightforward.

Primary Uses of AS:

  • Symbolic Constants: Create meaningful names for frequently used literal values
  • Character Definitions: Define alternative representations for special characters
  • System Dependencies: Abstract system-specific values behind portable names
  • Business Constants: Define business rule constants with descriptive names
  • Code Readability: Replace numeric codes with self-documenting identifiers

AS Keyword Syntax and Usage

Basic Syntax Structure

cobol
1
2
3
4
5
6
7
8
*> Basic AS syntax for symbolic constants 01 CONSTANT-NAME PIC X(n) VALUE literal AS "symbolic-name". *> Character definition with AS 01 CHAR-CONSTANT PIC X VALUE X"hex-value" AS "character-name". *> Numeric constant with AS 01 NUMERIC-CONSTANT PIC 9(n) VALUE numeric-value AS "constant-name".

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
DATA DIVISION. WORKING-STORAGE SECTION. *> Character constants with AS 01 SPECIAL-CHARACTERS. 05 TAB-CHAR PIC X VALUE X"09" AS "TAB". 05 NEWLINE-CHAR PIC X VALUE X"0A" AS "LF". 05 CARRIAGE-RETURN PIC X VALUE X"0D" AS "CR". 05 ESCAPE-CHAR PIC X VALUE X"1B" AS "ESC". 05 SPACE-CHAR PIC X VALUE " " AS "SPACE". *> Business constants with AS 01 BUSINESS-CONSTANTS. 05 ACTIVE-STATUS PIC X VALUE "A" AS "ACTIVE". 05 INACTIVE-STATUS PIC X VALUE "I" AS "INACTIVE". 05 PENDING-STATUS PIC X VALUE "P" AS "PENDING". 05 MAX-RECORDS PIC 9(5) VALUE 99999 AS "MAX-REC". 05 MIN-BALANCE PIC 9(7)V99 VALUE 100.00 AS "MIN-BAL". *> File operation constants 01 FILE-OPERATIONS. 05 READ-MODE PIC X VALUE "R" AS "READ". 05 WRITE-MODE PIC X VALUE "W" AS "WRITE". 05 APPEND-MODE PIC X VALUE "A" AS "APPEND". 05 UPDATE-MODE PIC X VALUE "U" AS "UPDATE". PROCEDURE DIVISION. DEMONSTRATE-AS-USAGE. *> Using symbolic names instead of literal values IF CUSTOMER-STATUS = ACTIVE-STATUS DISPLAY "Customer is active" END-IF. IF ACCOUNT-BALANCE < MIN-BALANCE DISPLAY "Balance below minimum: " MIN-BALANCE END-IF. *> File operations with meaningful names MOVE READ-MODE TO FILE-ACCESS-TYPE. PERFORM OPEN-FILE-FOR-PROCESSING. *> Character manipulation with named constants STRING CUSTOMER-NAME DELIMITED BY SIZE TAB-CHAR DELIMITED BY SIZE CUSTOMER-ID DELIMITED BY SIZE INTO OUTPUT-RECORD END-STRING.

Advanced AS Applications

Environment-Specific Constants

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
DATA DIVISION. WORKING-STORAGE SECTION. *> Platform-specific character definitions 01 PLATFORM-CHARS. 05 LINE-TERMINATOR PIC XX VALUE X"0D0A" AS "CRLF". *> Windows *> 05 LINE-TERMINATOR PIC X VALUE X"0A" AS "LF". *> Unix/Linux *> 05 LINE-TERMINATOR PIC X VALUE X"0D" AS "CR". *> Classic Mac *> Database-specific constants 01 DATABASE-CONSTANTS. 05 NULL-INDICATOR PIC X VALUE "?" AS "NULL". 05 COMMIT-POINT PIC 9(4) VALUE 1000 AS "COMMIT-SIZE". 05 TIMEOUT-VALUE PIC 9(3) VALUE 300 AS "DB-TIMEOUT". *> Application-specific codes 01 ERROR-CODES. 05 SUCCESS-CODE PIC 99 VALUE 00 AS "SUCCESS". 05 WARNING-CODE PIC 99 VALUE 04 AS "WARNING". 05 ERROR-CODE PIC 99 VALUE 08 AS "ERROR". 05 SEVERE-CODE PIC 99 VALUE 12 AS "SEVERE". PROCEDURE DIVISION. PROCESS-WITH-CONSTANTS. *> Using environment-specific line terminator STRING REPORT-LINE DELIMITED BY SIZE LINE-TERMINATOR DELIMITED BY SIZE INTO OUTPUT-BUFFER END-STRING. *> Database processing with named constants IF RECORD-COUNT >= COMMIT-POINT EXEC SQL COMMIT END-EXEC MOVE ZERO TO RECORD-COUNT END-IF. *> Error handling with meaningful codes IF RETURN-CODE = SUCCESS-CODE CONTINUE ELSE IF RETURN-CODE = WARNING-CODE DISPLAY "Warning occurred: " WARNING-MESSAGE ELSE DISPLAY "Error code: " RETURN-CODE PERFORM ERROR-HANDLING END-IF END-IF.

Configuration Management

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. *> Configuration constants using AS 01 SYSTEM-CONFIG. 05 DEBUG-MODE PIC X VALUE "Y" AS "DEBUG-ON". 05 PRODUCTION-MODE PIC X VALUE "N" AS "DEBUG-OFF". 05 TRACE-LEVEL PIC 9 VALUE 2 AS "NORMAL-TRACE". 05 MAX-ERRORS PIC 99 VALUE 10 AS "ERROR-LIMIT". *> Report formatting constants 01 REPORT-CONFIG. 05 PAGE-SIZE PIC 99 VALUE 60 AS "LINES-PER-PAGE". 05 LINE-WIDTH PIC 99 VALUE 132 AS "CHARS-PER-LINE". 05 HEADER-LINES PIC 9 VALUE 5 AS "HEADER-SIZE". 05 FOOTER-LINES PIC 9 VALUE 3 AS "FOOTER-SIZE". *> Processing limits 01 PROCESSING-LIMITS. 05 BATCH-SIZE PIC 9(5) VALUE 5000 AS "BATCH-LIMIT". 05 MEMORY-LIMIT PIC 9(8) VALUE 50000000 AS "MAX-MEMORY". 05 TIME-LIMIT PIC 9(4) VALUE 3600 AS "MAX-SECONDS". PROCEDURE DIVISION. CONFIGURATION-DEMO. *> Debug mode processing IF CURRENT-MODE = DEBUG-MODE MOVE NORMAL-TRACE TO CURRENT-TRACE-LEVEL DISPLAY "Debug mode activated" END-IF. *> Report formatting IF LINE-COUNTER >= PAGE-SIZE PERFORM PRINT-PAGE-FOOTER PERFORM PRINT-PAGE-HEADER MOVE HEADER-SIZE TO LINE-COUNTER END-IF. *> Batch processing control PERFORM VARYING RECORD-NUM FROM 1 BY 1 UNTIL RECORD-NUM > BATCH-SIZE OR END-OF-FILE = "Y" PERFORM PROCESS-RECORD IF ERROR-COUNT >= ERROR-LIMIT DISPLAY "Error limit exceeded: " ERROR-LIMIT EXIT PERFORM END-IF END-PERFORM.

Best Practices and Guidelines

Naming Conventions

Use descriptive, self-documenting names for AS constants. Names should clearly indicate the purpose and usage of the constant. Follow consistent naming patterns throughout your application to improve maintainability.

Group related constants together in logical sections of the working storage. This makes it easier to locate and modify constants when business rules change or system requirements evolve.

Consider creating COPY members for commonly used constants that span multiple programs. This ensures consistency across the application suite and simplifies maintenance when constant values need to be updated.

Maintenance Considerations

Document the meaning and usage of each AS constant, especially those representing business rules or system-specific values. This documentation helps future maintainers understand the purpose and appropriate usage of each constant.

Avoid hardcoding literal values throughout your program. Instead, define them once using AS and reference the symbolic name. This approach makes it easier to change values when business requirements evolve.

Regular review and consolidation of constants can help prevent duplication and ensure that related constants use consistent values and naming patterns across the application.

Frequently Asked Questions

Q: Can AS be used with all data types in COBOL?

AS can be used with most elementary data items that have VALUE clauses, including numeric, alphabetic, and alphanumeric fields. However, usage may vary depending on your COBOL compiler and version.

Q: Are AS constants visible throughout the entire program?

The visibility of AS constants depends on where they are defined. Constants defined in WORKING-STORAGE are available throughout the program, while those in LOCAL-STORAGE are limited to the current program invocation.

Q: Can AS constants be modified during program execution?

AS constants defined with VALUE clauses are typically treated as compile-time constants. While the underlying field might be modifiable, it's considered poor practice to change values intended as constants during program execution.

Q: How do I share AS constants across multiple programs?

Create COPY members containing your AS constant definitions and include them in programs that need access to these constants. This ensures consistency and simplifies maintenance across your application suite.

Practice Exercises

Exercise 1: Business Rule Constants

Create a set of AS constants for a customer management system including status codes, credit limits, and business rule values. Use these constants in conditional logic.

Exercise 2: Special Character Library

Develop a comprehensive library of special character constants using AS, including control characters, delimiters, and formatting characters for report generation.

Exercise 3: Configuration Management

Design a configuration system using AS constants that can be easily modified for different environments (development, testing, production) by changing COPY member contents.

Knowledge Check Quiz

Question 1: What is the primary purpose of the AS keyword in COBOL?

Correct Answer: B) To create symbolic names for literal values

Question 2: Where are AS constants typically defined in a COBOL program?

Correct Answer: C) DATA DIVISION - WORKING-STORAGE SECTION