|Date: ||Mon, 1 Nov 2004 12:28:09 -0500|
|Reply-To: ||Richard Ristow <firstname.lastname@example.org>|
|Sender: ||"SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>|
|From: ||Richard Ristow <email@example.com>|
|Subject: ||Re: compute within a loop|
|Content-Type: ||text/plain; charset="us-ascii"; format=flowed|
At 11:13 AM 11/1/2004, Jed Teres wrote:
> Can someone tell me what's wrong with this syntax?
>loop #i=1 to 50.
> compute depint(#i)=sum(dep(rlaff(#i)) to dep(rcaff(#i+1)-1)).
> compute manint(#i)=sum(mnc(rlaff(#i)) to mnc(rcaff(#i+1)-1)).
> compute mixint(#i)=sum(mix(rlaff(#i)) to mix(rcaff(#i+1)-1)).
>because I get this error message:
>>Error # 4027 in column 40. Text: to
>>An expression contains an invalid or inappropriate use of the TO
>Why doesn't this work? Please refrain from offering such extremely
>unhelpful advice as "Are you sure you're using SPSS and not Microsoft
>Word? Is your computer turned on?" I come here seeking help as an
>advanced user, not condescension.
You're doing two things, one of which is logical but dubious, and the
other of which I'm pretty sure won't work. Here's one of your COMPUTEs:
compute depint(#i)=sum(dep(rlaff(#i)) to dep(rcaff(#i+1)-1)).
OK: you want to compute that element of depint() as the sum of a
certain range of values of vector dep(). (You don't include the VECTOR
statements for dep(), rlaff(), or rcaff(), but I assume they're
declared, numeric, and have sufficient number of elements. Your error
message doesn't look like VECTOR trouble.)
A. You're double-indexing into dep(), i.e. getting the index by
indexing into a second vector: dep(rlaff(#i)). Most languages that use
indexing handle this fine, but I'm not sure about SPSS.
B. Here's what I'm pretty sure won't work: You're trying to set up your
"TO" list dynamically; that is,
"dep(rlaff(#i)) to dep(rcaff(#i+1)-1)"
refers to a different set of variables at each iteration. I don't think
SPSS can handle this; I think each TO list has to be fixed. I'm not
sure that SPSS can handle even constant vector elements in TO lists,
"dep(4) to dep(17)"
So what do you do? Well, after turning on your computer and invoking
SPSS instead of Microsoft Word, I think you need an inner loop. By the
way, I'm following preferred SPSS practice, using pseudo-indents (lines
beginning "." and then indented to the keyword) rather than actual
indented lines, as you have. Expand your
. compute depint(#i)=sum(dep(rlaff(#i)) to dep(rcaff(#i+1)-1)).
. compute depint(#i) = 0.
. LOOP #DEP_IDX = (rlaff(#i) /* Note 1 */
. compute depint(#i) = depint(#i) /* Note 2 */
*-----or, alternatively ---------------.
. compute depint(#i) = SUM(depint(#i) /* Note 2 */
. END LOOP.
1. A. If LOOP can't handle expressions this complex as lower and upper
limits, compute them separately:
. COMPUTE #LBOUND = rlaff(#i).
. COMPUTE #HBOUNd = rcaff(#i+1)-1.
. LOOP #DEP_IDX = #LBOUND TO #HBOUND
B. Your logic, of course, will work with a single inner loop for all
three of your COMPUTEs
2. These two alternative COMPUTE statements are not equivalent. The
second is more nearly equivalent to what you wanted to do.
3. And do turn on your computer.