Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: variable format (graphics example)
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: variable format (graphics example)
Date
Fri, 11 Mar 2011 08:39:51 +0000
Much depends on precisely what you mean by "works" but -string()-
followed by -destring- can't get around the limits to precision
either. It's just a roundabout way of going, in your case,
gen double rvar = round(var, 0.01)
Set your variable's display format to say %23.18f and you will see
that there are higher decimal digits beyond the first in all (or
almost all) values. (Numbers ending in .5 can be held exactly, for
example.)
Nick
On Fri, Mar 11, 2011 at 6:09 AM, Andreas Drichoutis <[email protected]> wrote:
> It worked. I first used generate rvar = string( var ,
> "%9.2f")
> and then destring rvar, replace
>
> this also works: tostring var, g(rvar) format(%9.2f) force
>
> my new variable has exactly 2 decimal points.
>
Nick Cox
> Several misconceptions, some old, some new, have surfaced in this thread.
>
> -round()-'s main role is to round to the nearest integer: that's the
> one argument version.
>
> It allows a second argument, but nothing in that overturns the limits
> to decimal precision implied by binary numbers. While the effect of
>
> . di round(42.42, 1)
> 42
>
> is clear, using -round()- to multiples of 0.01 implies just "convert
> this number as closely as you can to a multiple of 0.01". So, even if
> at first sight it seems that in this example
>
> . di round(42.42, 0.1)
> 42.4
>
> the rounding succeeded, that is just an illusion caused by the lens of
> the display format implied here. In fact the result is not exactly
> 42.4 at all:
>
> . di %23.18f round(42.42, 0.1)
> 42.400000000000006000
>
> Either way, as has been pointed out, using a display format for the
> purpose is the way to get numbers displayed as you wish.
>
> Finally, -tostring- is a convenience and safety-conscious command for
> converting a bundle of variables at once. But whenever you know that
> you want a string variable and that you want to trim away
> representations of high decimal places, then doing it directly with
>
> generate ru = string(u , "%9.2f")
>
> is the way to do it. There is nothing wrong with using -tostring- for
> the purpose, but it is firing up a command with two hundred lines of
> Stata code for iteration, caution and special cases when a one-line
> command gets you there directly. In fact, the -string()- function long
> predates -tostring- and -tostring- is just an elaborate wrapper for
> -string()-, so it's bizarre to see that being overlooked.
>
> Of course, -tostring- is a great command when you really need it.
>
> Nick
>
> On Thu, Mar 10, 2011 at 10:12 PM, Feiveson, Alan H. (JSC-SK311)
> <[email protected]> wrote:
>
>> Daniel - That's because I was trying to implement -tostring- by hand!
>>
>> Thanks for making me aware of it.
>
> Daniel Klein
>
>> Changing
>>
>> . gen ru = round(u,.01)
>>
>> to
>>
>> . tostring u ,g(ru) format(%9.2f) force
>>
>> in your example, might be what you are looking for. However, I do not see
> how this is "too much work", so probably I don't see the point.
*
* 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/