Creating a counter of ids within similar records

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

Creating a counter of ids within similar records

Roberts, Michael
Hi Listers,

I would appreciate some help with a way to create a counter of the
number of occurrences within records that have two variables - id and
date, that are the same.  In other words, when the id changes, or the
date changes, I want to begin counting again.

There are solutions but they all to require a numeric value for the id.
I seem to remember doing something like this relatively easily a long
while back, but cannot find the code.

Any help would be much appreciated.

TIA

Mike

=====================
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: Creating a counter of ids within similar records

Richard Ristow
At 05:20 PM 6/23/2008, Roberts, Michael wrote:

>I would like to create a counter of the number of occurrences within
>records that have two variables - id and date. When the id changes,
>or the date changes, I want to begin counting again.

There's a number of ways. This one (code not tested) is fairly easy.
It requires that the file be sorted by ID and Date; and it leaves
variable NewGroup in the file, which you may not want.

>There are solutions but they all to require a numeric value for the id.

This will work with any datatypes for ID and Date.

ADD FILES
   /FILE=*
   /BY ID Date
   /FIRST = NewGroup.

NUMERIC SeqInGrp (F4).
DO IF   NewGroup.
.  COMPUTE SeqInGrp = 1.
ELSE.
.  COMPUTE SeqInGrp = LAG(SeqInGrp) + 1.
END IF.

=====================
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: Creating a counter of ids within similar records

Roberts, Michael
Richard,

Thank you for the solution.  I did finally manage this by creating a
counter, setting its default value to 1, then testing both values (date
and ID)lagged by 1, and incrementing the counter by 1; seems to work.

Your solution, however looks very interesting, and I am going to check
it out in the morning. In my search for a solution, I did see some code
you posted a while back which includes 'Before' and 'After' commands.
My search for information on these two turned up very little.  How can I
find out more about them?

Thanking You
Sincerely

Mike




-----Original Message-----
From: Richard Ristow [mailto:[hidden email]]
Sent: Monday, June 23, 2008 6:29 PM
To: Roberts, Michael; [hidden email]
Subject: Re: Creating a counter of ids within similar records

At 05:20 PM 6/23/2008, Roberts, Michael wrote:

>I would like to create a counter of the number of occurrences within
>records that have two variables - id and date. When the id changes,
>or the date changes, I want to begin counting again.

There's a number of ways. This one (code not tested) is fairly easy.
It requires that the file be sorted by ID and Date; and it leaves
variable NewGroup in the file, which you may not want.

>There are solutions but they all to require a numeric value for the id.

This will work with any datatypes for ID and Date.

ADD FILES
   /FILE=*
   /BY ID Date
   /FIRST = NewGroup.

NUMERIC SeqInGrp (F4).
DO IF   NewGroup.
.  COMPUTE SeqInGrp = 1.
ELSE.
.  COMPUTE SeqInGrp = LAG(SeqInGrp) + 1.
END IF.

=====================
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: Creating a counter of ids within similar records

Richard Ristow
At 06:40 PM 6/23/2008, Roberts, Michael wrote:

>Thank you for the solution.

You're welcome!

>I [managed] this by creating a counter, setting its default value to
>1, then testing both values (date and ID) lagged by 1, and
>incrementing the counter by 1 [when the lagged values both matched
>the current values?]; seems to work.

Yes; that's fundamentally very similar logic. Did you specify LEAVE
for the counter, or use LAG (as I did) to increment the counter from
the previous case?

>Your solution, however looks very interesting, and I am going to
>check it out in the morning.

It's the easiest to program, since it doesn't need separate checking
of the two key variables, and it doesn't need special-case logic for
the very first record in the file (as LAG logic sometimes does).
Apart from leaving the extra variable in the file, it has a lot going for it

>I did see some code you posted a while back which includes 'Before'
>and 'After' commands. My search for information on these two turned
>up very little. How can I find out more about them?

I don't recall that; could you refer me to the posting you're writing
about? Neither 'Before' nor 'After' is an SPSS command; I'd need to
see the context in which I mentioned them.

-Best wishes, and onward,
  Richard

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