Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

RE: st: egen anycount

From   Thomas Speidel <>
To   <>
Subject   RE: st: egen anycount
Date   Tue, 24 May 2011 13:03:48 -0600

The reality is much less picture perfect: a MET value is assigned from a published compendium of physical activities. This compendium has a single decimal point of precision (just like the last page on the Wikipedia article). Of course, if I had a choice I would do the calculations myself, thus preserving more accuracy, but I have to rely on published sources and work within its constraints.
I suspect you are going to suggest using reshape... :-)


On Tue, 24 May 2011 18:20:46 +0100, Nick Cox <> wrote:
The problem can thus be traced upstream. Don't reduce such a variable
to 1.2, 0.8, 1.5; if you really need such a variable map to integers
with value labels.


Thomas Speidel

 Nick, these values represent metabolic equivalent (MET) which is a
 measure of energy consumption.
 In this context I need to separate sedentary activities (</=1.5MET)
 from non-sedentary activities (>1.5MET)

On Thu, 19 May 2011 13:21:48 +0100, Nick Cox <>
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
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
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.


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:

Thomas Speidel
*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index