How to Fix COBOL Value-clause and UNSTRING Errors

Here is the resolution for the Value clause and Unstring errors in COBOL. In COBOL, there are different data definition clauses. Out of those, the VALUE clause is one of them. Its purpose is to define values before the program process. That means during program-loading value will assign to that variable.

COBOL Value clause.

Value-clause example

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 can hold. I asked the same question in an interview about what will happen. No one answered correctly.

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

Do you know?

The Top Differences Between Unique Vs Clustered Index.

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 give a 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 the definition length is short. So these things you will know during execution time.

Unstring example

01 Daystr    pic x(02).
01 MonthStr  pic x(02.
01 YearStr   pic x(02).

MOVE "25-07-2013" 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

Related

Author: Srini

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