Summing a variable.

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

Summing a variable.

drfg2008
This post was updated on .
SPSS 20 Windows

I have the variable "goals", which measures the goals at each time (see below: one goal at each time) and I just want the variable "goals" to be added up (like in the variable "GoalSum").

Goal  GoalSum Minutes

1 1 3
. 1 4
. 1 5
1 2 6
1 3 7
. 3 8
1 4 9
1 5 10
1 6 11
. 6 12
. 6 13
. 6 14
. 6 15
. 6 16
1 7 17
1 8 18
. 8 19
1 9 20
. 9 21
1 10 22


Thanks for help.

Frank
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Summing a variable.

David Marso
Administrator
Once again, LAG is your friend.
COMPUTE GOALSUM=SUM(GOAL,LAG(GOALSUM)).

drfg2008 wrote
SPSS 20 Windows

I have the variable "goals", which measures the goals at each time (see below: one goal at each time) and I just want the variable "goals" to be added up (like in the variable "GoalSum").

Goal  GoalSum Minutes

1 1 3
. 1 4
. 1 5
1 2 6
1 3 7
. 3 8
1 4 9
1 5 10
1 6 11
. 6 12
. 6 13
. 6 14
. 6 15
. 6 16
1 7 17
1 8 18
. 8 19
1 9 20
. 9 21
1 10 22


Thanks for help.

Frank
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 variable.

Bruce Weaver
Administrator
I was thinking of CREATE with the CSUM function, but David's solution is a lot neater.  Note that it is the use of SUM that makes it work.  I.e., the following would not work, because LAG(GOALSUM) = SYSMIS on the first row.

COMPUTE GOALSUM=GOAL + LAG(GOALSUM).

Here's a more complete example of the difference.

data list list (",") / goal minutes.
begin data
1, 3
,  4
,  5
1, 6
1, 7
,  8
1, 9
1, 10
1, 11
,  12
,  13
,  14
,  15
,  16
1, 17
1, 18
,  19
1, 20
,  21
1, 22
end data.

COMPUTE GOALSUM1=SUM(GOAL,LAG(GOALSUM1)). /* David's method.
COMPUTE GOALSUM2= GOAL + LAG(GOALSUM2). /* Simple addition rather than SUM().
COMPUTE LagGS1 = LAG(goalsum1).
COMPUTE LagGS2 = LAG(goalsum2).
FORMATS all(f5.0).
LIST.

OUTPUT:

 goal minutes GOALSUM1 GOALSUM2 LagGS1 LagGS2

    1      3        1        .       .      .
    .      4        1        .       1      .
    .      5        1        .       1      .
    1      6        2        .       1      .
    1      7        3        .       2      .
    .      8        3        .       3      .
    1      9        4        .       3      .
    1     10        5        .       4      .
    1     11        6        .       5      .
    .     12        6        .       6      .
    .     13        6        .       6      .
    .     14        6        .       6      .
    .     15        6        .       6      .
    .     16        6        .       6      .
    1     17        7        .       6      .
    1     18        8        .       7      .
    .     19        8        .       8      .
    1     20        9        .       8      .
    .     21        9        .       9      .
    1     22       10        .       9      .

Number of cases read:  20    Number of cases listed:  20



David Marso wrote
Once again, LAG is your friend.
COMPUTE GOALSUM=SUM(GOAL,LAG(GOALSUM)).

drfg2008 wrote
SPSS 20 Windows

I have the variable "goals", which measures the goals at each time (see below: one goal at each time) and I just want the variable "goals" to be added up (like in the variable "GoalSum").

Goal  GoalSum Minutes

1 1 3
. 1 4
. 1 5
1 2 6
1 3 7
. 3 8
1 4 9
1 5 10
1 6 11
. 6 12
. 6 13
. 6 14
. 6 15
. 6 16
1 7 17
1 8 18
. 8 19
1 9 20
. 9 21
1 10 22


Thanks for help.

Frank
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Summing a variable.

drfg2008
In reply to this post by David Marso
thanks
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Summing a variable.

David Marso
Administrator
In reply to this post by Bruce Weaver
Anticipating the next drfg question "What if I have multiple ID's stacked blah blah blah...?"
BEGIN DATA
1 . . 1 1 . 1 1 1 . . . . . 1 1 . 1 . 1
1 1 1 1 . . 1 1 . 1 1 1 . . . . . 1 1 .
END DATA.
COMPUTE ID=TRUNC(($CASENUM-.001)/20)+1.
COMPUTE GOALSUM=SUM(GOAL,LAG(GOALSUM)*(LAG(ID) EQ ID)).
LIST.

    GOAL       ID  GOALSUM

    1.00     1.00     1.00
     .       1.00     1.00
     .       1.00     1.00
    1.00     1.00     2.00
    1.00     1.00     3.00
     .       1.00     3.00
    1.00     1.00     4.00
    1.00     1.00     5.00
    1.00     1.00     6.00
     .       1.00     6.00
     .       1.00     6.00
     .       1.00     6.00
     .       1.00     6.00
     .       1.00     6.00
    1.00     1.00     7.00
    1.00     1.00     8.00
     .       1.00     8.00
    1.00     1.00     9.00
     .       1.00     9.00
    1.00     1.00    10.00
    1.00     2.00     1.00
    1.00     2.00     2.00
    1.00     2.00     3.00
    1.00     2.00     4.00
     .       2.00     4.00
     .       2.00     4.00
    1.00     2.00     5.00
    1.00     2.00     6.00
     .       2.00     6.00
    1.00     2.00     7.00
    1.00     2.00     8.00
    1.00     2.00     9.00
     .       2.00     9.00
     .       2.00     9.00
     .       2.00     9.00
     .       2.00     9.00
     .       2.00     9.00
    1.00     2.00    10.00
    1.00     2.00    11.00
     .       2.00    11.00




Bruce Weaver wrote
I was thinking of CREATE with the CSUM function, but David's solution is a lot neater.  Note that it is the use of SUM that makes it work.  I.e., the following would not work, because LAG(GOALSUM) = SYSMIS on the first row.

COMPUTE GOALSUM=GOAL + LAG(GOALSUM).

Here's a more complete example of the difference.

data list list (",") / goal minutes.
begin data
1, 3
,  4
,  5
1, 6
1, 7
,  8
1, 9
1, 10
1, 11
,  12
,  13
,  14
,  15
,  16
1, 17
1, 18
,  19
1, 20
,  21
1, 22
end data.

COMPUTE GOALSUM1=SUM(GOAL,LAG(GOALSUM1)). /* David's method.
COMPUTE GOALSUM2= GOAL + LAG(GOALSUM2). /* Simple addition rather than SUM().
COMPUTE LagGS1 = LAG(goalsum1).
COMPUTE LagGS2 = LAG(goalsum2).
FORMATS all(f5.0).
LIST.

OUTPUT:

 goal minutes GOALSUM1 GOALSUM2 LagGS1 LagGS2

    1      3        1        .       .      .
    .      4        1        .       1      .
    .      5        1        .       1      .
    1      6        2        .       1      .
    1      7        3        .       2      .
    .      8        3        .       3      .
    1      9        4        .       3      .
    1     10        5        .       4      .
    1     11        6        .       5      .
    .     12        6        .       6      .
    .     13        6        .       6      .
    .     14        6        .       6      .
    .     15        6        .       6      .
    .     16        6        .       6      .
    1     17        7        .       6      .
    1     18        8        .       7      .
    .     19        8        .       8      .
    1     20        9        .       8      .
    .     21        9        .       9      .
    1     22       10        .       9      .

Number of cases read:  20    Number of cases listed:  20



David Marso wrote
Once again, LAG is your friend.
COMPUTE GOALSUM=SUM(GOAL,LAG(GOALSUM)).

drfg2008 wrote
SPSS 20 Windows

I have the variable "goals", which measures the goals at each time (see below: one goal at each time) and I just want the variable "goals" to be added up (like in the variable "GoalSum").

Goal  GoalSum Minutes

1 1 3
. 1 4
. 1 5
1 2 6
1 3 7
. 3 8
1 4 9
1 5 10
1 6 11
. 6 12
. 6 13
. 6 14
. 6 15
. 6 16
1 7 17
1 8 18
. 8 19
1 9 20
. 9 21
1 10 22


Thanks for help.

Frank
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 variable.

drfg2008
David,

you are always right. Except this time.

I have different games in one file. And this is how I would solve the problem:

DO IF section = 1 & timeRange = 0.
COMPUTE GOALSUM1 = Counts_PointTeam1.
ELSE.
COMPUTE GOALSUM1=SUM(Counts_PointTeam1,LAG(GOALSUM1)).
END IF.
EXECUTE.


It works.

Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: Summing a variable.

David Marso
Administrator
I am correct here as well ;-)
I don't know what section or timerange are but my ID variable is merely a proxy for such concerns.
If you were clever you could map that to an equally elegant one liner rather than that clumsy DO IF abomination ;-))

drfg2008 wrote
David,

you are always right. Except this time.

I have different games in one file. And this is how I would solve the problem:

DO IF section = 1 & timeRange = 0.
COMPUTE GOALSUM1 = Counts_PointTeam1.
ELSE.
COMPUTE GOALSUM1=SUM(Counts_PointTeam1,LAG(GOALSUM1)).
END IF.
EXECUTE.


It works.
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 variable.

David Marso
Administrator
In lieu of "clumsy DO IF biz"...;-)))
COMPUTE GOALSUM2=
     SUM(Counts_PointTeam1,LAG(GOALSUM2)*NOT(section EQ 1 AND timeRange EQ 0 )).

David Marso wrote
I am correct here as well ;-)
I don't know what section or timerange are but my ID variable is merely a proxy for such concerns.
If you were clever you could map that to an equally elegant one liner rather than that clumsy DO IF abomination ;-))

drfg2008 wrote
David,

you are always right. Except this time.

I have different games in one file. And this is how I would solve the problem:

DO IF section = 1 & timeRange = 0.
COMPUTE GOALSUM1 = Counts_PointTeam1.
ELSE.
COMPUTE GOALSUM1=SUM(Counts_PointTeam1,LAG(GOALSUM1)).
END IF.
EXECUTE.


It works.
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 variable.

drfg2008
This post was updated on .
it seems to be even faster, thanks


(If there was a way to program commands with less than one line, I'm sure you would do that)
Dr. Frank Gaeth