MainframeMaster

COBOL Tutorial

COBOL ASSIGN TO Clause - Quick Reference

Progress0 of 0 lessons

Overview

The ASSIGN TO clause is used in the FILE-CONTROL paragraph to specify the external name or device for a file. It establishes the connection between the logical file name used in the program and the physical file or device in the operating system.

Purpose and Usage

  • File assignment - Connect logical files to physical datasets
  • Device specification - Assign files to specific devices
  • External naming - Specify external file names
  • System integration - Interface with operating system resources
  • Dynamic assignment - Use variables for runtime file assignment

File Assignment Concept

Logical Name: CUSTOMER-FILE
ASSIGN TO: "CUSTOMER.DAT"
Physical File: /data/customer.dat
Program ↔ External Name ↔ Physical File

ASSIGN TO creates the bridge between program logic and physical storage.

Syntax

The ASSIGN TO clause follows specific syntax patterns within SELECT statements and can use various types of values for file assignment.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
* Basic ASSIGN TO syntax SELECT file-name ASSIGN TO external-name * With literal string SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" * With data name (variable) SELECT CUSTOMER-FILE ASSIGN TO WS-FILE-NAME * With figurative constant SELECT OUTPUT-FILE ASSIGN TO PRINTER * Complete example with other clauses SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS

ASSIGN TO can use literal strings, data names, or figurative constants.

Value Types for ASSIGN TO

TypeExampleUsage
Literal String"CUSTOMER.DAT"Fixed file names
Data NameWS-FILE-NAMEDynamic assignment
Figurative ConstantPRINTER, CONSOLESystem devices
Environment Variable$DATAFILESystem configuration

Common Figurative Constants

cobol
1
2
3
4
5
6
7
8
9
10
11
* System devices and standard files SELECT OUTPUT-FILE ASSIGN TO PRINTER * System printer SELECT INPUT-FILE ASSIGN TO SYSIN * Standard input SELECT OUTPUT-FILE ASSIGN TO SYSOUT * Standard output SELECT ERROR-FILE ASSIGN TO SYSERR * Error output SELECT CONSOLE-FILE ASSIGN TO CONSOLE * Console I/O * Mainframe specific SELECT TAPE-FILE ASSIGN TO TAPE * Tape device SELECT DISK-FILE ASSIGN TO DISK * Disk device SELECT CARD-FILE ASSIGN TO CARD-READER * Card reader

Figurative constants provide standard device references.

Practical Examples

These examples demonstrate how to use the ASSIGN TO clause effectively in different file handling scenarios.

Sequential File Assignment

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
IDENTIFICATION DIVISION. PROGRAM-ID. SEQUENTIAL-FILE-EXAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT CUSTOMER-FILE ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD CUSTOMER-FILE. 01 CUSTOMER-RECORD. 05 CUSTOMER-ID PIC 9(6). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(8)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC X(2). PROCEDURE DIVISION. MAIN-PROCESS. OPEN INPUT CUSTOMER-FILE IF WS-FILE-STATUS NOT = "00" DISPLAY "Error opening file: " WS-FILE-STATUS STOP RUN END-IF * Process file records PERFORM UNTIL WS-FILE-STATUS = "10" READ CUSTOMER-FILE IF WS-FILE-STATUS = "00" DISPLAY "Customer: " CUSTOMER-NAME END-IF END-PERFORM CLOSE CUSTOMER-FILE STOP RUN.

Basic sequential file assignment with literal string and file status checking.

Dynamic File Assignment

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
IDENTIFICATION DIVISION. PROGRAM-ID. DYNAMIC-FILE-ASSIGNMENT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO WS-INPUT-FILE-NAME ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-INPUT-FILE-NAME PIC X(50). 01 WS-FILE-STATUS PIC X(2). 01 WS-USER-INPUT PIC X(50). PROCEDURE DIVISION. MAIN-PROCESS. * Get file name from user DISPLAY "Enter input file name: " ACCEPT WS-USER-INPUT * Set the file name for assignment MOVE WS-USER-INPUT TO WS-INPUT-FILE-NAME * Open the dynamically assigned file OPEN INPUT INPUT-FILE IF WS-FILE-STATUS NOT = "00" DISPLAY "Error opening file: " WS-FILE-STATUS STOP RUN END-IF DISPLAY "File opened successfully: " WS-INPUT-FILE-NAME CLOSE INPUT-FILE STOP RUN.

Dynamic file assignment using a data name that can be set at runtime.

Device Assignment

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
IDENTIFICATION DIVISION. PROGRAM-ID. DEVICE-ASSIGNMENT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT REPORT-FILE ASSIGN TO PRINTER ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL. SELECT LOG-FILE ASSIGN TO "LOG.TXT" ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL. SELECT CONSOLE-OUTPUT ASSIGN TO CONSOLE ORGANIZATION IS SEQUENTIAL ACCESS MODE IS SEQUENTIAL. DATA DIVISION. FILE SECTION. FD REPORT-FILE. 01 REPORT-LINE PIC X(80). FD LOG-FILE. 01 LOG-LINE PIC X(80). FD CONSOLE-OUTPUT. 01 CONSOLE-LINE PIC X(80). PROCEDURE DIVISION. MAIN-PROCESS. * Write to printer OPEN OUTPUT REPORT-FILE MOVE "This goes to the printer" TO REPORT-LINE WRITE REPORT-LINE CLOSE REPORT-FILE * Write to log file OPEN OUTPUT LOG-FILE MOVE "This goes to the log file" TO LOG-LINE WRITE LOG-LINE CLOSE LOG-FILE * Write to console OPEN OUTPUT CONSOLE-OUTPUT MOVE "This goes to the console" TO CONSOLE-LINE WRITE CONSOLE-LINE CLOSE CONSOLE-OUTPUT STOP RUN.

Different types of device assignments including printer, file, and console.

Indexed File Assignment

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
IDENTIFICATION DIVISION. PROGRAM-ID. INDEXED-FILE-ASSIGNMENT. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INVENTORY-FILE ASSIGN TO "INVENTORY.IDX" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS PRODUCT-ID FILE STATUS IS WS-FILE-STATUS. DATA DIVISION. FILE SECTION. FD INVENTORY-FILE. 01 INVENTORY-RECORD. 05 PRODUCT-ID PIC 9(6). 05 PRODUCT-NAME PIC X(30). 05 QUANTITY-ON-HAND PIC 9(5). 05 UNIT-PRICE PIC 9(6)V99. WORKING-STORAGE SECTION. 01 WS-FILE-STATUS PIC X(2). 01 WS-PRODUCT-ID PIC 9(6). PROCEDURE DIVISION. MAIN-PROCESS. OPEN I-O INVENTORY-FILE IF WS-FILE-STATUS NOT = "00" DISPLAY "Error opening indexed file: " WS-FILE-STATUS STOP RUN END-IF * Read a specific record MOVE 123456 TO WS-PRODUCT-ID READ INVENTORY-FILE IF WS-FILE-STATUS = "00" DISPLAY "Product: " PRODUCT-NAME ELSE DISPLAY "Product not found" END-IF CLOSE INVENTORY-FILE STOP RUN.

Indexed file assignment with random access and record key specification.

Best Practices and Considerations

Understanding best practices ensures proper file assignment and system integration.

File Assignment Best Practices

  • Use meaningful names - Choose descriptive external file names
  • Validate file existence - Check file status after OPEN operations
  • Handle errors gracefully - Provide meaningful error messages
  • Use appropriate devices - Match file purpose to device type
  • Consider security - Ensure proper file permissions and access

Common Pitfalls to Avoid

PitfallProblemSolution
Hardcoded pathsPoor portabilityUse environment variables or configuration
Missing error handlingProgram crashesCheck file status codes
Incorrect device namesAssignment failuresVerify device availability
Case sensitivityFile not foundMatch exact case requirements
Permission issuesAccess deniedCheck file permissions

Platform Considerations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
* Windows file assignment SELECT DATA-FILE ASSIGN TO "C:\DATA\CUSTOMER.DAT" * Unix/Linux file assignment SELECT DATA-FILE ASSIGN TO "/data/customer.dat" * Mainframe dataset assignment SELECT DATA-FILE ASSIGN TO "USER.CUSTOMER.DATA" * Environment variable usage SELECT DATA-FILE ASSIGN TO "$DATADIR/customer.dat" * Relative path assignment SELECT DATA-FILE ASSIGN TO "./data/customer.dat"

Different platforms may require different assignment syntax.

ASSIGN TO Clause Quick Reference

UsageSyntaxExample
Literal file nameASSIGN TO "filename"ASSIGN TO "CUSTOMER.DAT"
Variable assignmentASSIGN TO data-nameASSIGN TO WS-FILE-NAME
System deviceASSIGN TO device-constantASSIGN TO PRINTER
Standard I/OASSIGN TO SYSIN/SYSOUTASSIGN TO SYSIN
Console I/OASSIGN TO CONSOLEASSIGN TO CONSOLE

Test Your Knowledge

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

  • To assign variables
  • To assign files to external devices or datasets
  • To assign memory
  • To assign program names

2. In which division is the ASSIGN TO clause typically used?

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

3. What type of value can be used with ASSIGN TO?

  • Only literal values
  • Only variable names
  • Literal values, variable names, or figurative constants
  • Only figurative constants

4. What is the relationship between ASSIGN TO and file organization?

  • They are the same thing
  • ASSIGN TO specifies the device, organization specifies the structure
  • ASSIGN TO is only for sequential files
  • They cannot be used together

5. Which of the following is a valid ASSIGN TO usage?

  • ASSIGN TO "CUSTOMER.DAT"
  • ASSIGN TO CUSTOMER-FILE
  • ASSIGN TO PRINTER
  • All of the above

Frequently Asked Questions