Loop through items and sum items

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Loop through items and sum items

huang jialin
Hi,

I have two sets of variables called ITEM 1 to ITEM 47, and another called L1 to L47. What I want to do is to calculate the sum of Ls if any ITEM#i=1. What I wrote is as following:
LOOP
       #i=1 to 47.
       COMPUTE LSUM=0.
       IF (ITEM(#i)=1) LSUM=LSUM+L(#i).
END LOOP.

But I got an error message saying the characters do not match any existing function or vector. What should I do then? Your inputs will be very appreciated.

Thanks.

Sincerely,
Jialin Huang
Reply | Threaded
Open this post in threaded view
|

Re: Loop through items and sum items

Art Kendall
try this untested approach.
numeric Lsum Lcount (f2).
compute Lsum = 0.
compute Lcount = 0.
do repeat item = item1 to item 47
� /L = L1 to L47.
do if item eq 1.
compute lcount = lcount+1
compute lsum = lsum +L.
end repeat.

when you use the LOOP approach L and Item should be Vectors.� � The presetting to zero should be outside the loop.
Art Kendall
Social Research Consultants
On 10/3/2012 10:39 AM, huang jialin wrote:
Hi,

I have two sets of variables called ITEM 1 to ITEM 47, and another called L1 to L47. What I want to do is to calculate the sum of Ls if any ITEM#i=1. What I wrote is as following:
LOOP
� � � � � #i=1 to 47.
� � � � � COMPUTE LSUM=0.
� � � � � � IF (ITEM(#i)=1) LSUM=LSUM+L(#i).
END LOOP.

But I got an error message saying the characters do not match any existing function or vector. What should I do then? Your inputs will be very appreciated.

Thanks.

Sincerely,
Jialin Huang

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Loop through items and sum items

David Marso
Administrator
In reply to this post by huang jialin
You have not declared your vectors.
You also want to initialize LSUM outside of the loop.

huang jialin wrote
Hi,

I have two sets of variables called ITEM 1 to ITEM 47, and another called
L1 to L47. What I want to do is to calculate the sum of Ls if any ITEM#i=1.
What I wrote is as following:
LOOP
       #i=1 to 47.
       COMPUTE LSUM=0.
       IF (ITEM(#i)=1) LSUM=LSUM+L(#i).
END LOOP.

But I got an error message saying the characters do not match any existing
function or vector. What should I do then? Your inputs will be very
appreciated.

Thanks.

Sincerely,
Jialin Huang
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Loop through items and sum items

Art Kendall
In reply to this post by Art Kendall
Yes put "end if." before the "end repeat"
Art Kendall
Social Research Consultants
On 10/3/2012 11:06 AM, huang jialin wrote:
Art,

There are multiple warnings when I ran it. Also, should END IF be associated with DO IF in the syntax?

Thanks.

Jialin Huang

On Wed, Oct 3, 2012 at 9:59 AM, Art Kendall <[hidden email]> wrote:
try this untested approach.
numeric Lsum Lcount (f2).
compute Lsum = 0.
compute Lcount = 0.
do repeat item = item1 to item 47
� /L = L1 to L47.
do if item eq 1.
compute lcount = lcount+1
compute lsum = lsum +L.
end repeat.

when you use the LOOP approach L and Item should be Vectors.� � The presetting to zero should be outside the loop.
Art Kendall
Social Research Consultants
On 10/3/2012 10:39 AM, huang jialin wrote:
Hi,

I have two sets of variables called ITEM 1 to ITEM 47, and another called L1 to L47. What I want to do is to calculate the sum of Ls if any ITEM#i=1. What I wrote is as following:
LOOP
� � � � � #i=1 to 47.
� � � � � COMPUTE LSUM=0.
� � � � � � IF (ITEM(#i)=1) LSUM=LSUM+L(#i).
END LOOP.

But I got an error message saying the characters do not match any existing function or vector. What should I do then? Your inputs will be very appreciated.

Thanks.

Sincerely,
Jialin Huang



===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Loop through items and sum items

Art Kendall
try this
* make up cases and items.
new file.
input program.
� � vector L (47,f1).
� � vector item (47,f2).
� � loop id = 1 to 25.
� � � � � loop #i = 1 to 47.
� � � � � � � � compute item(#i) = rv.bernoulli(.5).
� � � � � � � � compute l(#i) = rnd(rv.normal(50,10)).
� � � � � end loop.
� � � � � end case.
� � end loop.
� � end file.
end input program.
* do this with data once it is made up.
numeric Lsum Lcount (f2).
compute Lsum = 0.
compute Lcount = 0.
do repeat item = item1 to item47
� /L = L1 to L47.
do if item eq 1.
compute lcount = lcount+1.
compute lsum = lsum +L.
end if.
end repeat.
frequencies vars=� lsum lcount.

Art Kendall
Social Research Consultants
On 10/3/2012 12:38 PM, huang jialin wrote:
numeric Lsum Lcount (f2).
compute Lsum = 0.
compute Lcount = 0.
do repeat item = item1 to item 47
� /L = L1 to L47.
do if item eq 1.
compute lcount = lcount+1
compute lsum = lsum +L.
end repeat.

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants