Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: RE: check if variable is constant within id


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: RE: check if variable is constant within id
Date   Thu, 17 May 2007 23:38:49 +0100

The -assert- command was the subject of one of the very 
first Stata Tips: 

SJ-3-4  dm0003  . . . . . . . . . . . . . . . Stata tip 3: How to be assertive
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . W. Gould
        Q4/03   SJ 3(4):448                                      (no commands)
        tips for using assert

which is reprinted in the slim volume "Thirty-three Stata Tips" 
from StataCorp, a snip at the price. 

Another solution has occurred to me. Both 
cases, missings present and missings not present, can be covered by 

bysort id (var1) : assert (var1 == var1[1]) | missing(var1) 

That way, every value must be equal to the first (after sorting), 
or missing. 

If nothing is said, then you have no problems, unless 
your missings take two or more of the values . .a ... .z. 

Nick 
[email protected] 

Mak, Timothy

> Thank you Nick. 
> 
> Tim 
> 
> PS The FAQ page did not mention the -assert- command, which I didn't
> know before.  

Nick Cox
 
> I am pleased to hear that you don't want a program to do this. 
> 
> First without missings, 
> 
> bysort id (var1) : assert var1[1] == var1[_N] 
> 
> checks that values of var1 are constant within id. 
> 
> Second with missings, we need to set missings on one side: 
> 
> gen byte ismiss = missing(var1)
> bysort ismiss id (var) : assert var1[1] == var1[_N] 
> 
> That's two lines. Perhaps someone can shorten it. 
> 
> But the solutions here can be worked out from this FAQ, which 
> says more
> that I won't repeat here: 
> 
> How do I list observations in a group that differ on a variable? 
> http://www.stata.com/support/faqs/data/diff.html
> 
> My suggestion to you is to scan the FAQs before posting.  
 
Mak, Timothy
  
> > I'm looking for an easy way to check if all the observations for a 
> > particular variable (say var1) within id (a subject identifying
> > variable) are constant a) for the case where missings can 
> be excluded,
> 
> > and b) for the case where missings cannot be excluded. The 
> best answer
> 
> > would be a one-liner, with a short display. I don't want a 
> program to 
> > do this.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index