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:36:40 +0100
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/