Search code examples
cobolmainframe

Comparing what's in a variable to text?


I'm attempting to do some data validation and am trying to use an if statement to see if what is in the variable ERROR-FLAG and RECORD-CODE is "NO" and "VC". Example below..

       MOVE "NO"                   TO ERROR-FLAG.
       MOVE "NO"                   TO ERROR-FLAG2.
       IF VEND-NUM = SPACES
           MOVE "YES"              TO ERROR-FLAG
           MOVE "********" TO BC-AST-OUT
           MOVE "B"  TO B-ERROR-OUT
       END-IF.
       IF VEND-NUM IS NOT NUMERIC AND ERROR-FLAG IS NOO
           MOVE "YES"              TO ERROR-FLAG
           MOVE "********" TO BC-AST-OUT
           MOVE "C"  TO C-ERROR-OUT
       END-IF.
       IF RECORD-CODE IS NOT VC
           MOVE "YES"              TO ERROR-FLAG
           MOVE "**" TO A-AST-OUT
           MOVE "A"  TO A-ERROR-OUT

       END-IF.

NOO AND VC are defined in the working storage as "NO" and "VC" respectively. I can't seem to figure this out, any and all help is much appreciated! I'm not sure if it matters but VEND-NUM and RECORD-CODE are read in.

Error code
   176  IGYPS2074-S   "NOO" was defined as a type that was invalid in this     context.  The statement was discarded.

                  Same message on line:    195    205    210    224

   181  IGYPS2074-S   "VC" was defined as a type that was invalid in this context.  The statement was discarded.

I want it to determine if ERROR-FLAG IS "NO" or not. If it's "NO" I want it to do the following move instructions for the if.


Solution

  • You are using NOO as if it was a CLASS. I will skip what a CLASS is here (but NUMERIC is a CLASS that regroup numeric values "0, 1, 2, 3, 4, 5, 6, 7, 8, 9" for example). I will just explain how to compile your code and make it easier to read and understand.

    I will give you a solution for "ERROR-FLAG" and "NOO", it is the same for "RECORD-CODE" and "VC".

    Here you want to test if the value of "ERROR-FLAG" is equal to "NOO". In COBOL you could literally write:

    IF VEND-NUM IS NOT NUMERIC AND ERROR-FLAG IS EQUAL TO NOO
    

    Also it is may be easier to read this way:

    IF VEND-NUM IS NOT NUMERIC AND ERROR-FLAG = NOO
    

    This is strictly the same.

    In order to make your code more maintainable, I strongly recommend you to use parenthesis like this:

    IF (VEND-NUM IS NOT NUMERIC) AND (ERROR-FLAG = NOO)
    

    Finally, COBOL gives you a great tool: level-88 declaration. In your case, you could declare a level-88 value on ERROR-FLAG like this:

    01 ERROR-FLAG           PIC X(02).
       88 ERROR-FLAG-NOO    value 'NO'.
    

    In this case, when "ERROR-FLAG" contains the value "NO", then ERROR-FLAG-NOO is true (it works like a boolean).

    Your IF statement could then be:

    IF (VEND-NUM IS NOT NUMERIC) AND (ERROR-FLAG-NOO)
    

    That is for the first step: be able to compile your program and make it a bit easier to understand through level-88 values. In a second time, you could use an EVALUATE statement. In you second IF statement you are testing if ERROR-FLAG is "NO" because you don't want to do the second test if the first one is not correct. You could do:

    EVALUTE TRUE
       WHEN VEND-NUM = SPACES
            ...
       WHEN VEND-NUM IS NOT NUMERIC
            ...
       WHEN RECORD-CODE NOT = VC
            ...
       WHEN OTHER
            ...
    END-EVALUATE
    

    In this case, if the first WHEN is true, the code following the when (which I wrote "...") will be executed. The following WHEN will not be tested and the EVALUATE statement will go to "END-EVALUATE". If the first WHEN statement is false, the second WHEN statement will be tested. And so on. If all the WHEN statements are false, the "WHEN OTHER" statement will always be executed. You can find documentation on EVALUATE statement fairly easily on the internet.