A Do Loop can be used to execute a fixed block of statement an indefinite number of times. One common use of DO Loops in a macro is to loop through records in a file until either the end of file is reached or the criteria for the loop is no longer true. The syntax for a DO Loop is:
DO{Condition1 Statement1;
Condition2 Statement2;
Condition3 Statement3}
Where Statement1 will continue to execute as long as Condition1 is true, Statement2 will continue to execute as long as Statement2 is true, etc. The semi-colon (;) represents the else statement. Once Condition1 returns a Nil (“”) value then this condition is no longer considered true and Condition2 will be evaluated. Nested DO Loops are allowed through Magic code.
Example:
In order to find all inpatients in the hospital today, we can loop through the room.bed.index:
room.bed.index[facility,room,bed] = urn
""^facility^bed^urn^room,
IF{c.xx.date'=@.today;
DO{@Next(facility,room.bed.index) 1,
DO{@Next(room,room.bed.index) 1,
DO{@Next(bed,room.bed.index) @room.bed.index^urn,
1^/LIST[urn]}}}}
The DO LOOP can also be written in reverse order. This is often done in order to avoid having the code extend past the right of the page therefore making the code easier to read:
""^facility^bed^urn^room,
IF{c.xx.date'=@.today;
DO{@Next(bed,room.bed.index) @room.bed.index^urn,
1^/LIST[urn];
@Next(room,room.bed.index);
@Next(facility,room.bed.index)}}
Note:
In order to move from one subscript to another subscript, you can utilize the following standard MEDITECH program calls:
@Next Moves to the next subscript
@Prev Moves to the previous subscript
@First Moves to the first subscript
@Last Moves to the last subscript