Statalist


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

st: RE: Re: Problem with replace if - test condition works only for some values of test variable


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: Problem with replace if - test condition works only for some values of test variable
Date   Fri, 24 Oct 2008 14:53:45 +0200

Use -help f_float- instead of -round-, as explained in
http://www.stata.com/support/faqs/data/float.html



HTH
Martin


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Naunihal Singh
Sent: Friday, October 24, 2008 1:58 AM
To: [email protected]
Subject: st: Re: Problem with replace if - test condition works only for
some values of test variable

I think I understand a bit better what's going on.
replace y=1 if x==.26 fails because x actually equals .2599999
. di x[27]
.25999999
(this is strange b/c x was generated with this command: generate x=
(_n/100)-0.01 in 1/101 )
So the problem wasn't that float had too few digits of precision (both
.25 and .26 fit in the same number of bytes), it's that stata was
storing (27/100)-0.01 as .25999999 instead of .26.

I've tried using the round command to tackle this, but I must be doing
something wrong in the syntax:
replace x=round(x,0.01)
(0 real changes made)
even though
di round(.25999999, 0.01)
.26
Is there a problem with using round in replace in that way?

Thanking you all in advance, and Phil for his earlier response,

N


On Thu, Oct 23, 2008 at 6:32 PM, Naunihal Singh
<[email protected]> wrote:
> Please, indulge me. I'm sure I've overlooked something basic, but
> replace if is working only for certain values of the test variable and
> not for others.
>
> Start with any dataset open
> Here is the code fragment that replicates my problem:
>
> ge y=.
> generate x= (_n/100)-0.01 in 1/101
> replace y=1 if x==0
> replace y=1 if x==.01
> replace y=1 if x==.25
> replace y=1 if x==.26
> replace y=1 if x==.5
> replace y=1 if x==.51
> replace y=1 if x==.75
> replace y=1 if x==.76
> replace y=1 if x==1
> replace y=1 if x==.99
>
> I'm able to make assignments to y only when x equals 0, .25, .5, .75,
> and 1 but for no other value. This is the output I get:
>
> . replace y=1 if x==.5
> (1 real change made)
>
> . replace y=1 if x==.51
> (0 real changes made)
>
> Why does this happen and how do I fix it? x is a float, so it should
> have enough precision for the test condition. And if the test can
> recognize .25 it should be able to recognize .26.
>
> Thanks
>
> N
>

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index