COBOL: Value Clause, Unstring Compilation Errors

A small mistake in assigning value to a variable and unstring may give compilation errors. You will learn in this post how to fix those.


In COBOL, there are different data definition clauses. Out of those, the VALUE clause is one of that. Its purpose is to define values before the program processed. That means during program-loading value will assign to that variable.

COBOL Value clause.

Example for Value clause

DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 999 VALUE ZEROS.
01 Num2 PIC 999 VALUE 15.
01 TaxRate PIC V99 VALUE .35.
01 CustomerName PIC X(15) VALUE "Mike123456789123".

In the above value clause, I have assigned 16 bytes of data ‘Mike123456789123‘. Actually, the definition says, only 15 bytes it can hold. I asked the same question in an interview about what will happen. No one answered correctly.

The answers is it throws compilation error. During compilation time only we need to fix this.

COBOL Unstring

During unstring, the declared output variable may be shorter, and it cannot hold full value. I asked the same question in an interview – what will happen if I give like this.

No one answered. The answer is it will not through compilation error. Only during execution time, according to the definition of ON OVERFLOW, it works. Suppose you did not write ON OVERFLOW logic, then the final string will truncate to the size you defined.

The YearStr data is not as expected. Since definition length is short. So these things you will know during execution time.

Example of Unstring

01 Daystr    pic x(02).
01 MonthStr pic x(02.
01 YearStr pic x(02).
MOVE "25-07-2013lost" TO DateStr. 
UNSTRING DateStr DELIMITED BY "-"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Characters unexamined"
END-UNSTRING.
DISPLAY DayStr SPACE MonthStr SPACE YearStr
DISPLAY "__________________________"
DISPLAY SPACES

Relates Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.