Running in total

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

Running in total

DEBOER
Hi all, I have a problem to solve. I want to calculate the running total, but for only the last 6 cases (in points) with respect to orgnr and year.
 I solved the cumulative variable as follow.

sort cases by orgnr year.
split file orgnr year.
create cumulative=csum(points).
execute.

The question is how can I create the desire variable so that we only calculate the last six cases in the variable of points?  Please see below at the desire variable
<<thanks in advance.

orgnr points year cumulative desire variable(last 6)
 1     1     2 004     1    
 1     1     2 004     2    
 1     -3     2 004     -1    
 1     -5     2 004     -6    
 1     5     2 004     -1    
 1     7     2 004     6    
 1     2     2 004     8             1    
 1     5     2 004     13     8    
 1     3     2 004     16     19    
 1     2     2 004     18     36    
 1     3     2 004     21     60    
 1     4     2 004     25     82    
 1     5     2 004     30     101    
 1     7     2 004     37     123    
 1     8     2 004     45     147    
 1     4     2 005     4    
 1     2     2 005     6    
 1     3     2 005     9    
 1     -      2 005     9    
 1     3     2 005     12    
 1     7     2 005     19    
 1     8     2 005     27     59    
 1     9     2 005     36     82    
 1     3     2 005     39     112    
 1     -4     2 005     35     142    
 1     -3     2 005     32     168    
 1     3     2 005     35     188    
 1     2     2 005     37     204    
 1     5     2 005     42     214    
 1     4     2 005     46     220    
 2     1     2 004     1    
 2     1     2 004     2    
 2     -3     2 004     -1    
 2     -5     2 004     -6    
 2     5     2 004     -1    
 2     5     2 004     4    
 2     2     2 004     6            -1    
 2     5     2 004     11     4    
 2     3     2 004     14     13    
 2     2     2 004     16     28    
 2     3     2 004     19     50    
 2     3     2 004     22     70    
 2     5     2 004     27     88    
 2     7     2 004     34     109    
 2     8     2 004     42     132    
 2     4     2 005     4    
 2     2     2 005     6    
 2     3     2 005     9    
 2     -      2 005     9    
 2     3     2 005     12    
 2     7     2 005     19    
 2     8     2 005     27     59    
 2     1     2 005     28     82    
 2     3     2 005     31     104    
 2     -4     2 005     27     126    
 2     -3     2 005     24     144    
 2     3     2 005     27     156    
 2     2     2 005     29     164    
 2     5     2 005     34     166    
 2     4     2 005     38     172    
Reply | Threaded
Open this post in threaded view
|

Re: Running in total

Garry Gelade
I'm not exactly sure what you want from looking at your example. However,
first of all I'd number your cases by something like:

COMPUTE const = 1.
COMPUTE casenum =$casenum.
SORT CASES by orgnr year casenum.
split file orgnr year.
create splitcasenum=csum(const).

(casenum is only there to ensure you don't inadvertantly screw up the
original case order)


Then calculate the maximum number of cases within each split (splitcasemax)
using AGGREGATE wih /BREAK = orgnr year using MODE = addvariables and
/splitcasemax = MAX(splitcasenum).

Then something like:
DO IF (splitcasemax - splitcasenum) > 6.
        COMPUTE desire2 = 0.
ELSE.
        COMPUTE desire2 = desire.
END IF.

You will then have the last 6 values in each split in desire2, and you can
calculate a running sum.
split file orgnr year.
create cumdesire=csum(desire2).

Hope this helps.

Garry Gelade


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
DEBOER
Sent: 19 June 2011 14:34
To: [hidden email]
Subject: Running in total

Hi all, I have a problem to solve. I want to calculate the running total,
but
for only the last 6 cases (in points) with respect to orgnr and year.
 I solved the cumulative variable as follow.

sort cases by orgnr year.
split file orgnr year.
create cumulative=csum(points).
execute.

The question is how can I create the desire variable so that we only
calculate the last six cases in the variable of points?  Please see below at
the desire variable
<<thanks in advance.

orgnr points year       cumulative      desire variable(last 6)
 1       1       2 004           1
 1       1       2 004           2
 1      -3       2 004          -1
 1      -5       2 004          -6
 1       5       2 004          -1
 1       7       2 004           6
 1       2       2 004           8               1
 1       5       2 004           13      8
 1       3       2 004           16      19
 1       2       2 004           18      36
 1       3       2 004           21      60
 1       4       2 004           25      82
 1       5       2 004           30      101
 1       7       2 004           37      123
 1       8       2 004           45      147
 1       4       2 005           4
 1       2       2 005           6
 1       3       2 005           9
 1       -      2 005            9
 1       3       2 005           12
 1       7       2 005           19
 1       8       2 005           27      59
 1       9       2 005           36      82
 1       3       2 005           39      112
 1      -4       2 005           35      142
 1      -3       2 005           32      168
 1       3       2 005           35      188
 1       2       2 005           37      204
 1       5       2 005           42      214
 1       4       2 005           46      220
 2       1       2 004           1
 2       1       2 004           2
 2      -3       2 004          -1
 2      -5       2 004          -6
 2       5       2 004          -1
 2       5       2 004           4
 2       2       2 004           6              -1
 2       5       2 004           11      4
 2       3       2 004           14      13
 2       2       2 004           16      28
 2       3       2 004           19      50
 2       3       2 004           22      70
 2       5       2 004           27      88
 2       7       2 004           34      109
 2       8       2 004           42      132
 2       4       2 005           4
 2       2       2 005           6
 2       3       2 005           9
 2       -      2 005            9
 2       3       2 005           12
 2       7       2 005           19
 2       8       2 005           27      59
 2       1       2 005           28      82
 2       3       2 005           31      104
 2      -4       2 005           27      126
 2      -3       2 005           24      144
 2       3       2 005           27      156
 2       2       2 005           29      164
 2       5       2 005           34      166
 2       4       2 005           38      172


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Running-in-total-tp4503507p450
3507.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: Running in total

DEBOER
In reply to this post by DEBOER
thanks for your answer but,

I want to calculate the running in tototal for the previous 6 cases, how can I do that?

orgnr desire
1
2
3
4
5
6
7 21 (1+2+3+4+5+6)
8 27 (2+3+4+5+6+7)
9 33 (3+4+5+6+7+8)
10 39 (4+5+6+7+8+9)
11 45 (5+6+7+8+9+10)
Reply | Threaded
Open this post in threaded view
|

Re: Running in total

Maguin, Eugene
I don't have your original posting (or Gerry's reply) because I thought his
reply provided a solution. But it didn't. As I remember, he used the Create
csum command to compute cumulative sum. This is untested, of course, but I
think you can replace the Do if structure where he made the cumulative sum
computation with the following line.

Compute
lag6sum=lag(desire,1)+lag(desire,2)+lag(desire,3)+lag(desire,4)+lag(desire,5
)+lag(desire,6).

This statement does not include the current value. If you want the sum of
the past six values and including the current value, use this statement.

Compute
lag6sum=desire+lag(desire,1)+lag(desire,2)+lag(desire,3)+lag(desire,4)+lag(d
esire,5).

So for records 1-5 in a set the lag6sum will be sysmis. If you use the first
statement, record 6 will also be sysmis. If you use the second statement, it
won't (shouldn't be).

Does this get to what you want?

Gene Maguin




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
DEBOER
Sent: Monday, June 20, 2011 9:01 AM
To: [hidden email]
Subject: Re: Running in total

thanks for your answer but,

I want to calculate the running in tototal for the previous 6 cases, how can
I do that?

orgnr desire
1
2
3
4
5
6
7       21 (1+2+3+4+5+6)
8       27 (2+3+4+5+6+7)
9       33 (3+4+5+6+7+8)
10      39 (4+5+6+7+8+9)
11      45 (5+6+7+8+9+10)


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Running-in-total-tp4503507p450
6319.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: Running in total

Albert-Jan Roskam
In reply to this post by DEBOER

Hello,

 

data list free / x (f).

begin data

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

end data.

do repeat #s = s1 to s6 / #i = 1 to 6.

do if ($casenum gt 6).

compute #s = lag(x, #i).

end if.

end repeat print.

compute s = sum(s1 to s6).


 
Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: DEBOER <[hidden email]>
To: [hidden email]
Sent: Mon, June 20, 2011 3:01:28 PM
Subject: Re: [SPSSX-L] Running in total

thanks for your answer but,

I want to calculate the running in tototal for the previous 6 cases, how can
I do that?

orgnr desire
1
2
3
4
5
6
7      21 (1+2+3+4+5+6)
8      27 (2+3+4+5+6+7)
9      33 (3+4+5+6+7+8)
10      39 (4+5+6+7+8+9)
11      45 (5+6+7+8+9+10)


--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Running-in-total-tp4503507p4506319.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
Reply | Threaded
Open this post in threaded view
|

Re: Running in total

Garry Gelade
In reply to this post by Maguin, Eugene
OK, now I see what you want to do. Here's how.


data list free / x (f).
begin data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
end data.

CREATE xsum = PMA(x,6).
COMPUTE xsum = xsum*6.
EXECUTE.
LIST.


Result:

x xsum

1   .
2   .
3   .
4   .
5   .
6   .
7   21
8   27
9   33
Etc..

Obviously, you'll need to split your file first.

Garry


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Gene Maguin
Sent: 20 June 2011 15:20
To: [hidden email]
Subject: Re: Running in total

I don't have your original posting (or Gerry's reply) because I thought his
reply provided a solution. But it didn't. As I remember, he used the Create
csum command to compute cumulative sum. This is untested, of course, but I
think you can replace the Do if structure where he made the cumulative sum
computation with the following line.

Compute
lag6sum=lag(desire,1)+lag(desire,2)+lag(desire,3)+lag(desire,4)+lag(desire,5
)+lag(desire,6).

This statement does not include the current value. If you want the sum of
the past six values and including the current value, use this statement.

Compute
lag6sum=desire+lag(desire,1)+lag(desire,2)+lag(desire,3)+lag(desire,4)+lag(d
esire,5).

So for records 1-5 in a set the lag6sum will be sysmis. If you use the first
statement, record 6 will also be sysmis. If you use the second statement, it
won't (shouldn't be).

Does this get to what you want?

Gene Maguin




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
DEBOER
Sent: Monday, June 20, 2011 9:01 AM
To: [hidden email]
Subject: Re: Running in total

thanks for your answer but,

I want to calculate the running in tototal for the previous 6 cases, how can
I do that?

orgnr desire
1
2
3
4
5
6
7       21 (1+2+3+4+5+6)
8       27 (2+3+4+5+6+7)
9       33 (3+4+5+6+7+8)
10      39 (4+5+6+7+8+9)
11      45 (5+6+7+8+9+10)


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Running-in-total-tp4503507p450
6319.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

=====================
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: Running in total

David Marso
Administrator
In reply to this post by DEBOER
It would be SO nice if the examples of desired results actually had any relationship to what is posted!!!
The PMA solution posted should do the trick, but don't count on ESP as a given.
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: Running in total

Albert-Jan Roskam
What's ESP? extrasensory perception??
 
Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: David Marso <[hidden email]>
To: [hidden email]
Sent: Mon, June 20, 2011 6:26:43 PM
Subject: Re: [SPSSX-L] Running in total

It would be SO nice if the examples of desired results actually had any
relationship to what is posted!!!
The PMA solution posted should do the trick, but don't count on ESP as a
given.

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Running-in-total-tp4503507p4506979.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
Reply | Threaded
Open this post in threaded view
|

Re: Running in total

DEBOER
In reply to this post by DEBOER
Hi guys,

thanks very much for your time and solutions,

Garry Gelade solution solved my problem

1. CREATE xsum = PMA(x,6).
2. COMPUTE xsum = xsum*6.
EXECUTE.
LIST.

1. Prior moving average(PMA), for the last 6 cases
2. if I multiply xsum by six I get the sum.

the create fuction together with split file could then easy solve my problem.

Thanks very much all

Peace