Ello mate, and welcome to the boards.
I'm sure someone will correct me if I'm wrong- folk know a lot here, but...
Are you sure the "WHERE" command is right.. isn't it "WHILE"
I'm not too sure about your particular settup and only have a couple of mins before I leave work.. will look at it more tomorrow, sorry I only have a couple of mins to spare, but I'm sure the conditional loop is "WHILE" not "WHERE"?
Search on here or Google the Web for the rather excellent Macro Programming guide by Scott Martinez, but I dunno if this pertains to your particular settup.
Also.. beware of global and local variables- some change with every call or exit from a subroutine (some stay the same).
Will post more tomorrow, and welcome here again!