Correct. Thanks.
Note also that precisely this problem is written up as an FAQ:
How do I list observations in a group that differ on a variable?
http://www.stata.com/support/faqs/data/diff.html
Nick
[email protected]
Martin Weiss
Should probably read
*************
bysort id var1 : gen different = var1[1] != var1[_N]
*************
?
Nick Cox
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
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/