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 (May 2006, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 26 May 2006 10:41:37 -0400
Reply-To:     Jim Groeneveld <jim2stat@YAHOO.CO.UK>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Jim Groeneveld <jim2stat@YAHOO.CO.UK>
Subject:      Re: Decimal places and values
Comments: To: Jyothi Nekkalapudi <jnekkalapudi@GMAIL.COM>

Hi Jyothi,

Have look at the log:

1 DATA _NULL_; 2 x = 54.46698; 3 x = PUT (x, 11.4); 4 PUT x=; 5 RUN; NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 3:7 x=54.467

Variable x is numeric, is been PUT to a _character_ value with format 11.4 as 54.4670, the correctly rounded value, and subsequently implicitly converted back to numeric before being stored in the numeric variable x as a value that prints as 54.467 .

If you store the result PUT in y, which then is a character variable, the result is (also correct rounding):

1 DATA _NULL_; 2 x = 54.46698; 3 y = PUT (x, 11.4); 4 PUT y=; 5 RUN; y=54.4670

If you want rounding of a numerical value while staying a numerical value you should use the ROUND function:

1 DATA _NULL_; 2 x = 54.46698; 3 x = ROUND (x,.0001); 4 PUT x=; 5 RUN; x=54.467

This is a correct rounding result, the trailing 0 is not printed.

Now if you don't want rounding, but just truncating, then do so by subtracting half the smallest unit before rounding (the functions TRUNC and FLOOR are not useful here):

1 DATA _NULL_; 2 x = 54.46698; 3 x = ROUND (x-.00005,.0001); 4 PUT x=; 5 RUN; x=54.4669

Another way is a picture format and a character variable Y:

1 PROC FORMAT; 2 PICTURE _4decs 3 OTHER = 00.9999 4 ; 5 RUN; 6 7 DATA _NULL_; 8 x = 54.46698; 9 y = PUT (x, _4decs.); 10 PUT y=; 11 RUN; y=54.4669

But the ultimate, unavoidable and always posed question to you is: why would you do this if you can store the whole original unrounded value and only truncate it at the moment of presentation using a (picture) format?

Regards - Jim. -- Jim Groeneveld, Netherlands Statistician, SAS consultant home.hccnet.nl/jim.groeneveld

On Fri, 26 May 2006 09:49:44 -0400, Jyothi Nekkalapudi <jnekkalapudi@GMAIL.COM> wrote:

>Hello, >I have a question regarding limiting the number of decimal values. In a >SAS dataset I have 8 variables whose values need to be restricted to 4 >places and 2 variables whose values need to be restricted to 2 places. > >If x = 54.46698 in the current SAS dataset, I want x = 54.4669. > >I have used put function > do over pd; > pd = put(pd,11.4); > end; > >which gives me a value of 54.467. >How can I avoid this rounding off and get the precise value, and limit the >decimal places. >Any help will be great.Thank You.


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