---
title: "How to Check ‘Comp Variable’ for Spaces and Low-Values"
description: "Here's sample COBOL logic where it tells you how to initialize the Comp variable before they move to numeric."
url: https://srinimf.com/2021/09/05/how-to-check-comp-variable-for-spaces-and-low-values/
date: 2021-09-05
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2021/09/pexels-photo-1370298.jpeg?fit=1880%2C1255&ssl=1
categories: ["COBOL"]
tags: ["COMP Variable Initialization"]
type: post
lang: en
---

# How to Check ‘Comp Variable’ for Spaces and Low-Values

In COBOL, the prime use of the **comp variable** is for faster calculation and to save memory. Here is a resolution on [how to](https://srinimf.com/2011/10/15/pli-declaring-arrays/) check for spaces or Low-values. It’s tricky but not complex. Remember as general notes while coding, [if you have a Comp* variable,](https://srinimf.com/2015/11/21/2-easy-steps-write-your-cobol-program-efficient-way/) you need to check for proper initialization.

*Comp-3 variables*

## COBOL Definition for Comp*

Here I use comp-4 to demonstrate.

```
01 COUNTERS.
    05 WS_COUNT    COMP-4.
    05 WS_COUNT_X  REDEFINES  WS_COUNT PIC X(02).           
01 MY_DATA       PIC 9(04).
```

I move the comp-4 value to MY_DATA, which is a numeric type. When the input WS_COUNT has valid numeric, you will not face any issue, but if the WS_COUNT has spaces or low-values, junk data populates to MY_DATA. Below is my example to avoid junk data. Useful for [COBOL/Mainframe projects](https://srinimf.com/2015/12/16/cobol-move-comp-3-to-numeric/).

## [COBOL MOVE](https://srinimf.com/2010/04/04/cobol-stuff/) Statement

```
MOVE WS_COUNT  TO MY_DATA.
```

## Resolution to Check for [Spaces and](https://srinimf.com/2016/06/16/cobol-gt-spaces-and-ne-spaces/) Low-Values

```
IF WS_COUNT_X SPACES or LOW_VALUES
   MOVE ZEROS  TO MY_DATA
ELSE
   MOVE WS_COUNT TO MY_DATA
END-IF.
```

This way, you can avoid populating junk data in the target variable. It applies to all COMP* variables. [Check out more on Comp-4, Comp-5 and Comp-6.](https://srinimf.com/2016/06/24/stratus-cobol-or-vos-cobol-comp-4-vs-comp-5-and-comp-6/)

Important Points

- Comp-4 internally takes 2 bytes, So I use 2 bytes in redefines
- Comp-5 internally takes 4 bytes
- Comp-6 internally takes 8 bytes

## Shopping

- Black-Eyed Peas Recipes to Make All Year (Not Just on New Year’s Day or Mardi Gras)
- [Daily use bag](https://www.amazon.in/gp/product/B07CJCGM1M?ie=UTF8?tag=srinimf-21)

Related Posts

- [COBOL: How to MOVE Comp-3 to Numeric](https://srinimf.com/2015/12/16/cobol-move-comp-3-to-numeric/)DateDecember 16, 2015
- [COBOL Tough Questions (2 of 3)](https://srinimf.com/2012/01/08/cobol-tough-questions-2-of-3/)DateJanuary 8, 2012
- [Stratus COBOL or VoS COBOL -Comp-4 Vs Comp-5 and Comp-6](https://srinimf.com/2016/06/24/stratus-cobol-or-vos-cobol-comp-4-vs-comp-5-and-comp-6/)DateJune 24, 2016

### 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%2F2021%2F09%2F05%2Fhow-to-check-comp-variable-for-spaces-and-low-values%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2021%2F09%2Fpexels-photo-1370298.jpeg%3Ffit%3D1200%252C801%26ssl%3D1&description=How%20to%20Check%20%27Comp%20Variable%27%20for%20Spaces%20and%20Low-Values)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2021/09/05/how-to-check-comp-variable-for-spaces-and-low-values/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2021/09/05/how-to-check-comp-variable-for-spaces-and-low-values/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2021/09/05/how-to-check-comp-variable-for-spaces-and-low-values/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2021/09/05/how-to-check-comp-variable-for-spaces-and-low-values/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Check%20%27Comp%20Variable%27%20for%20Spaces%20and%20Low-Values&body=https%3A%2F%2Fsrinimf.com%2F2021%2F09%2F05%2Fhow-to-check-comp-variable-for-spaces-and-low-values%2F&share=email)
-

### Like this:

Like Loading…
