LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (March 2012)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 28 Mar 2012 06:40:13 -0700
Reply-To:     David Marso <david.marso@gmail.com>
Sender:       "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:         David Marso <david.marso@gmail.com>
Subject:      Re: Summing a variable.
In-Reply-To:  <1332941278022-5600471.post@n5.nabble.com>
Content-Type: text/plain; charset=us-ascii

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

-- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Summing-a-variable-tp5600280p5600503.html Sent from the SPSSX Discussion mailing list archive at Nabble.com.

===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@LISTSERV.UGA.EDU (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

This email is intended only for the use of the individual or entity to which it is addressed and may contain information that is privileged and confidential. If the reader of this email message is not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication is prohibited. If you have received this email in error, please notify the sender and destroy/delete all copies of the transmittal. Thank you.

This email is intended only for the use of the individual or entity to which it is addressed and may contain information that is privileged and confidential. If the reader of this email message is not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication is prohibited. If you have received this email in error, please notify the sender and destroy/delete all copies of the transmittal. Thank you.

===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@LISTSERV.UGA.EDU (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


Back to: Top of message | Previous page | Main SPSSX-L page