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: problem with `r(min)' and `r(mean)' macros

From   Nick Cox <>
Subject   Re: st: problem with `r(min)' and `r(mean)' macros
Date   Mon, 28 Jan 2013 15:01:07 +0000

This is a precision issue, biting you twice over.

First off, there is a loss of precision possible with using e.g.
`r(min)' rather than r(min) which is roughly parallel to the lower
precision of macros compared with scalars.

!!! Don't use local macros if you seek maximum precision with non-integers. !!!

Second, testing for exact equality can be tricky at the best of times
for reasons often rehearsed on this list and elsewhere.

. search precision

in an up-to-date Stata and start with Bill Gould's blog postings.

Try this instead:

set obs 1000
gen var1 = runiform()
su var1
scalar max = r(max)
scalar min = r(min)
count if var1 == max
count if var1 == min

Whether the mean of random numbers on the unit interval is equal to
any particular random number is surely of less interest.


On Mon, Jan 28, 2013 at 2:47 PM, mario fiorini <> wrote:
> Dear statalist,
> I am summarising a variable and then trying to use the `r(.)' macros
> However, in some instances Stata does not seem to find any variables
> that take the `r(min)' and `r(mean)' values. For some reason, it does
> work with `r(max)' instead.
> The code below should clarify the problem. When I run it, I always get
> that no observation takes the min or the mean (why???), but there is
> an observation that takes the max.
> The problem does not depend on whether I use the original `r(min)' or
> a macro=`r(min)'.
> If it matters I am using a mac osx 10.8 with Stata 12.1 MP.
> Any hint would be appreciated because it is driving me crazy.
> Mario
>  // Start
> clear
> set obs 1000
> ge id = _n
> ge var1 = uniform()
> su var1
> local LCmin =`r(min)'
> local LCmean =`r(mean)'
> local LCmax =`r(max)'
> count if var1==`LCmin'
> count if var1==`LCmean'
> count if var1==`LCmax'
>  // Ends
*   For searches and help try:

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