Bookmark and Share

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   "Andreas Drichoutis" <[email protected]>
To   <[email protected]>
Subject   RE: st: variable format (graphics example)
Date   Fri, 11 Mar 2011 08:09:05 +0200

Hi all,

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.

Thanks a lot for the help,
Andreas Drichoutis

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nick Cox
Sent: Friday, March 11, 2011 2:42 AM
To: [email protected]
Subject: Re: st: variable format (graphics example)

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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index