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 (February 2005)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 23 Feb 2005 21:18:25 -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: Counting unique occurrences per client
Comments: To: Patrick MCINTIRE <Patrick.Mcintire@state.or.us>
In-Reply-To:  <s21c6e20.023@GW-IA.HR.STATE.OR.US>
Content-Type: text/plain; charset="us-ascii"; format=flowed

At 02:50 PM 2/23/2005, Patrick MCINTIRE wrote:

>I am trying to count the number of unique services per client.

Where, it appears, you have one SPSS case (record) per service; each record includes variables CLIENT and SERVICE, the latter identifying which service was performed.

>I want to read [the] set of records per client, and then count the >unique services. There are duplicate services with different episode >dates per client

i.e., one CLIENT may have several records with the same SERVICE, and you want the number of *different* SERVICE values for each client.

Try this (untested), which assumes your data is in the SPSS working file. (It had better be saved, too, as this logic destroys the working file.)

SORT CASES BY CLIENT SERVICE. NUMERIC NEW_SVC (F2). VAR LABEL NEW_SVC 'First occurrence of service, for this client'. DO IF MISSING(LAG(CLIENT)). * First case in the entire file . . COMPUTE NEW_SVC = 1. ELSE IF CLIENT NE LAG(CLIENT). * First case for this client . . COMPUTE NEW_SVC = 1. ELSE IF SERVICE NE LAG(SERVICE). * Service not seen before for . * this client . . COMPUTE NEW_SVC = 1. ELSE. . COMPUTE NEW_SVC = 0. END IF.

AGGREGATE/OUTFILE=* /BREAK=CLIENT /N_SVC 'No. of different services'=SUM(NEW_SVC) /N_EPS 'No. of service episodes' =N.

(You didn't ask for N_EPS, but there it is.)

Here is a variation (also untested) which is shorter and 'cuter', but you'll have to study MATCH FILES for it to make sense. The "MATCH FILES" replaces everything between "SORT CASES" and "AGGREGATE":

SORT CASES BY CLIENT SERVICE. MATCH FILES /FILE=* /BY CLIENT SERVICE /FIRST=NEW_SVC.

AGGREGATE/OUTFILE=* /BREAK=CLIENT /N_SVC 'No. of different services'=SUM(NEW_SVC) /N_EPS 'No. of service episodes' =N.

This isn't here nor there, but you may be misunderstanding SPSS's logic. In the code you wrote,

>DO IF (CLIENT EQ LAG(CLIENT)) AND SERVICE NE LAG(SERVICE)). > COUNT = COUNT + 1. > COMPUTE NUMSVC = MAX(COUNT). > IF (CLIENT NE LAG(CLIENT)) STOP = 1. >END IF (STOP = 1).

A. The syntax of . COUNT = COUNT + 1. is invalid. I assume you mean . COMPUTE COUNT = COUNT + 1.

B. You want to accumulate COUNT from one case to the next. That won't work unless you've specified LEAVE for COUNT: LEAVE COUNT. (See "LEAVE" statement in the syntax manual. The syntax is easy, but make sure you understand what it's doing.)

C. I don't know whether you noticed, but you never reset COUNT to 0, or to 1, at the beginning of a client.

D. This may be a big misunderstanding: the statement . COMPUTE NUMSVC = MAX(COUNT). is equivalent to . COMPUTE NUMSVC = COUNT. The functions like MAX work over a set of variables within a case; they do NOT work across the values of different cases. "MAX", as you have it, is taking the maximum of a single value: the value of COUNT *in the current case*.

E. The syntax of END IF (STOP = 1). is invalid; END IF doesn't take a conditional. (See the syntax manual regarding DO IF.) I think worse, though, is that you've stumbled over whether you're processing within a case, or between cases. A DO IF operates only within a case (and so does a LOOP), and it looks like you're trying to "END" when you reach the first case for a new client.


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