MainframeMaster

COBOL Tutorial

COBOL REPLACING Clause - Quick Reference

Progress0 of 0 lessons

Overview

The REPLACING clause is used with COPY statements to perform text substitution during compilation. It enables code customization and template processing by substituting specific text patterns in copied code.

Purpose and Usage

  • Text substitution - Replace text during compilation
  • Code customization - Customize copied code for specific needs
  • Template processing - Create reusable code templates
  • Environment adaptation - Adapt code for different environments
  • Code standardization - Maintain consistency with customization

REPLACING vs REPLACE Concept

REPLACING: COPY text → [Substitution] → Customized code
REPLACE: Runtime string → [Substitution] → Modified string
REPLACING is compile-time, REPLACE is runtime

REPLACING performs text substitution during compilation of COPY statements.

Syntax

The REPLACING clause follows specific syntax patterns within COPY statements and can include multiple text substitutions.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
* Basic REPLACING clause syntax COPY copybook-name REPLACING text BY text * Multiple replacements COPY copybook-name REPLACING text1 BY text2 text3 BY text4 * Complete example COPY CUSTOMER-DATA REPLACING "CUSTOMER" BY "CLIENT" "CUST" BY "CLT" * With specific text patterns COPY FILE-DEFINITION REPLACING "FILE-NAME" BY "CUSTOMER-FILE" "RECORD-SIZE" BY "200" * Alternative syntax COPY copybook-name REPLACING ==text== BY ==replacement==

REPLACING uses BY to separate the text to replace from the replacement text.

REPLACING vs REPLACE Comparison

AspectREPLACINGREPLACE
TimingCompile-timeRuntime
UsageCOPY statementsINSPECT statements
PurposeCode customizationData manipulation
ScopeSource codeString data
PerformanceNo runtime impactRuntime processing

Text Pattern Options

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* Simple text replacement COPY copybook REPLACING "OLD" BY "NEW" * Field name replacement COPY data-def REPLACING "FIELD-NAME" BY "CUSTOMER-ID" * Multiple word replacement COPY template REPLACING "CUSTOMER DATA" BY "CLIENT INFORMATION" * Numeric replacement COPY config REPLACING "100" BY "200" * Mixed replacement COPY template REPLACING "CUSTOMER" BY "CLIENT" "100" BY "200" "OLD-FORMAT" BY "NEW-FORMAT" * Using delimiters for complex text COPY template REPLACING ==CUSTOMER-NAME== BY ==CLIENT-NAME== ==RECORD-SIZE== BY ==200==

Different text patterns can be used for various customization needs.

Practical Examples

These examples demonstrate how to use the REPLACING clause effectively in different code customization scenarios.

Data Definition Customization

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
* Original copybook: CUSTOMER-DATA.CPY 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(5). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-ADDRESS PIC X(50). 05 CUSTOMER-PHONE PIC X(15). 05 CUSTOMER-BALANCE PIC 9(7)V99. * Using REPLACING to customize for different programs IDENTIFICATION DIVISION. PROGRAM-ID. CUSTOMER-PROCESS. DATA DIVISION. WORKING-STORAGE SECTION. COPY CUSTOMER-DATA REPLACING "CUSTOMER" BY "CLIENT" "BALANCE" BY "AMOUNT" * Results in: * 01 CLIENT-RECORD. * 05 CLIENT-ID PIC 9(5). * 05 CLIENT-NAME PIC X(30). * 05 CLIENT-ADDRESS PIC X(50). * 05 CLIENT-PHONE PIC X(15). * 05 CLIENT-AMOUNT PIC 9(7)V99. PROCEDURE DIVISION. MAIN-PROCESS. * Use the customized field names MOVE 12345 TO CLIENT-ID MOVE "JOHN DOE" TO CLIENT-NAME MOVE "123 MAIN ST" TO CLIENT-ADDRESS MOVE "555-1234" TO CLIENT-PHONE MOVE 1000.00 TO CLIENT-AMOUNT DISPLAY "Client ID: " CLIENT-ID DISPLAY "Client Amount: " CLIENT-AMOUNT STOP RUN.

REPLACING customizes data definitions for different program contexts.

Environment-Specific Configuration

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
* Configuration copybook: CONFIG.CPY 01 SYSTEM-CONFIG. 05 DATABASE-NAME PIC X(20) VALUE "PROD-DB". 05 FILE-PATH PIC X(50) VALUE "/prod/data/". 05 LOG-LEVEL PIC X(10) VALUE "ERROR". 05 MAX-RECORDS PIC 9(5) VALUE 10000. 05 TIMEOUT-VALUE PIC 9(3) VALUE 300. * Development environment IDENTIFICATION DIVISION. PROGRAM-ID. DEV-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. COPY CONFIG REPLACING "PROD-DB" BY "DEV-DB" "/prod/data/" BY "/dev/data/" "ERROR" BY "DEBUG" "10000" BY "1000" "300" BY "60" * Results in development configuration: * 01 SYSTEM-CONFIG. * 05 DATABASE-NAME PIC X(20) VALUE "DEV-DB". * 05 FILE-PATH PIC X(50) VALUE "/dev/data/". * 05 LOG-LEVEL PIC X(10) VALUE "DEBUG". * 05 MAX-RECORDS PIC 9(5) VALUE 1000. * 05 TIMEOUT-VALUE PIC 9(3) VALUE 60. * Production environment IDENTIFICATION DIVISION. PROGRAM-ID. PROD-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. COPY CONFIG * No REPLACING - uses default production values PROCEDURE DIVISION. MAIN-PROCESS. DISPLAY "Database: " DATABASE-NAME DISPLAY "File Path: " FILE-PATH DISPLAY "Log Level: " LOG-LEVEL DISPLAY "Max Records: " MAX-RECORDS DISPLAY "Timeout: " TIMEOUT-VALUE STOP RUN.

REPLACING adapts configuration for different environments.

Code Template Customization

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
* Template copybook: PROCESS-TEMPLATE.CPY PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT INPUT-FILE OPEN OUTPUT OUTPUT-FILE PERFORM UNTIL END-OF-FILE READ INPUT-FILE AT END MOVE "Y" TO EOF-FLAG NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM CLOSE INPUT-FILE CLOSE OUTPUT-FILE STOP RUN. PROCESS-RECORD. * Process the record MOVE INPUT-RECORD TO OUTPUT-RECORD WRITE OUTPUT-RECORD. * Using REPLACING to customize the template IDENTIFICATION DIVISION. PROGRAM-ID. CUSTOMER-PROCESS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL. SELECT REPORT-FILE ASSIGN TO "REPORT.OUT" ORGANIZATION IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD PIC X(100). FD REPORT-FILE. 01 REPORT-RECORD PIC X(100). WORKING-STORAGE SECTION. 01 EOF-FLAG PIC X VALUE "N". 88 END-OF-FILE VALUE "Y". PROCEDURE DIVISION. COPY PROCESS-TEMPLATE REPLACING "INPUT-FILE" BY "CUSTOMER-FILE" "OUTPUT-FILE" BY "REPORT-FILE" "INPUT-RECORD" BY "CUSTOMER-RECORD" "OUTPUT-RECORD" BY "REPORT-RECORD" "PROCESS-RECORD" BY "PROCESS-CUSTOMER" * Custom processing logic PROCESS-CUSTOMER. * Custom customer processing logic MOVE CUSTOMER-RECORD TO REPORT-RECORD * Add custom processing here WRITE REPORT-RECORD.

REPLACING customizes code templates for specific applications.

Multiple Program Variations

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
71
72
73
74
75
76
77
78
79
80
* Base copybook: FILE-PROCESS.CPY 01 FILE-STATUS PIC XX. 01 RECORD-COUNT PIC 9(6). 01 ERROR-COUNT PIC 9(6). PROCEDURE DIVISION. INITIALIZE-PROCESS. MOVE ZERO TO RECORD-COUNT MOVE ZERO TO ERROR-COUNT OPEN INPUT INPUT-FILE IF FILE-STATUS NOT = "00" DISPLAY "Error opening input file: " FILE-STATUS STOP RUN END-IF. PROCESS-LOOP. READ INPUT-FILE AT END GO TO FINALIZE-PROCESS NOT AT END ADD 1 TO RECORD-COUNT PERFORM PROCESS-RECORD END-READ GO TO PROCESS-LOOP. FINALIZE-PROCESS. CLOSE INPUT-FILE DISPLAY "Records processed: " RECORD-COUNT DISPLAY "Errors encountered: " ERROR-COUNT STOP RUN. * Program 1: Customer processing IDENTIFICATION DIVISION. PROGRAM-ID. CUSTOMER-PROCESS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT". DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD PIC X(100). WORKING-STORAGE SECTION. COPY FILE-PROCESS REPLACING "INPUT-FILE" BY "CUSTOMER-FILE" "PROCESS-RECORD" BY "PROCESS-CUSTOMER" PROCESS-CUSTOMER. * Customer-specific processing IF CUSTOMER-RECORD(1:1) = "A" ADD 1 TO ERROR-COUNT END-IF. * Program 2: Order processing IDENTIFICATION DIVISION. PROGRAM-ID. ORDER-PROCESS. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT ORDER-FILE ASSIGN TO "ORDER.DAT". DATA DIVISION. FILE SECTION. FD ORDER-FILE. 01 ORDER-RECORD PIC X(100). WORKING-STORAGE SECTION. COPY FILE-PROCESS REPLACING "INPUT-FILE" BY "ORDER-FILE" "PROCESS-RECORD" BY "PROCESS-ORDER" PROCESS-ORDER. * Order-specific processing IF ORDER-RECORD(1:1) = "X" ADD 1 TO ERROR-COUNT END-IF.

REPLACING creates multiple program variations from a single template.

Best Practices and Tips

Following these best practices ensures effective use of the REPLACING clause in COBOL applications.

REPLACING Best Practices

  • Use meaningful text patterns - Choose clear, descriptive replacement text
  • Document replacement rules - Clearly document what text is being replaced
  • Test thoroughly - Verify that replacements create valid COBOL code
  • Use consistent naming - Follow consistent naming conventions
  • Keep replacements simple - Avoid overly complex text patterns
  • Review compiler listings - Check the actual generated code

Common Pitfalls to Avoid

PitfallProblemSolution
Ambiguous text patternsUnintended replacementsUse specific, unique patterns
Syntax errors after replacementCompilation failuresTest replacements thoroughly
Missing replacementsIncomplete customizationReview all text patterns
Overly complex patternsMaintenance difficultiesKeep patterns simple
Inconsistent namingCode confusionFollow naming conventions

Performance Considerations

  • No runtime impact - REPLACING is resolved during compilation
  • Compile-time processing - Text substitution occurs during compilation
  • Efficient customization - No runtime overhead for customization
  • Template efficiency - Reusable code without runtime cost
  • Compiler optimization - Modern compilers handle REPLACING efficiently

When to Use REPLACING vs Other Methods

ScenarioUse REPLACINGUse Other Methods
Code customizationYesNo
Template processingYesNo
Runtime data manipulationNoYes (REPLACE)
Complex logicNoYes (procedures)
Environment configurationYesSometimes

REPLACING Clause Quick Reference

UsageSyntaxExample
Single replacementCOPY copybook REPLACING text BY textCOPY DATA REPLACING "OLD" BY "NEW"
Multiple replacementsCOPY copybook REPLACING text1 BY text2 text3 BY text4COPY DATA REPLACING "A" BY "X" "B" BY "Y"
Field name replacementCOPY copybook REPLACING "FIELD" BY "NEW-FIELD"COPY DATA REPLACING "CUSTOMER" BY "CLIENT"
Value replacementCOPY copybook REPLACING "100" BY "200"COPY CONFIG REPLACING "100" BY "200"
Delimited textCOPY copybook REPLACING ==text== BY ==replacement==COPY TEMPLATE REPLACING ==NAME== BY ==CUSTOMER==

Test Your Knowledge

1. What is the primary purpose of the REPLACING clause in COBOL?

  • To replace files
  • To perform text substitution in COPY statements
  • To replace records in files
  • To replace programs

2. In which statement is the REPLACING clause most commonly used?

  • MOVE
  • COPY
  • INSPECT
  • STRING

3. What happens when a REPLACING clause is executed?

  • Text is substituted during compilation
  • Text is substituted at runtime
  • Text is deleted from the program
  • Text is moved to another location

4. What is the relationship between REPLACING and COPY statements?

  • They are the same thing
  • REPLACING customizes COPY content
  • REPLACING is faster than COPY
  • They cannot be used together

5. Which of the following is a valid REPLACING clause usage?

  • REPLACING text BY text
  • REPLACING text WITH text
  • REPLACING text FOR text
  • All of the above

Frequently Asked Questions