| Date: | Fri, 25 Feb 2005 19:01:28 -0500 |
| Reply-To: | Richard Ristow <wrristow@mindspring.com> |
| Sender: | "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU> |
| From: | Richard Ristow <wrristow@mindspring.com> |
| Subject: | Coding style: MXLOOPS |
| In-Reply-To: | <5CFEFDB5226CB54CBB4328B9563A12EE012BFC06@hqemail2.spss.com > |
| Content-Type: | text/plain; charset="us-ascii"; format=flowed |
|---|
I've changed the thread name, because this goes from the particular to
the general.
At 12:26 PM 2/25/2005, Peck, Jon wrote ("RE: Leaving out weekend days
in count of days"), responding to a comment of mine:
>>>compute days=0.
>>>format days(f8.0).
>>>loop #index = startdate to enddate-1
>>> by time.days(1).
>>>. if (xdate.wkday(#index) ne 1
>>> and xdate.wkday(#index) ne 7) days = days+1.
>>>end loop.
>>
>>Sweet! But do you need to SET MXLOOPS? Syntax manual: "The indexing
>>clause[, which you have,] overrides the maximum number of loops
>>specified by SET MXLOOPS."
>
>You are right. SET MXLOOPS does not matter here. [In] an earlier
>version that didn't have an index but used LOOP IF, it did matter.
>Thanks for pointing that out.
The broad point: you can make any SPSS LOOP structure independent of
MXLOOPS. If you have an indexing clause, it's already independent. If
there's no indexing clause, syntax like
. SET MXLOOPS = 1000.
. LOOP IF <whatever condition>.
. <All sorts of stuff>
. END LOOP.
can be recast as
. LOOP #PASS = 1 To 1000 IF <whatever condition>.
. <All sorts of stuff>
. END LOOP.
I recommend this.
- Specifying 'locally' rather than 'globally', where workable, usually
gives clearer and more reliable code. In this case, you're solving your
problem where it needs to be solved, and not modifying a global
parameter (MXLOOPS) with unknown consequences elsewhere.
- It's a good habit to think about maximum passes for every loop. Among
other things, it avoids subtle bugs when a loop hits MXLOOPS
unexpectedly. (See thread "curious little problem", 11 Jan 2005 and
following.)
Would I take MXLOOPS out of SPSS? No, I wouldn't. It's a useful
'safety', even if best practice gives the same or better safety, with a
little more care, without MXLOOPS.
Should SPSS warn when a loop terminates on hitting MXLOOPS? A harder
question. It's like a lot of 'discipline' questions, such as warnings
on using an undeclared variable: warnings for 'undisciplined' code
helps a lot when you're trying to follow the disciplines; and following
them (and knowing you're doing it) makes for a lot fewer mistakes in
complicated code. On the other hand, having the disciplines enforced
can be maddening to the beginner, or even well-experienced person,
trying to just make fairly simple code work, quickly.
Sometimes, there are parameters to turn warnings on and off. It's not
been part of the SPSS philosophy, but there'd be a point to it: a SET
command, for whether to warn when a MXLOOP limit is reached.
|