Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: precision Problem taking string from double var


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: precision Problem taking string from double var
Date   Fri, 29 Oct 2010 11:42:19 +0100

I am not entirely clear what you are doing here but knowing about 

. di string(1/7, "%4.2f")
0.14

may help you towards a better strategy. 

Nick 
n.j.cox@durham.ac.uk 

Stefan.Gawrich@hlpug.hessen.de

yesterday I ran into a severe error caused by some precision(?) problem with a double var.
I know what happened but not how it happened and I'm wondering how to avoid this in future.


I use record linkage software which creates Stata output files. The linkage score is stored in a double var. 
I don't need that precision (two decimal places are enough, but I can't change this)

I use a  Stata do-File to export the Stata output files to fixed format text files.
The files are given to a database for further processing.
The linkage score must be  XX.XX (eg. 45.34) in the text files.

I use string functions to take the data out of the linkage score var "quality":

gen str5 linkage_score = substr(string(quality),1,2) + "." + substr(string(quality),4,2) 

Now I found that in one case "45.0000037" the last function produces ""

One can see that ".0000" is o.k. but ".00000" is not.

. di substr(string(45.0000037),4,2)


. di substr(string(45.000037),4,2)
00

This rearranged the var positions in the output files causing a database error

I have huge record linkage numbers so this is likely to happen again. How can I avoid that?


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