Re: Counting Unique Occurrences Across Variables

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

Re: Counting Unique Occurrences Across Variables

Chris Fisher
On Mon, 3 May 2004 10:39:44 +0200, Spousta Jan <[hidden email]> wrote:

>Hi Mark,
>It seems like a simply task, but it is a bit tricky, if you should program
it generally without knowing all the possible values and variable names in
advance. I tried to use standard loops and do repeats, but I found no
reasonable solution immediately. At the end, I solved it using the SPSS
macro facility:

>
>* the sample data.
>data list free / v1 to v3 .
>begin data.
>1     2     3
>1     2     1
>end data.
>
>* define the macro.
>
>define cntuniq(!pos = !cha('/') / !pos = !cmd).
>
>* Macro for counting unique values in a list of variables.
>* First argument (ends by a slash /) - nonempty list of existing variables
>* (do not use conventions like v1 TO v5, separate the variables by blanks)
>* Second arument - name of the output variable, containing # of unique
values in the

>* input variables for the given row.
>
>compute !2 = 1.
>
>!let !list = !head(!1).
>!do !i !in (!1).
>-  if (not any(!i, !list)) !2 = !2 + 1.
>-  !let !list = !concat (!list,",", !i).
>!doend.
>!enddefine.
>
>* run it.
>
>cntuniq v1 v2 v3 / cnt_uniq .
>
>exe.
>
>
>Hope this helps you.
>
>Jan
>
>-----Original Message-----
>From: Mark Webb [mailto:[hidden email]]
>Sent: Monday, May 03, 2004 9:29 AM
>To: [hidden email]
>Subject: Counting Unique Occurrences Across Variables
>
>
>The COUNT command counts the number of occurrences across variables. Is
>there a command that reports the number of unique occurrence across
>variables. An example will be demonstrate what I'm after.
>
>V1  V2   V3         Desired Answer
>
>1     2     3                3
>1     2     1                2      i.e. the '1' is only counted once.
>
>Any suggestions will be appreciated.
>
>
>
>Mark Webb
>TargetLink Research
>P O Box 13040 Mowbray 7705
>1st Floor, Standard Bank Building
>37 Main Rd, Mowbray, Cape Town
>Phone   021-689-8848
>Fax       021-686-9493
>Cell       072 199 1000
>e-mail    [hidden email]

This was a great answer...however, one question. How do I adapt it to not
count missing values as unique values?

Thanks.

=====================
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: Counting Unique Occurrences Across Variables

David Marso
Administrator
Here is another approach:
** Simulate some data **.
INPUT PROGRAM.
LOOP ID=1 TO 1000.
DO REPEAT v=v1 to v10.
COMPUTE v=trunc(uniform(10)).
END REPEAT.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
EXE.

MISSING VALUES v1 TO v10 (0).
SAVE OUTFILE 'tmp.sav'.

VARSTOCASES / MAKE val FROM v1 TO v10.
AGGREGATE OUTFILE * / BREAK ID val / N=N.
SELECT IF NOT MISSING(val).
AGGREGATE OUTFILE * / BREAK ID / Unique=N.
MATCH FILES / FILE 'tmp.sav' / FILE * / BY ID.

Chris Fisher wrote
On Mon, 3 May 2004 10:39:44 +0200, Spousta Jan <[hidden email]> wrote:

>Hi Mark,
>It seems like a simply task, but it is a bit tricky, if you should program
it generally without knowing all the possible values and variable names in
advance. I tried to use standard loops and do repeats, but I found no
reasonable solution immediately. At the end, I solved it using the SPSS
macro facility:
>
>* the sample data.
>data list free / v1 to v3 .
>begin data.
>1     2     3
>1     2     1
>end data.
>
>* define the macro.
>
>define cntuniq(!pos = !cha('/') / !pos = !cmd).
>
>* Macro for counting unique values in a list of variables.
>* First argument (ends by a slash /) - nonempty list of existing variables
>* (do not use conventions like v1 TO v5, separate the variables by blanks)
>* Second arument - name of the output variable, containing # of unique
values in the
>* input variables for the given row.
>
>compute !2 = 1.
>
>!let !list = !head(!1).
>!do !i !in (!1).
>-  if (not any(!i, !list)) !2 = !2 + 1.
>-  !let !list = !concat (!list,",", !i).
>!doend.
>!enddefine.
>
>* run it.
>
>cntuniq v1 v2 v3 / cnt_uniq .
>
>exe.
>
>
>Hope this helps you.
>
>Jan
>
>-----Original Message-----
>From: Mark Webb [mailto:[hidden email]]
>Sent: Monday, May 03, 2004 9:29 AM
>To: [hidden email]
>Subject: Counting Unique Occurrences Across Variables
>
>
>The COUNT command counts the number of occurrences across variables. Is
>there a command that reports the number of unique occurrence across
>variables. An example will be demonstrate what I'm after.
>
>V1  V2   V3         Desired Answer
>
>1     2     3                3
>1     2     1                2      i.e. the '1' is only counted once.
>
>Any suggestions will be appreciated.
>
>
>
>Mark Webb
>TargetLink Research
>P O Box 13040 Mowbray 7705
>1st Floor, Standard Bank Building
>37 Main Rd, Mowbray, Cape Town
>Phone   021-689-8848
>Fax       021-686-9493
>Cell       072 199 1000
>e-mail    [hidden email]

This was a great answer...however, one question. How do I adapt it to not
count missing values as unique values?

Thanks.

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Counting Unique Occurrences Across Variables

Kirill Orlov
In reply to this post by Chris Fisher
There can be found, on rivita.ru/spssmacros_en.shtml, quite flexible macro !hcount (collection "Horizontal tools) to count unique values and how often they appear across variables.


13.07.2012 21:02, Chris Fisher пишет:
On Mon, 3 May 2004 10:39:44 +0200, Spousta Jan [hidden email] wrote:

Hi Mark,
It seems like a simply task, but it is a bit tricky, if you should program
it generally without knowing all the possible values and variable names in
advance. I tried to use standard loops and do repeats, but I found no
reasonable solution immediately. At the end, I solved it using the SPSS
macro facility:
* the sample data.
data list free / v1 to v3 .
begin data.
1     2     3
1     2     1
end data.

* define the macro.

define cntuniq(!pos = !cha('/') / !pos = !cmd).

* Macro for counting unique values in a list of variables.
* First argument (ends by a slash /) - nonempty list of existing variables
* (do not use conventions like v1 TO v5, separate the variables by blanks)
* Second arument - name of the output variable, containing # of unique
values in the
* input variables for the given row.

compute !2 = 1.

!let !list = !head(!1).
!do !i !in (!1).
-  if (not any(!i, !list)) !2 = !2 + 1.
-  !let !list = !concat (!list,",", !i).
!doend.
!enddefine.

* run it.

cntuniq v1 v2 v3 / cnt_uniq .

exe.


Hope this helps you.

Jan

-----Original Message-----
From: Mark Webb [[hidden email]]
Sent: Monday, May 03, 2004 9:29 AM
To: [hidden email]
Subject: Counting Unique Occurrences Across Variables


The COUNT command counts the number of occurrences across variables. Is
there a command that reports the number of unique occurrence across
variables. An example will be demonstrate what I'm after.

V1  V2   V3         Desired Answer

1     2     3                3
1     2     1                2      i.e. the '1' is only counted once.

Any suggestions will be appreciated.



Mark Webb
TargetLink Research
P O Box 13040 Mowbray 7705
1st Floor, Standard Bank Building
37 Main Rd, Mowbray, Cape Town
Phone   021-689-8848
Fax       021-686-9493
Cell       072 199 1000
e-mail    [hidden email]
This was a great answer...however, one question. How do I adapt it to not
count missing values as unique values?

Thanks.

=====================
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: Counting Unique Occurrences Across Variables

Jon K Peck
In reply to this post by David Marso
Or this, using an extension command.

version 1 - includes missing values:

spssinc trans result = unique
/formula "len(set([v1,v2,v3]))".

version 2 - ignores sysmis values
begin program.
def countunique(*args):
  return len(set([v for v in args if not v is None]))
end program.

spssinc trans result = unique
/formula "countunique(v1,v2,v3)".

version 3: handling user missing would best be done by preceding with a temporary recode into sysmis and using version 2.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        07/13/2012 11:58 AM
Subject:        Re: [SPSSX-L] Counting Unique Occurrences Across Variables
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Here is another approach:
** Simulate some data **.
INPUT PROGRAM.
LOOP ID=1 TO 1000.
DO REPEAT v=v1 to v10.
COMPUTE v=trunc(uniform(10)).
END REPEAT.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
EXE.

MISSING VALUES v1 TO v10 (0).
SAVE OUTFILE 'tmp.sav'.

VARSTOCASES / MAKE val FROM v1 TO v10.
AGGREGATE OUTFILE * / BREAK ID val / N=N.
SELECT IF NOT MISSING(val).
AGGREGATE OUTFILE * / BREAK ID / Unique=N.
MATCH FILES / FILE 'tmp.sav' / FILE * / BY ID.


Chris Fisher wrote
>
> On Mon, 3 May 2004 10:39:44 +0200, Spousta Jan &lt;JSpousta@&gt; wrote:
>
>>Hi Mark,
>>It seems like a simply task, but it is a bit tricky, if you should program
> it generally without knowing all the possible values and variable names in
> advance. I tried to use standard loops and do repeats, but I found no
> reasonable solution immediately. At the end, I solved it using the SPSS
> macro facility:
>>
>>* the sample data.
>>data list free / v1 to v3 .
>>begin data.
>>1     2     3
>>1     2     1
>>end data.
>>
>>* define the macro.
>>
>>define cntuniq(!pos = !cha('/') / !pos = !cmd).
>>
>>* Macro for counting unique values in a list of variables.
>>* First argument (ends by a slash /) - nonempty list of existing variables
>>* (do not use conventions like v1 TO v5, separate the variables by blanks)
>>* Second arument - name of the output variable, containing # of unique
> values in the
>>* input variables for the given row.
>>
>>compute !2 = 1.
>>
>>!let !list = !head(!1).
>>!do !i !in (!1).
>>-  if (not any(!i, !list)) !2 = !2 + 1.
>>-  !let !list = !concat (!list,",", !i).
>>!doend.
>>!enddefine.
>>
>>* run it.
>>
>>cntuniq v1 v2 v3 / cnt_uniq .
>>
>>exe.
>>
>>
>>Hope this helps you.
>>
>>Jan
>>
>>-----Original Message-----
>>From: Mark Webb [
mailto:targetlk@]
>>Sent: Monday, May 03, 2004 9:29 AM
>>To: SPSSX-L@.UGA
>>Subject: Counting Unique Occurrences Across Variables
>>
>>
>>The COUNT command counts the number of occurrences across variables. Is
>>there a command that reports the number of unique occurrence across
>>variables. An example will be demonstrate what I'm after.
>>
>>V1  V2   V3         Desired Answer
>>
>>1     2     3                3
>>1     2     1                2      i.e. the '1' is only counted once.
>>
>>Any suggestions will be appreciated.
>>
>>
>>
>>Mark Webb
>>TargetLink Research
>>P O Box 13040 Mowbray 7705
>>1st Floor, Standard Bank Building
>>37 Main Rd, Mowbray, Cape Town
>>Phone   021-689-8848
>>Fax       021-686-9493
>>Cell       072 199 1000
>>e-mail    targetlk@
>
> This was a great answer...however, one question. How do I adapt it to not
> count missing values as unique values?
>
> Thanks.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> LISTSERV@.UGA (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
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Re-Counting-Unique-Occurrences-Across-Variables-tp5714205p5714206.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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: Counting Unique Occurrences Across Variables

Jarrod Teo-2
In reply to this post by Kirill Orlov
Hi Mark,
 
I have crafted a way with the usage of do repeat-end repeats. The syntax is lengthy but it shows a step by step on how the syntax 'count' the unique values across the variables.
 
I have also involved system missing in the sample data file to show how system missing values can be counted.
 
Hope it helps.
 
****************************.
*Counting Unique Values with sysmis.
****************************.
***************Sample Data********************.
data list free / v1 to v3 .
begin data.
1    2      3
1    2      1
3    3      3
end data.
recode v3 (3=sysmis).
recode v2 (3=sysmis).
exe.
***************Sample Data********************.
 
*****Changing all the sysmis into -99***.
do repeat x1=v1 to v3.
recode x1 (sysmis=-99).
end repeat.
exe.
*********************************************.

*****Counting all the values across the list***.
do repeat x1= f1 to f3 f99
/x2=1 2 3 -99.
count x1=v1 to v3 (x2).

end repeat.
exe.
*********************************************.

****Recoding the count into 1****.
do repeat x1=f1 to f99.
recode x1 (1 thru hi=1).
end repeat.
exe.
*********************************************.

****Summing the unique values in the row***.
compute unique=sum( f1 to f99).
exe.
 
delete var f1 to f99.

***************************************************************************************************************************************.
 

Date: Fri, 13 Jul 2012 21:56:08 +0400
From: [hidden email]
Subject: Re: Counting Unique Occurrences Across Variables
To: [hidden email]

There can be found, on rivita.ru/spssmacros_en.shtml, quite flexible macro !hcount (collection "Horizontal tools) to count unique values and how often they appear across variables.


13.07.2012 21:02, Chris Fisher пишет:
On Mon, 3 May 2004 10:39:44 +0200, Spousta Jan [hidden email] wrote:

Hi Mark,
It seems like a simply task, but it is a bit tricky, if you should program
it generally without knowing all the possible values and variable names in
advance. I tried to use standard loops and do repeats, but I found no
reasonable solution immediately. At the end, I solved it using the SPSS
macro facility:
* the sample data.
data list free / v1 to v3 .
begin data.
1     2     3
1     2     1
end data.

* define the macro.

define cntuniq(!pos = !cha('/') / !pos = !cmd).

* Macro for counting unique values in a list of variables.
* First argument (ends by a slash /) - nonempty list of existing variables
* (do not use conventions like v1 TO v5, separate the variables by blanks)
* Second arument - name of the output variable, containing # of unique
values in the
* input variables for the given row.

compute !2 = 1.

!let !list = !head(!1).
!do !i !in (!1).
-  if (not any(!i, !list)) !2 = !2 + 1.
-  !let !list = !concat (!list,",", !i).
!doend.
!enddefine.

* run it.

cntuniq v1 v2 v3 / cnt_uniq .

exe.


Hope this helps you.

Jan

-----Original Message-----
From: Mark Webb [[hidden email]]
Sent: Monday, May 03, 2004 9:29 AM
To: [hidden email]
Subject: Counting Unique Occurrences Across Variables


The COUNT command counts the number of occurrences across variables. Is
there a command that reports the number of unique occurrence across
variables. An example will be demonstrate what I'm after.

V1  V2   V3         Desired Answer

1     2     3                3
1     2     1                2      i.e. the '1' is only counted once.

Any suggestions will be appreciated.



Mark Webb
TargetLink Research
P O Box 13040 Mowbray 7705
1st Floor, Standard Bank Building
37 Main Rd, Mowbray, Cape Town
Phone   021-689-8848
Fax       021-686-9493
Cell       072 199 1000
e-mail    [hidden email]
This was a great answer...however, one question. How do I adapt it to not
count missing values as unique values?

Thanks.

=====================
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: Counting Unique Occurrences Across Variables

Spousta Jan
In reply to this post by Chris Fisher
Chris,

In the spirit of the original macro, you can use the following piece of syntax (of course outdated and complicated, I admit).

Best regards,

Jan


define cntuniqm(!pos = !cha('/') / !pos = !cmd).

* Macro for counting unique values in a list of variables.
* Missings (both user and system) are not counted.
* First argument (ends by a slash /) - nonempty list of existing variables
* (do not use conventions like v1 TO v5, separate the variables by blanks)
* Second arument - name of the output variable, containing # of unique values in the
* input variables for the given row.



!let !list = !head(!1).

compute !2 = not (missing(!list)).

!do !i !in (!1).
-  if ( (not (missing(!i)) and !2 = 0) ) or (not any(!i, !list)) and (not (missing(!i))  )  !2 = !2 + 1.
-  !let !list = !concat (!list,",", !i).
!doend.

exe.
!enddefine.



-----Original Message-----
From: Chris Fisher [mailto:[hidden email]]
Sent: Friday, July 13, 2012 7:03 PM
To: [hidden email]; Spousta Jan
Subject: Re: Counting Unique Occurrences Across Variables

On Mon, 3 May 2004 10:39:44 +0200, Spousta Jan <[hidden email]> wrote:

>Hi Mark,
>It seems like a simply task, but it is a bit tricky, if you should
>program
it generally without knowing all the possible values and variable names in advance. I tried to use standard loops and do repeats, but I found no reasonable solution immediately. At the end, I solved it using the SPSS macro facility:

>
>* the sample data.
>data list free / v1 to v3 .
>begin data.
>1     2     3
>1     2     1
>end data.
>
>* define the macro.
>
>define cntuniq(!pos = !cha('/') / !pos = !cmd).
>
>* Macro for counting unique values in a list of variables.
>* First argument (ends by a slash /) - nonempty list of existing
>variables
>* (do not use conventions like v1 TO v5, separate the variables by
>blanks)
>* Second arument - name of the output variable, containing # of unique
values in the

>* input variables for the given row.
>
>compute !2 = 1.
>
>!let !list = !head(!1).
>!do !i !in (!1).
>-  if (not any(!i, !list)) !2 = !2 + 1.
>-  !let !list = !concat (!list,",", !i).
>!doend.
>!enddefine.
>
>* run it.
>
>cntuniq v1 v2 v3 / cnt_uniq .
>
>exe.
>
>
>Hope this helps you.
>
>Jan
>
>-----Original Message-----
>From: Mark Webb [mailto:[hidden email]]
>Sent: Monday, May 03, 2004 9:29 AM
>To: [hidden email]
>Subject: Counting Unique Occurrences Across Variables
>
>
>The COUNT command counts the number of occurrences across variables. Is
>there a command that reports the number of unique occurrence across
>variables. An example will be demonstrate what I'm after.
>
>V1  V2   V3         Desired Answer
>
>1     2     3                3
>1     2     1                2      i.e. the '1' is only counted once.
>
>Any suggestions will be appreciated.
>
>
>
>Mark Webb
>TargetLink Research
>P O Box 13040 Mowbray 7705
>1st Floor, Standard Bank Building
>37 Main Rd, Mowbray, Cape Town
>Phone   021-689-8848
>Fax       021-686-9493
>Cell       072 199 1000
>e-mail    [hidden email]

This was a great answer...however, one question. How do I adapt it to not count missing values as unique values?

Thanks.



_____________
Tato zpráva a všechny připojené soubory jsou důvěrné a určené výlučně adresátovi(-ům). Jestliže nejste oprávněným adresátem, je zakázáno jakékoliv zveřejňování, zprostředkování nebo jiné použití těchto informací. Jestliže jste tento mail dostali neoprávněně, prosím, uvědomte odesilatele a smažte zprávu i přiložené soubory. Odesilatel nezodpovídá za jakékoliv chyby nebo opomenutí způsobené tímto přenosem.

Jste si jisti, že opravdu potřebujete vytisknout tuto zprávu a/nebo její přílohy? Myslete na přírodu.


This message and any attached files are confidential and intended solely for the addressee(s). Any publication, transmission or other use of the information by a person or entity other than the intended addressee is prohibited. If you receive this in error please contact the sender and delete the message as well as all attached documents. The sender does not accept liability for any errors or omissions as a result of the transmission.

Are you sure that you really need a print version of this message and/or its attachments? Think about nature.

-.- --

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