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, statalist.org is already up and running.


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

RE: st: egen anycount


From   Thomas Speidel <thomas@tmbx.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: egen anycount
Date   Tue, 24 May 2011 12:26:38 -0600

Thomas

On Tue, 24 May 2011 18:20:46 +0100, Nick Cox <n.j.cox@durham.ac.uk> 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.

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

Thomas Speidel

 Nick, these values represent metabolic equivalent (MET) which is a
 measure of energy consumption.
 http://en.wikipedia.org/wiki/Metabolic_equivalent
 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 <n.j.cox@durham.ac.uk>
 wrote:
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)
0

. di 0.8 == float(0.8)
0

. di 1.5 == float(1.5)
1

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.

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

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"
to
"program define _ganycount2"

2.2 change line 7 from

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

2.3 change line 26 from

"*/ if ``i'' == `nj' & `touse'"
to
*/ 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:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

--
Thomas Speidel
*
*   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/


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