MainframeMaster

AND/OR Conditions in INCLUDE and OMIT

In DFSORT, INCLUDE and OMIT let you filter records with a single COND= parameter. When you need to test more than one thing—for example "keep records where department is 10 and amount is positive," or "omit records where status is A or B"—you combine conditions using the keywords AND and OR. Each condition is written as a 5-tuple: (start, length, format, operator, value). You place AND or OR between conditions inside the same COND=. This page covers the syntax in detail: how to format the parentheses, where to put commas, how to continue long lines, and how to group conditions when you mix AND and OR. After reading this and the Boolean logic page, you will be able to write complex filters without leaving control statements.

INCLUDE / OMIT Advanced Filtering
Progress0 of 0 lessons

Basic Form of a Condition

Each "condition" in COND= is a single comparison: a field in the record tested against a value. The form is (start, length, format, operator, value). Start is the starting byte position (1-based). Length is the length of the field in bytes. Format is how to interpret the bytes: CH (character), PD (packed decimal), ZD (zoned decimal), BI (binary), etc. Operator is EQ, NE, GT, GE, LT, or LE. Value is the constant to compare to—for example C'ABC' for character or a numeric constant. When you combine two such conditions, you put the keyword AND or OR between them, separated by commas: (condition1,AND,condition2) or (condition1,OR,condition2).

AND: Syntax and Examples

AND means both conditions must be true. In COND= you write the first condition (full 5-tuple), then a comma, then the keyword AND, then a comma, then the second condition. Example: keep records where bytes 1–2 equal '01' and the packed-decimal field at 20–23 is greater than zero:

text
1
INCLUDE COND=(1,2,CH,EQ,C'01',AND,20,4,PD,GT,0)

Another example: keep only records where bytes 15–17 are 'ABC' and the 2-byte binary at 25–26 is greater than 5. Here we use a hex constant for the binary comparison:

text
1
2
INCLUDE COND=(15,3,CH,EQ,C'ABC',AND, 25,2,BI,GT,X'0005')

The second condition starts on the next line. Continuation is allowed; the exact rule (e.g. comma at end of line, no leading comma on next line) depends on your DFSORT version—see your manual.

OR: Syntax and Examples

OR means at least one condition must be true. You write the first condition, then comma, OR, comma, then the second condition. To keep records where a single-byte field at position 10 is either 'A' or 'B', you test the same field twice with different values:

text
1
INCLUDE COND=(10,1,CH,EQ,C'A',OR,10,1,CH,EQ,C'B')

To keep records where region (bytes 5–6) is 'NE' or 'NW':

text
1
INCLUDE COND=(5,2,CH,EQ,C'NE',OR,5,2,CH,EQ,C'NW')

You can chain more than two conditions: cond1,OR,cond2,OR,cond3 keeps records where any one of the three is true.

AND vs OR: When to Use Which

AND and OR: meaning and typical use
OperatorMeaningTypical use
ANDBoth conditions must be trueNarrow the set (e.g. region=North AND amount>100)
ORAt least one condition must be trueWiden the set (e.g. code=A OR code=B)

Use AND when you want to narrow the set: every criterion must be met. Use OR when you want to widen the set: any one of several criteria is enough. Mixing them requires parentheses so the grouping is clear.

Parentheses for Mixed AND and OR

When you have both AND and OR in one COND=, use parentheses to group subexpressions. For example, "keep records where (dept=10 and amount>0) or (dept=20 and amount>100)". In DFSORT-style syntax you could write:

text
1
2
3
INCLUDE COND=((10,2,CH,EQ,C'10',AND,20,4,PD,GT,0), OR, (10,2,CH,EQ,C'20',AND,20,4,PD,GT,100))

The inner parentheses group the two AND pairs; the OR connects the two groups. Parenthesis and continuation rules are product-specific—consult the DFSORT Application Programming Guide for your release.

OMIT with AND/OR

OMIT uses the same COND= syntax. OMIT COND=(cond1,AND,cond2) omits (drops) records when both conditions are true. OMIT COND=(cond1,OR,cond2) omits records when either condition is true. Example: drop all records where byte 7 is 'X' or 'Y':

text
1
OMIT COND=(7,1,CH,EQ,C'X',OR,7,1,CH,EQ,C'Y')

So you do not need different syntax for OMIT; only the meaning changes (omit vs include when the condition is true).

Continuation and Formatting

Long COND= expressions can be continued to the next line. Common practice is to break after a comma (e.g. after the first full condition or after AND/OR). The continued line usually does not start with a comma. Some systems use a specific continuation character in column 72 or elsewhere. Incorrect continuation (e.g. a leading comma on the new line) can cause syntax errors. Always verify with your installation's DFSORT documentation.

Field Positions and INREC

The (start,length) in each condition refer to the record as seen at the time INCLUDE/OMIT is applied. If you use INREC, that is after INREC has reformatted the record. So if INREC builds a new 50-byte record, your condition positions are 1–50 in that reformatted record, not the original input positions. If you do not use INREC, positions refer to the original input record.

Explain It Like I'm Five

AND is like a checklist: you must have a hat and a coat to go out. If you forget one, you don't go. OR is like having two doors: you can go out door A or door B; either one is fine. In DFSORT we write these as (rule1,AND,rule2) or (rule1,OR,rule2). The commas and the words AND and OR tell the sort program how to combine the rules. Parentheses are like putting a fence around a group so the program knows "do this group first, then combine it with the other group."

Exercises

  1. Write INCLUDE COND= to keep records where bytes 1–3 are 'USA' and bytes 40–43 (PD) are not equal to 0.
  2. Write OMIT COND= to drop records where byte 5 is '0' or '9'.
  3. How would you keep records where (bytes 10–11 = '01' and bytes 30–33 > 100) or (bytes 10–11 = '02' and bytes 30–33 > 200)? Use parentheses in your answer.
  4. If INREC builds a record that is 80 bytes and puts "region" at 1–2 and "amount" at 10–13, write one condition for region='NE' and amount>0 (AND).

Quiz

Test Your Knowledge

1. What is the correct way to write two conditions with AND in INCLUDE COND=?

  • INCLUDE COND=cond1 AND cond2
  • INCLUDE COND=(cond1,AND,cond2)—each condition is a 5-tuple, AND is a keyword between them
  • Two separate INCLUDE statements
  • INCLUDE AND cond1,cond2

2. For OR, how do you keep records where byte 10 is either A or B?

  • Two INCLUDE statements
  • INCLUDE COND=(10,1,CH,EQ,C'A',OR,10,1,CH,EQ,C'B')
  • INCLUDE COND=10,1,CH,EQ,A OR B
  • OR is not supported

3. When continuing a long COND= to the next line, what is a common rule?

  • Start the next line with a comma
  • End the current line with a comma (continuation); do not start the next line with a comma—check your DFSORT manual
  • AND or OR must be at the start of the next line
  • COND= cannot be continued

4. How do you express (field1=1 AND field2=2) OR (field3=3 AND field4=4)?

  • Two INCLUDE statements
  • INCLUDE COND=((field1 cond,AND,field2 cond),OR,(field3 cond,AND,field4 cond))—use parentheses to group
  • Only two conditions allowed
  • Use OMIT for the opposite

5. Can OMIT use the same AND/OR syntax as INCLUDE?

  • No
  • Yes—OMIT COND=(cond1,OR,cond2) omits records when either condition is true
  • Only AND
  • Only for character fields