There is a much easier way to solve this problem.
Use the COUNT command with a range to count all the distinct values.
For example, you could do
Count numtowns = town1 TO town20 (1 thru 1500).
This will give you the total number of distinct values.
If your town id's were strings, you would need to use Autorecode, since ranges only work here for numbers, but you indicated that you have a numerical id.
----- Original Message -----
From: "Sarah van Mastrigt" <firstname.lastname@example.org>
To: "David Wasserman" <email@example.com>
Sent: Thursday, April 13, 2006 3:27 AM
Subject: Re: Counting unique variable values in a case
> Thanks, David. Can you (or anyone else out there) be a bit more specific
> re. the steps to be taken? I have used the syntax you suggested to
> restructure my town.1 to town.20 variables into cases, but I am still
> unsure of how to then use the aggregate syntax you mentioned to count the
> UNIQUE number of towns associated with a specific case id number.
> Eveything I try (i.e. aggregate, identify duplicate cases, etc) just
> returns a value for the number of towns I have in total for that group id
> (which is equal to the number of indivdiuals in the group). I assume I am
> doing something wrong as I still can't seem to get a count of the unique
> towns? Any other suggestions, or specific syntax would be great! Thanks
> again. Sarah
> On Apr 13 2006, David Wasserman wrote:
>>You could use VARSTOCASES syntax to convert each group into a set of cases
>>with a unique identifier, and then use AGGREGATE syntax to calculate the
>>number of towns in each group.
>>The age difference calculation you describe could perhaps be replaced with
>>the standard deviation of the ages in each group, easily calculated with
>>Custom Data Analysis and SPSS Programming
>>----- Original Message -----
>>From: "Sarah van Mastrigt" <firstname.lastname@example.org>
>>Sent: Wednesday, April 12, 2006 4:37 PM
>>Subject: Counting unique variable values in a case
>>> Hi Everyone-
>>> I am struggling with 2 problems in SPSS 13.0
>>> My dataset is set up such that each case/row represents a particular
>>> group of individuals (each group has a unique ID number), and the
>>> variables indicate group characteristics, including demographics for
>>> each person in the group (i.e., age1, age2, age3....age20). Each row
>>> includes data on between 1 and 20 individuals (depending on group size);
>>> cells are system missing if there are less than 20 individuals in any
>>> given group.
>>> PROBLEM 1: I want to compute variables which reflect the composition of
>>> my groups in terms of how similar they are with regard to their
>>> demographic info. For example, I have a coded variable which represents
>>> the home town of each individual (numbered 1-1500), and want to compute
>>> a new variable that counts the number of unique towns represented in my
>>> In a case where I have 4 people in a group, and they are all from the
>>> same town, I would want my new variable to have a value of 1 (because
>>> there is only a single town represented). In contrast, if I have 3
>>> people in a group, and 2 are from the same town, whilst one is from a
>>> different town, I want the new value to be 2. I don't care what towns, I
>>> just want to know how many different towns are represented in each
>>> My dataset is enormous (over 100,000 cases)...so i cannot feasibly do
>>> of this by hand, or by using the basic count function- is there syntax
>>> something in the pull down menus that could achieve this? I can't find
>>> anywhere on line or in the help menu.
>>> PROBLEM 2: I want to compute an average age difference for each group. I
>>> am interested in the age difference between each individual and every
>>> other individuals in a group/case, but rather than creating a whole
>>> bunch of new variables/pairs, i just want to compute a single new
>>> variable which calculates the AVERAGE age difference between offenders
>>> in that group. So- for example, in a group of 2 individuals, one aged 19
>>> and the other aged 20, the age difference between 19 and 20 is 1 year,
>>> and the age difference between 20 and 19 is one year, for a total sum of
>>> 2 years difference, which is an average of 1 year for the group. This is
>>> relatively straightforward for the smaller groups, but do you know how i
>>> could calculate this easily in the larger groups?
>>> I would appreciate any suggestions as I have been struggling with these
>>> problems for a couple of weeks, with no progress!!
>>> Sarah van Mastrigt, PhD Candidate
>>> Institute of Criminology, University of Cambridge
> Sarah van Mastrigt, PhD Candidate
> Institute of Criminology, University of Cambridge
> Tel: +44(0)7841482240