MainframeMaster

Complex Conditional Expressions in INCLUDE and OMIT

When you combine more than two conditions in DFSORT INCLUDE or OMIT COND=, and mix AND and OR, the order of evaluation matters. For example, "keep records where (region is North and amount > 100) or (region is South and amount > 200)" is different from "keep records where region is North and (amount > 100 or region is South) and amount > 200." To get the right logic, you must group conditions using parentheses. In COND=, you group a subexpression by wrapping it in parentheses: ((cond1,AND,cond2),OR,(cond3,AND,cond4)). The inner parentheses form two groups; the OR applies between those groups. Different DFSORT products may support slightly different parenthesis syntax and have default precedence (e.g. AND before OR)—always use parentheses when mixing AND and OR so the intent is clear and correct. This page covers grouping with parentheses, precedence, continuation of long COND= lines, and tips for keeping complex expressions readable.

INCLUDE / OMIT Advanced Filtering
Progress0 of 0 lessons

Why Grouping Matters

With only AND or only OR, the order does not change the result: (A AND B) AND C is the same as A AND (B AND C). When you mix AND and OR, it does: (A AND B) OR C means "(A and B) or C," while A AND (B OR C) means "A and (B or C)." So "(region=North AND amount>100) OR (region=South AND amount>200)" keeps records that satisfy either of the two full clauses. Without parentheses, the parser may use a default precedence (often AND before OR), so cond1,AND,cond2,OR,cond3 might be read as cond1 AND (cond2 OR cond3), which is different. To avoid mistakes, always use parentheses to show exactly how conditions are grouped.

Parenthesis Syntax

In COND=, you wrap one or more conditions (and their AND/OR) in parentheses to form a group. That group is then treated as a single logical value and can be combined with another group or condition using AND or OR. Example: keep records where (field A = 1 and field B = 2) or (field A = 3 and field B = 4). Assume A is at 10, length 1, CH; B at 20, length 1, CH:

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

The first group is (10,1,CH,EQ,C'1',AND,20,1,CH,EQ,C'2') — A=1 and B=2. The second group is (10,1,CH,EQ,C'3',AND,20,1,CH,EQ,C'4') — A=3 and B=4. The two groups are connected by OR. Exact placement of parentheses and commas can vary by product; consult your IBM DFSORT Application Programming Guide.

Precedence When You Do Not Use Parentheses

If you write cond1,AND,cond2,OR,cond3 without parentheses, the parser must decide whether it means (cond1 AND cond2) OR cond3 or cond1 AND (cond2 OR cond3). Many sort products give AND higher precedence than OR, so the expression is evaluated as cond1 AND (cond2 OR cond3). Because this is not universal and can be confusing, the best practice is to always use parentheses when you mix AND and OR. That way the logic is explicit and portable.

Precedence and grouping
ExpressionReadingNote
cond1,AND,cond2,OR,cond3cond1 AND (cond2 OR cond3)If AND has higher precedence
(cond1,AND,cond2),OR,cond3(cond1 AND cond2) OR cond3Explicit grouping

The second row shows that with explicit parentheses you get a single, clear reading.

Continuation of Long COND=

Complex expressions can exceed one line. In JCL or SYSIN, you typically continue by ending the current line with a comma and continuing the next line with the next element (condition or AND/OR). Do not break in the middle of a character constant (e.g. inside C'…'). Some installations require a continuation character (e.g. a comma in column 72 or a specific character in column 1). Check your shop standards and DFSORT documentation. Example with a continuation (syntax may vary by site):

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

The line break after the comma following the first closing parenthesis allows the OR and the second group to sit on the next line. Align and indent for readability.

Nested Grouping

You can nest groups to more than two levels. For example: keep records where ((A=1 OR A=2) AND B=10) OR (C=5 AND D=20). You would write an inner group (A=1 OR A=2), AND that with B=10, then OR that whole group with (C=5 AND D=20). The depth of nesting and the exact parenthesis format are product-dependent; see your manual. Use indentation in your source (e.g. in a copybook or inline comments) to make nested logic easier to follow.

Readability and Maintenance

Complex COND= expressions are hard to read and easy to get wrong. Tips: (1) Build the logic in steps—write the desired truth table or English sentence first, then translate to conditions and groups. (2) Use meaningful comments in your JCL or a separate design doc to explain what each group represents. (3) Prefer INCLUDE when the "keep" condition is simpler than the "omit" condition (or vice versa) so the COND= is shorter. (4) If the expression becomes very long, consider whether a small COBOL or other program that writes a filtered file might be clearer; DFSORT is powerful but complex logic can sometimes be easier to maintain in code.

Explain It Like I'm Five

Imagine you have two rules: "big and red" OR "small and blue." You want things that are either (big and red) or (small and blue). If you don't use brackets, someone might think you mean big and (red or small) and blue, which is different. So we put brackets: (big and red) OR (small and blue). The brackets show that "big and red" is one package and "small and blue" is another, and we want either package. In DFSORT we use parentheses the same way so the computer knows exactly which conditions go together.

Exercises

  1. Write INCLUDE COND= with parentheses to keep records where (status at 5,1,CH = 'A' and amount at 10,4,PD > 0) OR (status = 'B' and amount > 100).
  2. What does cond1,AND,cond2,OR,cond3,AND,cond4 mean with no parentheses if AND has higher precedence than OR?
  3. Rewrite the expression in (1) so the OR group is (status='A' OR status='B') and the AND is with amount>50. Use parentheses.
  4. Where should you break the line when continuing a long COND=, and what should you avoid breaking?

Quiz

Test Your Knowledge

1. How do you express "keep records where (A=1 and B=2) OR (A=3 and B=4)" in INCLUDE COND=?

  • INCLUDE COND=(A=1,AND,B=2,OR,A=3,AND,B=4)
  • INCLUDE COND=((posA,len,fmt,EQ,1,AND,posB,len,fmt,EQ,2),OR,(posA,len,fmt,EQ,3,AND,posB,len,fmt,EQ,4))—parentheses group the two AND pairs
  • Two INCLUDE statements only
  • OR has higher precedence so no parentheses needed

2. Without parentheses, how does DFSORT evaluate cond1,AND,cond2,OR,cond3?

  • (cond1 AND cond2) OR cond3
  • cond1 AND (cond2 OR cond3)—AND often has higher precedence
  • Left to right
  • It is undefined

3. When continuing a long COND= to the next line, what should you do?

  • Start the next line with a comma
  • End the current line with a comma (continuation); next line continues with the next element; do not break inside a constant
  • Put AND/OR at column 1 on next line
  • COND= cannot be continued

4. What is a benefit of using parentheses in complex conditions?

  • Faster execution
  • Clearer intent and correct grouping when mixing AND and OR; avoids reliance on default precedence
  • Smaller SYSIN
  • Required for more than 2 conditions

5. Can you nest more than two levels of parentheses?

  • No
  • Yes—e.g. ((cond1,OR,cond2),AND,(cond3,OR,cond4)) or deeper; product limits may apply
  • Only in INCLUDE
  • Only for numeric fields