Loop suffixes
How loop iteration codes attach to export variable names. Underscore is reserved exclusively for the loop suffix — `Q1r1_3` means "row 1 of Q1 in loop iteration 3." Nested loops concatenate outer-to-inner. Predictable, parseable, one rule.
The rule
Every question that lives inside a - loop: gets an underscore-separated iteration code appended to its variable name — the last thing on the name, always:
Q1 _3 # bare question Q1, iteration 3
Q1r2 _3 # gridSingle row 2 of Q1, iteration 3
Q1r1o2 _3 # gridMulti (row 1, code 2) of Q1, iteration 3
Q1r1c2 _3 # gridNumber (row 1, col 2) of Q1, iteration 3
The suffix is the iteration code — the option code from the loop’s options: / group: list — not a sequence number. A loop over {1: US, 3: DE, 4: JP} produces suffixes _1, _3, _4 regardless of order, not _1, _2, _3.
r / c / o). This is what makes str.split('_') give the body and the optional iteration code, always, no edge cases.See Column naming for the alphanumeric-qid rule and the structural prefixes.
Nested loops — outer to inner
Iteration codes concatenate outer-to-inner with underscores in between:
Q5r1_2_4 # gridSingle row 1 of Q5, outer iteration 2, inner iteration 4
Q5_2_4 # bare Q5, outer 2, inner 4
The outer code comes first, matching the order of the loop nesting in the DSL. For a retailer × department nesting (retailer outer, department inner), QExperience_1_2r1 = retailer 1, department 2, row 1 of the gridSingle.
Three-level nesting works the same way — Q9_1_2_3 for outer 1, middle 2, inner 3 — but is rare in practice.
What an unreached iteration exports
Loop iterations are driven by options.from: (or the loop’s authored domain if no filter is set). For a respondent whose filter didn’t include iteration code N, the columns Q1_N, Q1r1_N, etc. still appear in the wide file — they just render blank / null.
This is a wide-format invariant: every respondent’s row has the same columns. The column set is a function of the survey’s authored domain, not any one respondent’s flow. So:
- Someone who ran iterations 1, 3, 4 sees
Q1_1= 5,Q1_2= null,Q1_3= 7,Q1_4= 6. - Someone who ran no iterations (empty
options.from:) sees all four columns as null. - The full authored domain of the loop drives the column set — even codes no respondent ran leave columns in the file (that’s how you preserve the domain across a fielding).
For multi / gridMulti inside a loop, the null propagates through the tri-state projection — unshown option cells are null, not 0.
Loop materialization: one row per iteration
Since v3.10.4 the runtime materializes loop answers as one storage row per iteration, not one row per changed answer. That’s the invariant every downstream tool builds on:
- Wide export column counts are stable across resubmits (no “orphan rows” from re-entering the loop).
- Long-format loop exports (see below) produce one row per (respondent, iteration) — never duplicates.
- Reporting cuts by loop iteration are well-defined.
When to switch to long format
Wide + loop suffix is honest and portable, but it explodes column count fast. A 3-brand × 5-attribute loop with 4 gridSingle rows already produces 60 columns per outer iteration bundle. For anything CBC-scale (12 tasks × 3 concepts) or iteration-count-varies-a-lot across respondents (a media loop where some diary-fill 3 things and others 30), the wide file becomes unwieldy.
The alternative: pass ?long=<loopId> to the export endpoint. That loop is removed from the wide file and shipped as its own long companion — one row per (respondent, iteration) — while the rest of the survey stays wide. Never both. See Long format.
Cross-iteration access in scripts
Wide-file suffixes and the runtime accessor share the same shape: the trailing-underscore proxy in DSL scripts reads iteration slots the same way the export names them.
QRating_1.value # matches column QRating_1 in the export
QRating_1_2.value # matches QRating_1_2 (nested)
QRating_[c].value # dynamic — c is a variable
QRating_[o][i].value # nested, dynamic outer + inner
See Loops → Reading a specific iteration’s answer for the full accessor surface.
The predictability contract
Because underscore is reserved for the loop suffix, three properties hold:
str.split('_')on any variable name returns a list of[body](no loop) or[body, iter1](one loop) or[body, iter1, iter2](nested) — nothing else.- A trailing underscore-plus-digits is always a loop iteration code — never a row code, an option code, or a specify suffix.
- Predictable programmatic column construction — knowing the qid, the row / column / option position, and the loop iteration, you can build the column name mechanically. See the
format_colsnippet on Column naming.
See also
- Column naming — the full mechanical rule that produces the pre-suffix body
- Tri-state projection — what
nullmeans when an iteration didn’t run - Long format — stacking a loop out of the wide file into its own companion
- Loops — authoring loops in the DSL