st: AW: RE: AW: how to identify unique id variables within groups?

 From "Martin Weiss" To Subject st: AW: RE: AW: how to identify unique id variables within groups? Date Wed, 20 May 2009 14:43:18 +0200

```<>

*************
bysort id var1 : gen different = var1[1] != var1[_N]
*************

?

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Nick Cox
Gesendet: Mittwoch, 20. Mai 2009 13:47
An: statalist@hsphsun2.harvard.edu
Betreff: st: RE: AW: how to identify unique id variables within groups?

This is not a good method in general. For example, it wouldn't work with string variables. Apart from -duplicates-, there are plenty more direct methods, such as that Maarten laid out. Here's another:

bysort id var1 : gen different : var1[1] != var1[_N]
list id var1 if different

Nick
n.j.cox@durham.ac.uk

Martin Weiss

Just check whether the standard deviation of the variable in question differs from zero, then:

*************

clear*
set obs 1000

//id
gen id=_n
//10 periods
expand 10
bys id: gen time=_n
xtset id time

//constant
bys id: gen byte myvar=runiform()<0.3

//but not for some...
replace myvar=cond(runiform()<0.1, 0, 1)

//Check for uniqueness
bys id:egen groupsd=sd(myvar)

//get dummy for uniqueness
gen byte unique=groupsd==0
la def unique 0 "not unique" 1 "unique"
la val unique unique
drop groupsd

l in 1/100, noo sepby(id)
xttab unique
*************

Jia Xiangping

For a datafile like :
id                    var1

1			1
1			2
1			3
2			1
2			2
2  		        1

I expect to check whether the value of "var1" within one group of "id"
is unique -- i.e. in group 2, the values are not unique. For a
non-grouped datafile, syntax like -isid- or -duplicates- is able to do
the work. But none of them works when combining with -by-.  Is there
any approach to check the unique  value within groups?

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```