Resetting a cumulative variable

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

Resetting a cumulative variable

Jim Arnold-3
Hello,

I have a data file whereby each respondent has precisely 4 cases of data.  It looks like this:

id v1 v2...
1 0 1...
1 1 2...
1 1 1...
1 0 2...
2 1 0...
2 1 0...
2 0 1...
2 0 1...

I am required to add a cumulative count variable for each respondent's record so the data would look like:

id v1 v2..."cume"
1 0 1... 1
1 1 2... 2
1 1 1... 3
1 0 2... 4
2 1 0... 1
2 1 0... 2
2 0 1... 3
2 0 1... 4

I am familiar with the LEAVE command which I have attempted, without success, to modify using conditional statements between each respondent.  I am aware of, but have not succeeded in using the LAG function.  How does one accomplish a simple resetting of a cumulative count variable between unique respondent records?

Thanks

Jim
Reply | Threaded
Open this post in threaded view
|

Re: Resetting a cumulative variable

Maguin, Eugene
Jim,

I kind of think I have seen someone post a procedure based on the Rank
command. But, I'm not sure. Another method (untested) is this (this scheme
assumes exactly four cases per student).

Compute cume=trun($casenum+3)/4).

Another way (also untested) is

Compute cume=1.
Do if ($casenum gt 1).
+  if (id eq lag(id)) cume=lag(cume).
+  if (id ne lag(id)) cume=lag(cume)+1.
End if.

Gene Maguin




I have a data file whereby each respondent has precisely 4 cases of data.
It looks like this:

id v1 v2...
1 0 1...
1 1 2...
1 1 1...
1 0 2...
2 1 0...
2 1 0...
2 0 1...
2 0 1...

I am required to add a cumulative count variable for each respondent's
record so the data would look like:

id v1 v2..."cume"
1 0 1... 1
1 1 2... 2
1 1 1... 3
1 0 2... 4
2 1 0... 1
2 1 0... 2
2 0 1... 3
2 0 1... 4

I am familiar with the LEAVE command which I have attempted, without
success, to modify using conditional statements between each respondent.  I
am aware of, but have not succeeded in using the LAG function.  How does one
accomplish a simple resetting of a cumulative count variable between unique
respondent records?

=====================
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: Resetting a cumulative variable

Richard Ristow
In reply to this post by Jim Arnold-3
At 08:21 AM 3/10/2009, Jim Arnold wrote:

I have a data file whereby each respondent has precisely 4 cases of data. I need to add a cumulative count variable for each respondent's record so the data would look like:

id v1 v2..."cume"
1 0 1... 1
1 1 2... 2
1 1 1... 3
1 0 2... 4
2 1 0... 1
2 1 0... 2

How does one accomplish a simple resetting of a cumulative count variable between unique respondent records?

Something like this (code not tested):

LEAVE cume.

DO IF    $CASENUM EQ 1.
.  COMPUTE cume = 1.
ELSE IF  id NE LAG(id).
.  COMPUTE cume = 1.
ELSE.
.  COMPUTE cume = cume + 1.
END IF.

Continuing -- at 04:05 PM 3/10/2009, Gene Maguin wrote:

I kind of think I have seen someone post a procedure based on the Rank command.

Yes; in fact, this is something of a FAQ. Here's the latest draft of the FAQ:

FAQ: Sequence numbers & random sequence numbers for cases

For adding a sequence number to the whole file,

COMPUTE SeqnNumb = $CASENUM.

is much the easiest.

There are several ways to create sequence numbers within groups. In the examples, groups are defined by the variable Group.  Letters in parentheses refer to the citations at the end of the examples:

Using LAG in a transformation program(a):

DO IF       $CASENUM EQ 1
        OR  Group NE LAG(Group).
.  COMPUTE SeqnNumb = 1.
ELSE.
.  COMPUTE SeqnNumb = LAG(SeqnNumb) + 1.
END IF.


Using CREATE(b):

COMPUTE NOBREAK = 1.
FORMATS NOBREAK (F2).

SPLIT FILE BY Group.
CREATE SeqnNumb = CSUM(NOBREAK).


Using RANK(c):

COMPUTE id=$casenum.
RANK VARIABLES=ID(A) BY Group
   /RANK INTO SeqnNumb
   /PRINT=NO .
DELETE VARIABLES id.
FORMAT SeqnNumb (F4).


Random sequencing within groups(d):

COMPUTE RandKey   = RV.UNIFORM(0,1).

RANK RandKey (A)
  BY GROUP
   /RANK INTO RandSeqn.

....................................
(a) This method is pretty well known; I haven't looked for a citation.

(b) Date: Tue, 13 Nov 2007 22:46:32 -0500
From:     Richard Ristow <[hidden email]>
Subject:  Re: Consecutive numbers of different size
To:       [hidden email]

(c)Date:  Wed, 14 Nov 2007 07:42:06 +0100
From:     Marta García-Granero <[hidden email]>
Subject:  Re: Consecutive numbers of different size
To:       [hidden email]


(d)Date:  Tue, 15 Jan 2008 11:15:44 -0800
From:     King Douglas <[hidden email]>
Subject:  Re: drawing samples for hundreds of workers
To:       [hidden email]

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