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.
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.
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:
1INCLUDE 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.
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.
| Expression | Reading | Note |
|---|---|---|
| cond1,AND,cond2,OR,cond3 | cond1 AND (cond2 OR cond3) | If AND has higher precedence |
| (cond1,AND,cond2),OR,cond3 | (cond1 AND cond2) OR cond3 | Explicit grouping |
The second row shows that with explicit parentheses you get a single, clear reading.
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):
12INCLUDE 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.
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.
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.
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.
1. How do you express "keep records where (A=1 and B=2) OR (A=3 and B=4)" in INCLUDE COND=?
2. Without parentheses, how does DFSORT evaluate cond1,AND,cond2,OR,cond3?
3. When continuing a long COND= to the next line, what should you do?
4. What is a benefit of using parentheses in complex conditions?
5. Can you nest more than two levels of parentheses?