MainframeMaster

COBOL Tutorial

COBOL SD Statement - Quick Reference

Progress0 of 0 lessons

Overview

The SD statement is used to define sort-merge files in COBOL. These files are used as temporary storage during SORT and MERGE operations and are managed automatically by the SORT utility.

Purpose and Usage

  • Sort-merge files - Define temporary files for sorting operations
  • SORT utility - Work with the COBOL SORT utility
  • Temporary storage - Provide temporary storage during sorting
  • Large datasets - Handle datasets too large for memory sorting
  • Automatic management - Files managed by SORT utility

Syntax

The SD statement follows a simple syntax for defining sort-merge files.

Basic Syntax

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
* Basic SD statement syntax SD sort-file-name. 01 record-name. * record description * Complete example IDENTIFICATION DIVISION. PROGRAM-ID. SORT-EXAMPLE. DATA DIVISION. FILE SECTION. SD SORT-WORK-FILE. 01 SORT-RECORD. 05 SORT-KEY-1 PIC X(10). 05 SORT-KEY-2 PIC 9(6). 05 SORT-DATA PIC X(50). FD INPUT-FILE. 01 INPUT-RECORD. 05 INPUT-KEY-1 PIC X(10). 05 INPUT-KEY-2 PIC 9(6). 05 INPUT-DATA PIC X(50). FD OUTPUT-FILE. 01 OUTPUT-RECORD. 05 OUTPUT-KEY-1 PIC X(10). 05 OUTPUT-KEY-2 PIC 9(6). 05 OUTPUT-DATA PIC X(50). PROCEDURE DIVISION. MAIN-LOGIC. SORT SORT-WORK-FILE ON ASCENDING KEY SORT-KEY-1 ON DESCENDING KEY SORT-KEY-2 USING INPUT-FILE GIVING OUTPUT-FILE STOP RUN.

SD defines the structure for sort-merge operations.

Practical Examples

Examples of using the SD statement in different sorting scenarios.

Simple Sort Operation

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
* Simple sort with SD DATA DIVISION. FILE SECTION. SD SORT-FILE. 01 SORT-RECORD. 05 SORT-KEY PIC X(20). 05 SORT-VALUE PIC 9(6). FD UNSORTED-FILE. 01 UNSORTED-RECORD. 05 UNSORTED-KEY PIC X(20). 05 UNSORTED-VALUE PIC 9(6). FD SORTED-FILE. 01 SORTED-RECORD. 05 SORTED-KEY PIC X(20). 05 SORTED-VALUE PIC 9(6). PROCEDURE DIVISION. SORT-DATA. SORT SORT-FILE ON ASCENDING KEY SORT-KEY USING UNSORTED-FILE GIVING SORTED-FILE STOP RUN.

SD enables simple sorting operations.

Best Practices

Understanding best practices ensures effective use of the SD statement.

Best Practices

  • Match record structures - Ensure SD record matches input/output records
  • Use meaningful names - Choose descriptive file names
  • Plan sort keys - Design sort keys carefully
  • Consider performance - Use SD for large datasets
  • Test thoroughly - Verify sort operations work correctly

Test Your Knowledge

1. What is the primary purpose of the SD statement in COBOL?

  • To define database files
  • To define sort-merge files
  • To define input files
  • To define output files

2. In which COBOL division is the SD statement typically used?

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

3. What does SD stand for in COBOL?

  • Sort Data
  • Sort Description
  • System Data
  • Storage Definition

4. Can SD files be used for regular file operations?

  • Yes, always
  • No, never
  • Only for reading
  • Only for writing

5. What is the relationship between SD and SORT statements?

  • They are the same thing
  • SD defines the file, SORT uses it
  • SD is faster than SORT
  • They cannot be used together

Frequently Asked Questions