# Re: st: how to get a scalar value of the ... mode

 From "Nick Cox" To Subject Re: st: how to get a scalar value of the ... mode Date Fri, 16 Aug 2002 14:03:04 +0100

Tarmo Räty

Recently Roger adviced us on how to get a scalar value of the column
sum
by

summ x
scal sidney=r(sum)

Works OK, for the most of the statistics, but I need the mode. After
short look on manuals, no funtion returns r(mode). Currently I'm using
egen and picking an observation from the created variable, but there
must be an easy way.

>>> Depends what you call easy. A sufficient reason for no r(mode)
being defined is that even when a mode is interesting and useful, it
may not
be uniquely defined.

Setting aside the possibility of ties, I cannot improve on
something like

egen Mode = mode(varname)
scalar mode = Mode[1]

which is presumably similar to what you did.

The shortest code I can come up with from first principles
is

bysort varname : gen freq = _N * (varname < .)
sort freq
scalar mode = varname[_N]

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

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