Sometimes blocks of script items need to be repeated several times. An example of this is asking the same set of questions about each member of a household. Although it is quite possible to do this by using multiple copies of the blocks of items, DO loops allow the script to be drastically simplified.
A DO loop is begun by an item of type DO. This is followed by a series of other item types. The loop is ended by a ENDD item. One of the parameters to the DO item specifies the maximum number of times the DO loop executes. Once this is exceeded the ENDD item becomes unprotected and is then the current item.
Some special conditions apply to DO loops:
The compiler deals with loops by
expanding them into a repeated series of items.
Suppose the DO loop shown in Table 8
is to be repeated 3 times.
When expanding the loop items within it are duplicated and renamed. The compiler also alters the SPSs for the repeated items (see Table 9). Once expanded, items within the loop are treated as any other item by the compiler.
Type | Variable Name | SPS |
DO | LOOPA | |
LOOPA2 | ||
LOOPA3 | ||
INFO | Q11 | LOOPA=1 |
CHCE | Q21 | Q11=1 |
CHCE | Q31 | Q21. |
CHCE | Q41 | Q31. |
INFO | Q12 | Q41. |
CHCE | Q22 | Q12=1 |
CHCE | Q32 | Q22. |
CHCE | Q42 | Q32. |
INFO | Q13 | Q42. |
CHCE | Q23 | Q13=1 |
CHCE | Q33 | Q23. |
CHCE | Q43 | Q33. |
ENDD | ENDLOOPA | Q43. |
The DO item itself generates 3 variables to control the looping. The DO item generates 3 variables within the ANSWERS data set: loopA, loopA2, and loopA3. These are used to control the looping.
If the item is called QNAME, the variables will be named QNAME1, QNAME2, and QNAME3. The variable QNAME1 will contain the highest loop count, the variable QNAME2 will contain the maximum possible loop count, and the variable QNAME3 will contain the current loop count. These variables can be directly referenced within the script.
The script syntax for the SPSs within the loop is the same as normal except that square brackets enclosing an integer are used to indicate the loop count. The square brackets follow an item name for an item that appears within a loop. They are used to reference the values of items within iterations around the loop.
A positive integer within square brackets (e.g. Q2[3]) refers to the item's value on that particular iteration. A negative integer (e.g. Q2[-5]) is used to refer to the item's value on an iteration relative to the current one. A zero integers (e.g. Q2[0]) refers to the item's value on the current iteration. This is also shown in Table 8. For example, a SPS Q2[-1]=3, which would be evaluated to be true in an interview when the answer to Q2 on the previous iteration around the loop was `3'.
Some special conditions apply to square bracket indices:
As explained above, positive indices correspond refer absolutely to a particular iteration. Thus, Q1[3] means the value of Q1 on the 3rd iteration. Negative indices refer in a relative sense to earlier iterations. Thus, Q1[-3] means the value of Q1 3 iterations ago.
It is possible to use indices that do not correspond to a loop iteration. If the maximum number of iterations is 5 then the SPS Q1[7] or Q1[-7] would be incorrect. The compiler will report the SPS Q1[7] as a syntax error. However, the Q1[-7] would be accepted, but the SPS evaluates it as a missing value during the interview.
If a negative index is used, such as Q1[-3], then the item it refers to will not exist until the fourth iteration. In this case the compiler will replace the reference by a missing value. The evaluation of the SPS Q1[-3]=1 and DO33 with successive loop iterations in shown in Table 11.
An internal macro variable called &CURRITER is available that holds the current loop count. So ``Q&CURRITER'' becomes ``Q1'' on the first time round the loop, then ``Q2'' the second time. This can be used to reference loop items as well as external items. The macro variable is illustrated in Table 12.
A loop is always set to execute a maximum number of times. If a loop is supposed to be exited before the maximum loop count is reached then the SPS for the ENDD must be set accordingly. The example in Table 8 could have used: (Q41=1) or (Q42=1) or (Q43=1) or (Q44=1), which would allow the loop to be exited from any of the four iterations as long as the answer to Q4 was code 1.
Changing the texts for questions on successive iterations can be done by using answer-quoting15. One way this could be done is to quote the loop count as in the question:
``How old is person ^LOOPA3^ in your household?''This appears in consecutive loops as:
``How old is person 1 in your household?''
``How old is person 2 in your household?''
``How old is person 3 in your household?''
Alternatively, a SCAL item can be used to supply the question texts. Suppose the SCAL contains the text to be quoted as four character strings. These strings can be quoted by using the loop count to refer to a particular SCAL item value. Suppose the SCAL item contained the 4 texts: ``man'', ``woman'', ``boy'', and ``girl'' and that a loop that refers to it runs 4 times. If the single question in the script is:
``Is person ^LOOPA3^ in your household a ^SCAL[0]^?''then the following questions will appear as the loop iterates:
``Is person 1 in your household a man?''
``Is person 1 in your household a woman?''
``Is person 1 in your household a boy?''
``Is person 1 in your household a girl?''
``Is person 2 in your household a man?''
``Is person 2 in your household a woman?''
``Is person 2 in your household a boy?''
``Is person 2 in your household a girl?''
``Is person 3 in your household a man?''
``Is person 3 in your household a woman?''
``Is person 3 in your household a boy?''
``Is person 3 in your household a girl?''
``Is person 4 in your household a man?''
``Is person 4 in your household a woman?''
``Is person 4 in your household a boy?''
``Is person 4 in your household a girl?''