How to process non formatted numeric variable from SYSIN DD from JCL in COBOL

100 Views Asked by At

I coded a very simple COBOL program that should take data from sysin dd * and put it in my WORKING-STORAGE variable, but it doesn’t work as expected.

The problem is when I try to pass a value of 10 to a pic 9(10) variable, coded like this in jcl:

//SYSIN    DD  *
10
/*

I get 1000000000 instead of 0000000010. Is there a simple way to make it work without changing the input data?

Thanks in advance :)

3

There are 3 best solutions below

0
Simon Sobisch On BEST ANSWER

The simple way would be to do a MOVE FUNCTION NUMVAL (ALPHANUMERIC-SYSIN) TO NUMERIC-VAR, possibly followed by a check that the result is not zero (which would be the case for invalid and empty data, as well as an actual zero.
Depending on the version of the compiler (that information is missing in the question) you may be able to use FUNCTION TEST-NUMVAL (ALPHANUMERIC-SYSIN) to do a separate validation - or do it yourself completely.

0
not2savvy On

Presuming that the number is always left-adjusted and followed by spaces, you could right-adjust it first before you move it to the numeric field.

Something like

perform until jclinput(10:1) is not space
  perform varying ind1 from 9 to 1 by -1
     ind 2 = ind1 + 1
     move jclinput(ind1:1) to jclinput(inp2:2)
     move zero to jclinput(1:1)
  end-perform
end-perform

Perhaps not what you had in mind when asking "is there a simple way" though.

1
Rick Smith On

Using ACCEPT to place the SYSIN data directly into a data-item may be done only when the data is properly aligned. See Example of the ACCEPT statement where a three-digit number (PIC 9(3)) is used to ACCEPT a two-digit number. The data in SYSIN has a leading space to provide alignment.

For your example (10), the data in SYSIN would need eight leading spaces or zeros for alignment.

If the data is first ACCEPTed into an alphanumeric data-item, it may then be processed using different commands. That processing may also include validation of the input data.

Simon in another answer has provided a way to use FUNCTION NUMVAL to obtain the value.

UNSTRING may be used as well and for 1 or more fields.

Here, jcl-in is used to hold the record as it may be ACCEPTed from SYSIN. It contains three numbers. Essentially,

    ACCEPT JCL-IN

I then use UNSTRING to separate the three numbers.

Code:

   data division.
   working-storage section.
   01 jcl-in pic x(80).
   01 num1 pic 9(10) value 0.
   01 num2 pic 9(10) value 0.
   01 num3 pic 9(10) value 0.
   procedure division.
       move "10 365 2014" to jcl-in
       unstring jcl-in delimited all space
           into num1 num2 num3
       display num1
       display num2
       display num3
       stop run
       .

Output:

0000000010
0000000365
0000002014