Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HOW I can count number of unique values of variable?

11,562 views
Skip to first unread message

Nick

unread,
Aug 30, 2002, 2:45:59 AM8/30/02
to
Hello All!
Please, help, i need a function, that counts number of values (unique)
of variable between cases? I have not found one in SPSS syntax... nor
in SAX Basic... How i can do this?
I need this to pass a count of values of a variable to my program in
sax basic for further computations...

Thanks and i am sorry for my english not very well

Raynald Levesque

unread,
Aug 30, 2002, 7:40:07 AM8/30/02
to

Hi

1) to calculate the number of unique values, use
AGGREGATE OUTFILE=*
/BREAK=yourvar
/cnt=N.

where yourvar is the name of your variable.

2) you then add that value to every case of your data base using the
ADD command.

3) You then use syntax to automatically write a small syntax file
containing only
SCRIPT 'path\name.SBS' ('###').

where ### is replaced by the number obtained. in 1). For an example
of how to write a syntax file using syntax, see
"include 2 output in syntax.sps" in
http://pages.infinit.net/rlevesqu/Macros.htm#SelfAdjustingMacros

4) you then use the INCLUDE command to read the syntax file created in
3).

HTH

Raynald Levesque rlev...@videotron.ca
Visit My SPSS Pages: http://pages.infinit.net/rlevesqu/index.htm

Bjarte Aagnes

unread,
Aug 30, 2002, 7:44:30 AM8/30/02
to Nick
On 29 Aug 2002, Nick wrote:

> ... i need a function, that counts number of values (unique)


> of variable between cases? I have not found one in SPSS syntax... nor
> in SAX Basic... How i can do this?

If I understand your problem correct you could
use RANK with /TIES=CONDENSE and pick the highest
value with AGGREGATE (or see below for an alternative)

RANK var00001 (A) /RANK /TIES=CONDENSE.
XSAVE OUTFILE='C:tmpres.sav' /KEEP=rvar0000.
TEMP.
SEL IF $CASENUM=1.
FRE rvar0000.

> I need this to pass a count of values of a variable to my program in
> sax basic for further computations...


--
Bjarte Aagnes

donha...@gmail.com

unread,
Jun 1, 2016, 12:20:56 AM6/1/16
to
Vào 18:40:07 UTC+7 Thứ Sáu, ngày 30 tháng 8 năm 2002, Raynald Levesque đã viết:
Dear Raynald Levesque1

I don't Know. Can you guide detail help me? Thank you

Bruce Weaver

unread,
Jun 1, 2016, 9:41:42 AM6/1/16
to
On Wednesday, June 1, 2016 at 12:20:56 AM UTC-4, donha...@gmail.com wrote:
> > On 29 Aug 2002 23:45:59 -0700, sanm...@yandex.ru (Nick) wrote:
> >
> > >Hello All!
> > >Please, help, i need a function, that counts number of values (unique)
> > >of variable between cases? I have not found one in SPSS syntax... nor
> > >in SAX Basic... How i can do this?
> Dear Raynald Levesque1
>
> I don't Know. Can you guide detail help me? Thank you

You are replying to a very old thread, and I doubt very much that Raynald is reading this list any more.

The original post in the thread suggests that you want to compute a new variable that shows the number of unique values of some other variable, is that right? If so, here is one way that uses RANK, as suggested by Bjarte Aagnes (in another very old post in this thread).

* Generate some data to illustrate.
NEW FILE.
DATASET CLOSE all.
INPUT PROGRAM.
loop Case = 1 to 500.
- COMPUTE X = TRUNC(RV.UNIFORM(1,10)).
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
EXECUTE.
FORMATS X(F2.0).
MISSING VALUES X (8 THRU HI).
FREQUENCIES X.

* Count unique values of variable X.
* Use RANK, as suggested by Bjarte Aagnes.
* The default behaviour of RANK is to exclude
* missing values. If you wish to include them
* include a /MISSING=INCLUDE sub-command.

* Get ranks for variable X excluding missing values.
RANK VARIABLES=X (A)
/RANK
/PRINT=YES
/TIES=CONDENSE.
* Get ranks for variable X including missing values.
RANK VARIABLES=X (A)
/RANK
/PRINT=YES
/MISSING=INCLUDE
/TIES=CONDENSE.

AGGREGATE
/OUTFILE=* MODE=ADDVARIABLES
/BREAK=
/NumVals1 '# of unique X values (excluding missing values)'=MAX(Rx)
/NumVals2 '# of unique X values (including missing values)'=MAX(RAN001)
.

FORMATS NumVals1 NumVals2(F5.0).
FREQUENCIES X NumVals1 NumVals2.

* End of syntax.

When I run this syntax, the frequency table for X shows 7 valid values and 2 missing values, and variables NumVals1 and NumVals2 have values of 7 and 9 respectively.

HTH.
0 new messages