LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (October 2006, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Tue, 3 Oct 2006 13:48:43 -0400
Reply-To:   mah-j@statworks.com
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   "mah-j@statworks.com" <msoobader@VERIZON.NET>
Subject:   Re: coding to decimals precision
Comments:   To: chris@OVIEW.CO.UK
In-Reply-To:   <1159889090.304413.284650@h48g2000cwc.googlegroups.com>
Content-type:   text/plain; charset=iso-8859-1

Thank you Chris, I appreciate your detailed and precise response to my question. You are correct- that is exactly what the problem is- this is an existing public use dataset- and the variable was stored as length 3.

Much appreciated, Mah-J

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU]On Behalf Of chris@OVIEW.CO.UK Sent: Tuesday, October 03, 2006 11:25 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: coding to decimals precision

Hi Mah-J,

Your variable "s7q28" probably has a length of less than 8. Numeric variables in SAS default to have length 8, but this can be reduced as low as 3. The length refers to the number of bytes used to store the floating-point binary representation of the number and *not* the number of digits in the decimal representation of the number. Reducing the number of bytes doesn't affect the range of values that can be stored, but it does reduce the precision, so that (in your case) 0.1 can no longer be stored precisely.

It's a common mistake for programmers to specify a length of 3 for a numeric because they know that they will only be storing numbers less than 100 - however, that's *not* how it works!

Another important point is that all numerics are expanded to 8 bytes during data step processing, even if they are then reduced in precision for storage. This can lead to strange results like this:

82 data test; 83 length x 3; 84 x = 0.1; 85 put x=; 86 if x = 0.1 then put "Bingo!"; 87 run;

X=0.1 Bingo! NOTE: The data set WORK.TEST has 1 observations and 1 variables. NOTE: DATA statement used: real time 0.10 seconds cpu time 0.01 seconds

88 89 data _null_; 90 set test; 91 put x=; 92 if x = 0.1 then put "Bingo!"; 93 run;

X=0.0999908447 NOTE: There were 1 observations read from the data set WORK.TEST. NOTE: DATA statement used: real time 0.00 seconds cpu time 0.01 seconds

where the lack of precision only starts causing a problem in the second data step.

The only reason to use less than the default length is to save disk space, which is almost never an issue nowadays, so just keep to the default length and you'll be fine.

Hope this helps,

Chris. -------------------------------------------------------- Elvis SAS Log Analyser - http://www.oview.co.uk/elvis --------------------------------------------------------

"mah-j@statworks.com" wrote: > Hi all, > > I came across something strange - and wanted to know why. > > Why is that when I code: if s7q28=0 then s7q28=0.1 and I run a frequency- I > get this variable coded to 0.0999908447 > > However if I code it to 0.5 - it recodes to exactly 0.5 > > Thanks > Mah-J


Back to: Top of message | Previous page | Main SAS-L page