summing a sequence

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

summing a sequence

Arthur Kramer

I’m using version 22 and I’m trying to sum the contents of the counter_sum.* fields and have the sum appear in the tot_clsses field, but all that is happening is that whatever in in the counter_sum.1 field is what ends up in the tot_clsses field. That is. all I get when I run “frequencies” is the contents of whatever is in the counter_sum.1 field.  The summed variable I am trying to create is not being created.   Is there something I am missing in the code.

 

COMPUTE tot_clsses=0.

do if counter_sum.1 ge 1.

  compute tot_clsses =tot_clsses+counter_sum.1.

else if (counter_sum.2 ge 1).

  compute tot_clsses=tot_clsses +counter_sum.1 + counter_sum.2.

else if (counter_sum.3 ge 1).

  compute tot_clsses=tot_clsses +counter_sum.1 + counter_sum.2+counter_sum.3.

else if (counter_sum.4 ge 1).

  compute tot_clsses=tot_clsses +counter_sum.1 + counter_sum.2+counter_sum.3+counter_sum.4.

end if.

 

 

Arthur Kramer

 

===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: summing a sequence

Andy W
My guess, Your DO IF will evaluate to TRUE on the first condition, so the subsequent conditions will never be evaluated. E.g. if "counter_sum.2 = 1" only when "counter_sum.1 = 1", then the second ELSE IF statement will never be evaluated, since the first DO IF is always true.

Try this (untested) code:

*************************.
COMPUTE tot_clsses=0.
VECTOR count = counter_sum.1 TO counter_sum.4.
LOOP #i = 1 TO 4.
  IF count(#i) ge 1 tot_clsses = tot_clsses + count(#i).
END LOOP.
*************************.

This presumes that "counter_sum.1 TO counter_sum.4" are in sequential order in the file. Another simple solution would be to use RECODE (or whatever) and transform any negative (and those below 0) numbers into 0 and then SUM them. If you have only integers that approach becomes a bit simpler.

You could also do one the crazy long compute statement like so:

COMPUTE tot = SUM(counter_sum.1*(counter_sum.1 ge 1),counter_sum.2*(counter_sum.2 ge 1), etc...

Because the inner if statement evaluates to 1 if true, and 0 otherwise, so will not add anything to the sum if the condition in the parenethesis is not false.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: summing a sequence

David Marso
Administrator
"My guess, Your DO IF will evaluate to TRUE on the first condition, so the subsequent conditions will never be evaluated."
Indeed!  It will also simply pass thru if there are MISSING values in the relevant variable ;-(

May I request that Arthur post an example of what the data look like and the desired result?
Fixing 'bad' code (whatever that is ;-) is often not the best solution.
Better to see what the actual problem is and fix that.

Andy W wrote
My guess, Your DO IF will evaluate to TRUE on the first condition, so the subsequent conditions will never be evaluated. E.g. if "counter_sum.2 = 1" only when "counter_sum.1 = 1", then the second ELSE IF statement will never be evaluated, since the first DO IF is always true.

Try this (untested) code:

*************************.
COMPUTE tot_clsses=0.
VECTOR count = counter_sum.1 TO counter_sum.4.
LOOP #i = 1 TO 4.
  IF count(#i) ge 1 tot_clsses = tot_clsses + count(#i).
END LOOP.
*************************.

This presumes that "counter_sum.1 TO counter_sum.4" are in sequential order in the file. Another simple solution would be to use RECODE (or whatever) and transform any negative (and those below 0) numbers into 0 and then SUM them. If you have only integers that approach becomes a bit simpler.

You could also do one the crazy long compute statement like so:

COMPUTE tot = SUM(counter_sum.1*(counter_sum.1 ge 1),counter_sum.2*(counter_sum.2 ge 1), etc...

Because the inner if statement evaluates to 1 if true, and 0 otherwise, so will not add anything to the sum if the condition in the parenethesis is not false.
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: summing a sequence

Jon K Peck
In reply to this post by Andy W
Assuming that the data values are nonnegative, and the variables are consecutive in the file, you can just do
compute tot_clsses = sum(counter_sum.1 to counter_sum.4).


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Andy W <[hidden email]>
To:        [hidden email]
Date:        11/03/2014 10:24 AM
Subject:        Re: [SPSSX-L] summing a sequence
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




My guess, Your DO IF will evaluate to TRUE on the first condition, so the
subsequent conditions will never be evaluated. E.g. if "counter_sum.2 = 1"
only when "counter_sum.1 = 1", then the second ELSE IF statement will never
be evaluated, since the first DO IF is always true.

Try this (untested) code:

*************************.
COMPUTE tot_clsses=0.
VECTOR count = counter_sum.1 TO counter_sum.4.
LOOP #i = 1 TO 4.
 IF count(#i) ge 1 tot_clsses = tot_clsses + count(#i).
END LOOP.
*************************.

This presumes that "counter_sum.1 TO counter_sum.4" are in sequential order
in the file. Another simple solution would be to use RECODE (or whatever)
and transform any negative (and those below 0) numbers into 0 and then SUM
them. If you have only integers that approach becomes a bit simpler.

You could also do one the crazy long compute statement like so:

COMPUTE tot = SUM(counter_sum.1*(counter_sum.1 ge
1),counter_sum.2*(counter_sum.2 ge 1), etc...

Because the inner if statement evaluates to 1 if true, and 0 otherwise, so
will not add anything to the sum if the condition in the parenethesis is not
false.




-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/summing-a-sequence-tp5727778p5727779.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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


===================== 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
Reply | Threaded
Open this post in threaded view
|

Re: summing a sequence

Arthur Kramer
In reply to this post by David Marso
Thank you, all, for your advice.

Here is a sample of the data( extra tabs have been inserted for readability because the data were cut and pasted from the data editor):
Counter_sum.1 counter_sum.2 counter_sum.3 counter_sum.4
1.00 1.00 1.00
4.00 1.00 1.00
2.00
1.00 1.00 2.00
1.00 1.00 1.00 1.00
1.00
1.00 1.00
1.00
1.00
1.00 1.00
1.00
1.00
1.00
1.00
1.00
1.00 2.00 1.00
1.00 1.00
1.00 1.00 1.00 1.00

With that said, the simplicity of Jon's response worked beautifully.
Thank you again.


Arthur Kramer



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of David Marso
Sent: Monday, November 03, 2014 1:27 PM
To: [hidden email]
Subject: Re: summing a sequence

"My guess, Your DO IF will evaluate to TRUE on the first condition, so the subsequent conditions will never be evaluated."
Indeed!  It will also simply pass thru if there are MISSING values in the relevant variable ;-(

May I request that Arthur post an example of what the data look like and the desired result?
Fixing 'bad' code (whatever that is ;-) is often not the best solution.
Better to see what the actual problem is and fix that.


Andy W wrote

> My guess, Your DO IF will evaluate to TRUE on the first condition, so
> the subsequent conditions will never be evaluated. E.g. if "counter_sum.2 = 1"
> only when "counter_sum.1 = 1", then the second ELSE IF statement will
> never be evaluated, since the first DO IF is always true.
>
> Try this (untested) code:
>
> *************************.
> COMPUTE tot_clsses=0.
> VECTOR count = counter_sum.1 TO counter_sum.4.
> LOOP #i = 1 TO 4.
>   IF count(#i) ge 1 tot_clsses = tot_clsses + count(#i).
> END LOOP.
> *************************.
>
> This presumes that "counter_sum.1 TO counter_sum.4" are in sequential
> order in the file. Another simple solution would be to use RECODE (or
> whatever) and transform any negative (and those below 0) numbers into
> 0 and then SUM them. If you have only integers that approach becomes a
> bit simpler.
>
> You could also do one the crazy long compute statement like so:
>
> COMPUTE tot = SUM(counter_sum.1*(counter_sum.1 ge
> 1),counter_sum.2*(counter_sum.2 ge 1), etc...
>
> Because the inner if statement evaluates to 1 if true, and 0
> otherwise, so will not add anything to the sum if the condition in the
> parenethesis is not false.





-----
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?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/summing-a-sequence-tp5727778p5727781.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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

=====================
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