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: Converting numbers to string: %f versus %g


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Converting numbers to string: %f versus %g
Date   Thu, 22 Aug 2013 18:56:27 +0100

The major innovation in Stata 10 was the extension from dates to date-times.

If I recall correctly the history of date support from StataCorp runs
like this.

Prehistory. Stata did not exist. Hard to imagine for young users, but
some remember.

Ancient history. Stata introduced daily dates (origin 1 January 1960).
Important note: the name -date()- now appears ill-chosen, but made
more sense back when _daily_ dates were the only kind of date on
offer. -daily()- exists as a synonym.

Early modern history. Stata added support for dates in weeks, months,
quarters, half-year and years. (Not much support needed for years as
such.)

Version 10. Date-times added, down to milliseconds.

In terms of your question, I would use -tostring- only if I were
converting several run-together date variables at once. Otherwise
-string()- with an %f format sounds best. I wouldn't use -todate-
because I would then have to read my help file and rediscover the
syntax, but I am happy at the thought that it's still useful and in
use.
Nick
[email protected]


On 22 August 2013 18:45, Joe Canner <[email protected]> wrote:
> For Stata>=10 is there a better way to do this than a combination of -date()- and -string()- (or -tostring-)?  Or is the ability of -date()- to handle run-together dates new what is new with Stata 10?
>
> Moribund or not, for Stata>=10 users I think -todate- might still be preferable to trying to remember the correct form of -date(string())- required. (Although for large data sets I suppose there might be performance considerations.)
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: Thursday, August 22, 2013 1:37 PM
> To: [email protected]
> Subject: Re: st: Converting numbers to string: %f versus %g
>
> Thanks. -todate- (SSC), however, is in essence a program dating back to 2001. Its rationale was that Stata did not (then or indeed
> previously) support easy import of "run together" dates until version 10.
>
> Moreover, -todate- does not support date-times, which again Stata didn't support until version 10.
>
> It remains accessible for two major reasons:
>
> 1. There are people still using Stata <10.
>
> 2. If anybody's program or do file uses -todate- then the program should be accessible too.
>
> However, it is moribund and will not be revisited by me unless someone finds a bug.
> Nick
> [email protected]
>
>
> On 22 August 2013 18:29, Joe Canner <[email protected]> wrote:
>> Nick,
>>
>> Thanks for the caution (and the good laugh).  If nothing else, the process of reviewing and documenting this problem  will (hopefully) remind me not to use %g in the future for situations like this (and to use -todate- where applicable).
>>
>> Regards,
>> Joe
>>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Nick Cox
>> Sent: Thursday, August 22, 2013 1:03 PM
>> To: [email protected]
>> Subject: Re: st: Converting numbers to string: %f versus %g
>>
>> My impression is that %g is more complicated than you would really want documented. It tries to be as smart as possible for a great range of situations, but it's hard to predict exactly what it does.
>>
>> A little like the usual R stories about what "R" stands for (next to "S" but first written by a Robert and a Ross), "g" does follow "f" but also stands for Gould. Could be curious coincidence.
>>
>> -todate- is from SSC.
>>
>> Nick
>> [email protected]
>>
>>
>> On 22 August 2013 17:43, Joe Canner <[email protected]> wrote:
>>> Dear Stata community,
>>>
>>> According to the (Stata 12) help for -format-: " %g differs from %f in that (1) it decides how many digits to display to the right of the decimal point, and (2) it will switch to a %e format if the number is too large or  too small."
>>>
>>> However, there seem to be more differences than this.  I have a numeric (long) variable with a date in the form YYYYMMDD that I want to convert to a date variable. I did the following:
>>>
>>> . gen DOS=date(string(DateofService,"%8.0g"),"YMD")
>>>
>>> This didn't work, because the -string()- function returned the same value ("2.0e+07") regardless of the value of DateofService.  This suggests that the %g format thinks the number is too large for the format. This can be fixed with one of the following:
>>
>>>
>>> . gen DOS=date(string(DateofService,"%10.0g"),"YMD")
>>> . gen DOS=date(string(DateofService,"%8.0f"),"YMD")
>>>
>>> My question is this: why does %f work with the right number of digits (8), but %g only works if you overstate the number of digits?  Is the required number always two more than the actual number of digits?
>>>
>>> This is mostly a curiosity question, but it sometimes trips me up when I instinctively/mindlessly choose %8.0g and then wonder why my code isn't working.  I also sometimes forget the format and I wonder why -string()- can't guess the correct format, but that is another story...
>>>
>>> (And, yes, I recently became aware of Nick Cox's -todate- program and
>>> will probably use that from now on.)
>>>
>>> Thanks,
>>> Joe Canner
>>> Johns Hopkins University School of Medicine
>>>
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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