Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: egen anycount

From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: egen anycount
Date   Thu, 19 May 2011 13:21:48 +0100

I would advise circumspection here. 

First off, wanting to know if something is equal to one of 1.2, 0.8, 1.5 sounds an unusual kind of problem and it would be interesting to know the context. For example, if these are codes in some classification system, you might be better off holding the variable as string or mapped to integers with value labels. 

Second, Daniel's hack solves one problem only to create another. The code looks OK for -float- variables but for -double- variables it will often give the wrong answer. This can be seen directly with the examples given: 

. di 1.2 == float(1.2)

. di 0.8 == float(0.8)

. di 1.5 == float(1.5)

1.5 _can_ be held as an exact binary in both -float- and -double-, but most decimals can't. So, Daniel's program wouldn't catch 1.2 or 0.8 in a -double- variable. 

The bug is fixable and would require a branch in which a call -float()- was not used if the variable was -double-. 

On the whole, comparisons with decimals are best avoided, so seeking work-arounds is, in my view, not a good strategy. For every program like _ganycount where the file is short and a user-programmer is willing to show you how to change it, there are many more where the opposite is true. 

[email protected] 

daniel klein

I am not aware of a command, but it should not be hard changing the
existing -anycount-. Here's an ad hoc step-by-step

1. locate -anycount- and open it in the do-file editor

. findfile _ganycount.ado
. doedit "`r(fn)'"

2. change the file (don't save the changes yet)

2.1 change line 2 form

"program define _ganycount"
"program define _ganycount2"

2.2 change line 7 from

"[...] , Values(numlist int) /*"
"[...] ,Values(numlist) /*"

2.3 change line 26 from

"*/ if ``i'' == `nj' & `touse'"
*/ if ``i'' == float(`nj') & `touse'

3. save the file as _ganycount2.ado to your "personal" folder
(to locate type: -di c(sysdir_personal)-) or alternatively to your
"plus/_/" folder (locate: -di c(sysdir_personal)-)

4. -discard- Stata

5. use your -anycount2- function
. egen somevar = anycount2(varlist), values(1.2 0.8 1.5)

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index