Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: real vs destring


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: real vs destring
Date   Fri, 4 Apr 2008 17:25:11 +0100

To add to Phil's comments: 

In essence, -destring- is a more or less elaborate or intelligent
wrapper 
for -real()-. So, a short answer is: if used appropriately, they should
give the same result. 

Note crucially here that -real()- has two arguments, a string for
conversion and 
a format to use in converting it. You can omit the second argument and
thereby use the default format. That is what Nicholas Harrigan did. He
also by default assigned his result to a float variable. 

For his values, those are not good choices. -destring- is a bit smarter,
but in turn there are limits to its smartness. 

Nick
[email protected] 

Phil Schumm

On Apr 4, 2008, at 9:10 AM, Nicholas Harrigan wrote:

> I am trying to recode a variable containing large numbers which are  
> currently held as strings.
>
> I am wondering why real and destring give different results

<snip>

> gen new_var = real(old_var)
> gen new_var2 = old_var
> destring new_var2, replace


This is because your values (e.g., 5560001090 and 5560001108) are too  
large to be stored accurately as floats.  -destring- is smart enough  
to recognize this, and, I believe, notifies you when it creates a  
variable as a double.  You would get the same result if you used

     gen double new_var = real(old_var)

instead (see [U] 12.2.2 Numeric storage types for more information).


> Does this mean I should use always use destring in my coding  
> instead of real?


Not a bad idea, since -destring- will balk if the conversion involves  
a loss of information.  However, you should always give some thought  
as to how you are storing your data and its implications for what you  
are doing with them.


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index