COBOL SOURCE Clause - Quick Reference
The SOURCE clause in COBOL is used to specify the source or origin of data items. It provides information about where data comes from and how it should be processed.
Primary Use
Define data source and origin specification
Division
DATA DIVISION
Data Type
All data types
Purpose
Data origin and source management
Overview
The SOURCE clause is a COBOL feature that allows you to specify the source or origin of data items. It provides information about where data comes from, which can be useful for data validation, integration purposes, and maintaining data lineage. The SOURCE clause helps document and manage data flow between different systems and components.
Syntax
1234567801 data-name [PICTURE clause] [SOURCE source-specification]. * Examples of SOURCE specifications: SOURCE IS "EXTERNAL-SYSTEM" SOURCE IS "DATABASE-TABLE" SOURCE IS "FILE-INPUT" SOURCE IS "USER-INPUT" SOURCE IS "CALCULATED"
Practical Examples
Basic SOURCE Clause Usage
1234567891011121314151617181920212223242526272829* Basic SOURCE clause examples IDENTIFICATION DIVISION. PROGRAM-ID. SOURCE-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * Customer data from external system 01 CUSTOMER-DATA. 05 CUSTOMER-ID PIC X(10) SOURCE IS "EXTERNAL-SYSTEM". 05 CUSTOMER-NAME PIC X(30) SOURCE IS "EXTERNAL-SYSTEM". 05 CUSTOMER-BALANCE PIC 9(8)V99 SOURCE IS "DATABASE". * Transaction data from file input 01 TRANSACTION-DATA. 05 TRANSACTION-ID PIC X(15) SOURCE IS "FILE-INPUT". 05 TRANSACTION-AMOUNT PIC 9(8)V99 SOURCE IS "FILE-INPUT". 05 TRANSACTION-DATE PIC X(8) SOURCE IS "FILE-INPUT". * Calculated fields 01 CALCULATED-FIELDS. 05 TOTAL-AMOUNT PIC 9(10)V99 SOURCE IS "CALCULATED". 05 AVERAGE-BALANCE PIC 9(8)V99 SOURCE IS "CALCULATED". PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "Customer ID source: EXTERNAL-SYSTEM" DISPLAY "Transaction ID source: FILE-INPUT" DISPLAY "Total amount source: CALCULATED" STOP RUN.
Explanation: This example demonstrates basic usage of the SOURCE clause to specify the origin of different data items. CUSTOMER-DATA fields are marked as coming from an external system, TRANSACTION-DATA fields are from file input, and CALCULATED-FIELDS are marked as computed values. This helps document data lineage and origins.
SOURCE with Data Integration
12345678910111213141516171819202122232425262728293031323334353637383940* SOURCE clause for data integration scenarios IDENTIFICATION DIVISION. PROGRAM-ID. DATA-INTEGRATION-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * Legacy system data 01 LEGACY-CUSTOMER. 05 LEGACY-CUST-ID PIC X(8) SOURCE IS "LEGACY-SYSTEM". 05 LEGACY-CUST-NAME PIC X(25) SOURCE IS "LEGACY-SYSTEM". 05 LEGACY-ACCOUNT-NO PIC 9(10) SOURCE IS "LEGACY-SYSTEM". * Modern system data 01 MODERN-CUSTOMER. 05 MODERN-CUST-ID PIC X(10) SOURCE IS "MODERN-SYSTEM". 05 MODERN-CUST-NAME PIC X(30) SOURCE IS "MODERN-SYSTEM". 05 MODERN-ACCOUNT-NO PIC X(12) SOURCE IS "MODERN-SYSTEM". * Consolidated customer data 01 CONSOLIDATED-CUSTOMER. 05 CONSOLIDATED-ID PIC X(10) SOURCE IS "CONSOLIDATED". 05 CONSOLIDATED-NAME PIC X(30) SOURCE IS "CONSOLIDATED". 05 CONSOLIDATED-ACCOUNT PIC X(12) SOURCE IS "CONSOLIDATED". 05 DATA-SOURCE-FLAG PIC X(1) SOURCE IS "CONSOLIDATED". * L = Legacy, M = Modern, C = Combined PROCEDURE DIVISION. MAIN-LOGIC. * Process legacy system data MOVE LEGACY-CUST-ID TO CONSOLIDATED-ID MOVE LEGACY-CUST-NAME TO CONSOLIDATED-NAME MOVE LEGACY-ACCOUNT-NO TO CONSOLIDATED-ACCOUNT MOVE 'L' TO DATA-SOURCE-FLAG DISPLAY "Processing data from: " DATA-SOURCE-FLAG DISPLAY "Customer ID: " CONSOLIDATED-ID DISPLAY "Customer Name: " CONSOLIDATED-NAME DISPLAY "Account Number: " CONSOLIDATED-ACCOUNT STOP RUN.
Explanation: This example shows how the SOURCE clause can be used in data integration scenarios. Different customer data structures are marked with their respective sources (LEGACY-SYSTEM, MODERN-SYSTEM, CONSOLIDATED). The DATA-SOURCE-FLAG tracks which system the data originated from, helping maintain data lineage during integration processes.
SOURCE with Data Validation
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455* SOURCE clause for data validation IDENTIFICATION DIVISION. PROGRAM-ID. DATA-VALIDATION-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * User input data 01 USER-INPUT-DATA. 05 USER-NAME PIC X(20) SOURCE IS "USER-INPUT". 05 USER-AGE PIC 99 SOURCE IS "USER-INPUT". 05 USER-EMAIL PIC X(50) SOURCE IS "USER-INPUT". * Database reference data 01 REFERENCE-DATA. 05 VALID-AGE-MIN PIC 99 SOURCE IS "DATABASE" VALUE 18. 05 VALID-AGE-MAX PIC 99 SOURCE IS "DATABASE" VALUE 120. 05 EMAIL-PATTERN PIC X(50) SOURCE IS "DATABASE" VALUE "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$". * Validation results 01 VALIDATION-RESULTS. 05 NAME-VALID PIC X(1) SOURCE IS "VALIDATION". 05 AGE-VALID PIC X(1) SOURCE IS "VALIDATION". 05 EMAIL-VALID PIC X(1) SOURCE IS "VALIDATION". PROCEDURE DIVISION. MAIN-LOGIC. * Validate user name (not empty) IF USER-NAME = SPACES MOVE 'N' TO NAME-VALID ELSE MOVE 'Y' TO NAME-VALID END-IF * Validate age range IF USER-AGE >= VALID-AGE-MIN AND USER-AGE <= VALID-AGE-MAX MOVE 'Y' TO AGE-VALID ELSE MOVE 'N' TO AGE-VALID END-IF * Validate email format (simplified) IF USER-EMAIL CONTAINS '@' AND USER-EMAIL CONTAINS '.' MOVE 'Y' TO EMAIL-VALID ELSE MOVE 'N' TO EMAIL-VALID END-IF * Display validation results DISPLAY "Validation Results:" DISPLAY "Name valid: " NAME-VALID DISPLAY "Age valid: " AGE-VALID DISPLAY "Email valid: " EMAIL-VALID STOP RUN.
Explanation: This example demonstrates how the SOURCE clause can be used for data validation. User input data is marked with SOURCE IS "USER-INPUT", reference data from the database is marked with SOURCE IS "DATABASE", and validation results are marked with SOURCE IS "VALIDATION". This helps track data flow and maintain clear separation between input data, reference data, and validation results.
SOURCE with Financial Data Processing
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455* SOURCE clause for financial data processing IDENTIFICATION DIVISION. PROGRAM-ID. FINANCIAL-DATA-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. * Transaction data from multiple sources 01 TRANSACTION-RECORD. 05 TRANSACTION-ID PIC X(15) SOURCE IS "TRANSACTION-SYSTEM". 05 ACCOUNT-NUMBER PIC X(12) SOURCE IS "ACCOUNT-SYSTEM". 05 TRANSACTION-AMOUNT PIC 9(8)V99 SOURCE IS "TRANSACTION-SYSTEM". 05 TRANSACTION-DATE PIC X(8) SOURCE IS "TRANSACTION-SYSTEM". 05 TRANSACTION-TYPE PIC X(2) SOURCE IS "TRANSACTION-SYSTEM". * Account reference data 01 ACCOUNT-REFERENCE. 05 REF-ACCOUNT-NUMBER PIC X(12) SOURCE IS "ACCOUNT-SYSTEM". 05 ACCOUNT-TYPE PIC X(1) SOURCE IS "ACCOUNT-SYSTEM". 05 ACCOUNT-LIMIT PIC 9(8)V99 SOURCE IS "ACCOUNT-SYSTEM". 05 ACCOUNT-STATUS PIC X(1) SOURCE IS "ACCOUNT-SYSTEM". * Calculated financial data 01 FINANCIAL-CALCULATIONS. 05 DAILY-TOTAL PIC 9(10)V99 SOURCE IS "CALCULATED". 05 MONTHLY-TOTAL PIC 9(12)V99 SOURCE IS "CALCULATED". 05 AVERAGE-TRANSACTION PIC 9(8)V99 SOURCE IS "CALCULATED". 05 TRANSACTION-COUNT PIC 9(6) SOURCE IS "CALCULATED". * Audit trail data 01 AUDIT-INFORMATION. 05 PROCESSING-DATE PIC X(8) SOURCE IS "SYSTEM-DATE". 05 PROCESSING-TIME PIC X(6) SOURCE IS "SYSTEM-TIME". 05 USER-ID PIC X(8) SOURCE IS "USER-SESSION". 05 BATCH-NUMBER PIC X(10) SOURCE IS "BATCH-SYSTEM". PROCEDURE DIVISION. MAIN-LOGIC. * Process transaction data DISPLAY "Processing transaction from: TRANSACTION-SYSTEM" DISPLAY "Account data from: ACCOUNT-SYSTEM" DISPLAY "Calculations: CALCULATED" DISPLAY "Audit info from: SYSTEM-DATE, SYSTEM-TIME, USER-SESSION" * Simulate data processing MOVE 1000.00 TO DAILY-TOTAL MOVE 25000.00 TO MONTHLY-TOTAL MOVE 125.00 TO AVERAGE-TRANSACTION MOVE 20 TO TRANSACTION-COUNT DISPLAY "Daily Total: " DAILY-TOTAL DISPLAY "Monthly Total: " MONTHLY-TOTAL DISPLAY "Average Transaction: " AVERAGE-TRANSACTION DISPLAY "Transaction Count: " TRANSACTION-COUNT STOP RUN.
Explanation: This example shows how the SOURCE clause can be used in financial data processing. Different data elements are marked with their respective sources: transaction data from TRANSACTION-SYSTEM, account reference data from ACCOUNT-SYSTEM, calculated financial data as CALCULATED, and audit information from various system sources. This helps maintain data lineage and provides clear documentation of data origins in financial processing applications.
Best Practices and Considerations
Important Considerations
- Use consistent source naming conventions across your application
- Document source specifications clearly for maintenance purposes
- Consider using SOURCE for data lineage and audit trails
- Validate that source specifications are accurate and up-to-date
- Use SOURCE in conjunction with other data definition clauses
Advantages
- Provides clear data lineage documentation
- Helps with data validation and processing
- Supports data integration scenarios
- Improves code maintainability
- Facilitates audit trail creation
Limitations
- Requires consistent naming conventions
- May not be supported in all COBOL implementations
- Adds complexity to data definitions
- Requires maintenance when data sources change
- May impact code readability if overused
Best Practices
- • Use descriptive and consistent source names
- • Document source specifications in program comments
- • Update source specifications when data sources change
- • Use SOURCE for critical data lineage requirements
- • Consider using SOURCE in data integration projects
Test Your Knowledge
1. What is the primary purpose of the SOURCE clause in COBOL?
- To specify source code location
- To define data source and origin specification
- To source programs
- To source files
2. In which COBOL division is the SOURCE clause typically used?
- IDENTIFICATION DIVISION
- ENVIRONMENT DIVISION
- DATA DIVISION
- PROCEDURE DIVISION
3. What does the SOURCE clause control in data definition?
- Data validation
- Data source and origin specification
- Data type
- Data format
4. Can SOURCE be used with all data types?
- Yes, with all data types
- No, only with numeric types
- No, only with character types
- Only with specific data types
5. What is the relationship between SOURCE and data processing?
- They are unrelated
- SOURCE specifies data origin for processing
- SOURCE only affects display
- SOURCE is for validation only
Related Concepts
SOURCE-COMPUTER Clause
Understanding SOURCE-COMPUTER for development environment specification.
Data Integration
Data integration techniques and best practices.
Data Lineage
Data lineage and audit trail management.
Data Validation
Data validation and quality assurance.
System Integration
System integration and data flow management.