---
title: "COBOL: How to Define Key in Array"
description: "Key phrase in COBOL OCCURS is to define key value. Read here what is Key phrase and syntax to use that."
url: https://srinimf.com/2015/03/24/cobol-use-of-key-phrase-in-occurs-clause/
date: 2015-03-24
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2015/03/pexels-photo-3783725.jpeg?fit=1880%2C1243&ssl=1
categories: ["COBOL"]
tags: ["COBOL Tips and Tricks"]
type: post
lang: en
---

# COBOL: How to Define Key in Array

In COBOL, arrays are useful to store data internally. Here’s the best example [how to](https://srinimf.com/2011/10/15/pli-declaring-arrays/) define key in Array of OCCURS clause.

## What is KEY Phrase

- [Earlier post](http://srinimf.com/2015/07/20/mainframe-cobol-occurs-indexed-byusage-is-index-1-of-2/)I have discussed about **Indexed By** phrase. In this post I want to show you, how we can define KEY phrase in an array.

- **Tip:**SEARCH ALL works only for sorted table. That means, you must define both INDEXED BY and KEY IS phrases on a Table, on which you are searching.

### KEY in OCCURS

- The KEY phrase in an OCCURS clause states that the table is sorted by the sequence of keys named data-name-3.

- Each of the fields named by data-name-3 must be part of the table—either items subordinate to this data item or possibly the data-name of the table itself.

- The purpose of the KEY clause is to define the table’s ordering when you use the SEARCH statement with the ALL phrase.

### Syntax:

**Ascending/Descending KEY is data-name-3**

- Data-name-3 can be qualified, but it must not be subscripted even though it is a table element, since the reference is really to the entire table.

- Each key must be described as either ASCENDING or DESCENDING. This defines the order, either from lowest to highest (ASCENDING) or highest to lowest (DESCENDING) in which the table is arranged. The order is determined by either numeric comparison for a numeric data-name-3 or non-numeric comparison for other operands. The keys are listed in order of their significance from major to minor.

- The presence of a KEY clause does not mean that the table is necessarily in sorted order, nor does it cause the table to be sorted. It merely states the expected order of the table.

- The SEARCH statement  works correctly only if the table is in the order specified by the keys.
-

### Here is an example of a table defined with two keys:

```
01 MAIL-INFO.
      03 MAIL-LIST OCCURS 1000 TIMES
            ASCENDING KEY IS STATE, CITY.
          05 NAME      PIC X(20).
         05 ADDRESS   PIC X(20) OCCURS 3 TIMES.
         05 CITY      PIC X(20).
         05 STATE     PIC XX.
         05 ZIP       PIC 9(5).
```

### **Important interview question:-

**How sorting occurs**

1. This example shows a table (MAIL-LIST) whose elements are (expected to be) in order by STATE (from lowest to highest).
2. Elements of the table with the same STATE will be in order by CITY (from lowest to highest).

**Interview Question:**

1. Indexed By and KEY is – Both are different phrases in the context of OCCURS.
2. The phrase INDEXED BY, we use to retrieve a record dynamically. The KEY IS, a phrase you can use to sort an array.

**Summary**

Explained [how to use](https://srinimf.com/2018/05/25/cobol-real-time-example-to-write-if-logic-in-search-statement/)d Key phrase in COBOL internal table. Syntax is given to understand quickly and real interview questions added.

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2015%2F03%2F24%2Fcobol-use-of-key-phrase-in-occurs-clause%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2015%2F03%2Fpexels-photo-3783725.jpeg%3Ffit%3D1200%252C793%26ssl%3D1&description=COBOL%3A%20How%20to%20Define%20Key%20in%20Array)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2015/03/24/cobol-use-of-key-phrase-in-occurs-clause/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2015/03/24/cobol-use-of-key-phrase-in-occurs-clause/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2015/03/24/cobol-use-of-key-phrase-in-occurs-clause/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2015/03/24/cobol-use-of-key-phrase-in-occurs-clause/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20COBOL%3A%20How%20to%20Define%20Key%20in%20Array&body=https%3A%2F%2Fsrinimf.com%2F2015%2F03%2F24%2Fcobol-use-of-key-phrase-in-occurs-clause%2F&share=email)
-

### Like this:

Like Loading…