Date: Thu, 12 Dec 2002 18:25:50 -0500
Reply-To: Richard Ristow <wrristow@mindspring.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Richard Ristow <wrristow@mindspring.com>
Subject: Re: counter
In-Reply-To: <3DF765EA.3020604@tm.tno.nl>
Content-Type: text/plain; charset="us-ascii"; format=flowed
At 05:20 PM 12/11/2002 +0100, Anja Langefeld wrote (paraphrased;
original question at end of posting):
[My datafile contains a key variable, 'var1', with the same key for
several cases. How do I create variable 'varnew' to number the cases,
starting with 1, within each value of 'var1'?]
First, all posted solutions assume that the file is sorted by var1; use
SORT CASES if it isn't. You may wish to sort by other variables in
addition, to determine the order of cases (for example, by date) within
a value of var1.
At 02:10 PM 12/12/2002 +0100, Reinhard Stoop wrote:
>You can start the procedure with the first case
>
>IF ($casenum=1) varnew 1.
>IF (var1 ~= LAG(var1)) varnew = 1 . EXECUTE .
>IF (var1 = LAG(var1)) varnew = LAG (varnew) + 1. EXECUTE .
This is the classic solution using LAG, and it works fine. The EXECUTE
statements are not needed, and slow the program; each causes a complete
extra pass through the data. The code works fine without them. It can
be recast, perhaps more clearly, with DO IF:
DO IF ($casenum=1).
. COMPUTE varnew = 1.
ELSE IF (var1 ~= LAG(var1)).
. COMPUTE varnew = 1.
ELSE.
. COMPUTE varnew = LAG(varnew) + 1.
END IF.
The solution by Zaid Kimmie <ZAID@CASE.ORG.ZA>, using RANK, also works
and takes even fewer lines of code, but
A. It is slower, because it requires two complete passes through the
data. (The LAG solution requires none at all, but can be done the next
time the data is read for a statistical procedure.)
B. It leaves variable "var" in the dataset, which can be confusing if
it is not later delete.
...............
Question, as originally posed:
>my datafile looks like this (var1 and var2).
>var1 var2 ... varnew
>1 12 1
>1 17 2
>1 15 3
>1 14 4
>2 .. 1
>2 2
>2 3
>2 ..
>2 6
>3 1
>..
>
>How can I create the variable varnew? So, that it is a teller?
>thanks
>anja