Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perform Operations with COMP variables

Status
Not open for further replies.

IonVlad

Programmer
May 16, 2007
3
CA
Hello,

I am trying to perform arithmetical calculations on a COMP variable and, so far, have been unsuccessful. I have also tried to perform the required calculations on a normal variable and then MOVE the result to a COMP variable, again, without success.

Could somebody tell me if this is at all possible to accomplish this in COBOL?

Thank you,
Ion
 
is at all possible to accomplish this in COBOL
Accomplish WHAT ?
Why not post the code and explain us what's going wrong ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Here's the code sample:

05 PARM-DATE-YEAR PIC 9(04) VALUE 1995.

05 CONV-NON-APPL-ISSUE-DATE
PIC S9(08) COMP VALUE 20010304.
05 CONV-REF-NON-APPL-ISSUE-DATE.
10 CONV-NON-APPL-ISSUE-YR PIC 9(04).
10 CONV-NON-APPL-ISSUE-MM PIC 9(02) VALUE 03.
10 CONV-NON-APPL-ISSUE-DD PIC 9(02) VALUE 04.

COMPUTE CONV-NON-APPL-ISSUE-YR = PARM-DATE-YEAR - 3.
MOVE CONV-REF-NON-APPL-ISSUE-DATE TO
CONV-NON-APPL-ISSUE-DATE
DISPLAY "CONV-NON-APPL-ISSUE-DATE " CONV-NON-APPL-ISSUE-DATE

So, I want to change the value of CONV-NON-APPL-ISSUE-DATE to display 19920304. To do that, I am performing the COMPUTE on a separate, non-COMP, variable (CONV-NON-APPL-ISSUE-YR)and then MOVING the result back to the COMP variable. At the end of it all, the DISPLAY shows:"00000000". I know, for a fact, that the COMPUTE on the regular (non-COMP) variable works fine, but the move to a COMP variable does not work.

How can I move the result to the COMP variable, CONV-NON-APPL-ISSUE-DATE?

Thanks
Ion

 
You are moving a text field to the comp value, which won't work out well. Any group field is considered a text value. So effectively, you are moving a PIC X(8) to the S9(08) comp field.

Try redefining the group field to be a 9(8)
 

Also some (most) compilers have problems displaying comp fields.
Try moving the data to the comp field and then move it to a 9(8) field. Then display the new non comp field.

Tom
 
what about this ?
Code:
05 PARM-DATE-YEAR             PIC 9(04) VALUE 1995.  
05  CONV-NON-APPL-ISSUE-DATE  PIC S9(08) COMP VALUE 20010304.
05  CONV-REF-NON-APPL-ISSUE-DATE.          
    10 CONV-NON-APPL-ISSUE-YR PIC 9(04).   
    10 CONV-NON-APPL-ISSUE-MM PIC 9(02) VALUE 03.   
    10 CONV-NON-APPL-ISSUE-DD PIC 9(02) VALUE 04.    
[!]05  CONV-REF-NON-APPL-ISSUE-DATE-NUM REDEFINES
    CONV-REF-NON-APPL-ISSUE-DATE PIC 9(08).[/!]

COMPUTE CONV-NON-APPL-ISSUE-YR  = PARM-DATE-YEAR - 3.
MOVE CONV-REF-NON-APPL-ISSUE-DATE[!]-NUM[/!]  TO                       
                             CONV-NON-APPL-ISSUE-DATE
DISPLAY "CONV-NON-APPL-ISSUE-DATE " CONV-NON-APPL-ISSUE-DATE

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You are moving a text field to the comp value, which won't work out well. Any group field is considered a text value. So effectively, you are moving a PIC X(8) to the S9(08) comp field.

Try redefining the group field to be a 9(8)"

This works!!

Thank you everyone for your help!
 
The problem you are having is that you are confused about the internal representation of COMP fields. Standard COBOL rules are that the internal representation of a COMP field is [compiler] implementer defined, and is suposedly the most efficient form for the hardware to perform arithmetic computations. This means that it varies from vendor to vendor. This is generally not true, as most COBOL vendors define COMP fields as small-endian binary to conform to IBM, the formerly largest vendor for COBOL compilers. In this format, S9(8) COMP VALUE 20010304 is [sub]x[/sub]0001EACC0. There is no way to divide this binary number into year, month and day fields. To accompilish such division, a date field must be defined as a decimal or display format. The only decimal format that I know of that works is unsigned COMP-6, which is available with some compilers.

In the early 80's, Microsoft published their own compiler in which COMP was implemented as each digit using one byte, with the value encoded as BCD. This would work great for dates, except that it took as much memory as display, and the compiler only ran under CPM.
 
Not that it matters a whole lot but the hex equivalent of decimal 20010304 is 01315540.

You could work with this binary field without moving to a zoned decimal if you wanted to. You're trying to subtract 3 years from the date. If you treat the date as an integer such as 20010304, the units position of the year is actually in the 10 thousands position of the date integer. Subtracting 30000 from that integer is the same as subtracting 3 from the year alone.

05 CONV-NON-APPL-ISSUE-DATE
PIC S9(08) COMP VALUE 20010304.

COMPUTE CONV-NON-APPL-ISSUE-DATE
= CONV-NON-APPL-ISSUE-DATE - 30000.

This will yield the value of 19980304 stored in the COMP field.


 
You are right on both counts, Kenny. I must have keyed the date in wrong. I did it again this morning and got what you said. And I didn't analize the problem enough in general.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top