Date: Mon, 17 Jul 2006 10:20:29 -0400
Reply-To: Michael Ni <Michael.Ni@COGNIGENCORP.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Michael Ni <Michael.Ni@COGNIGENCORP.COM>
Subject: Re: How to calculate number of decimals
In-Reply-To: <200607170200.k6GJtFm6029008@mailgw.cc.uga.edu>
Content-Type: text/html; charset=us-ascii
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Howard,<br>
<br>
Thanks a lot for your detailed explanation!<br>
<br>
Best Regards,<br>
<br>
Howard Schreier <hs AT dc-sug DOT org> wrote:
<blockquote cite="mid200607170200.k6GJtFm6029008@mailgw.cc.uga.edu"
type="cite">
<pre wrap="">On Fri, 14 Jul 2006 12:45:48 -0400, Howard Schreier <hs AT dc-sug DOT org>
<a class="moz-txt-link-rfc2396E" href="mailto:nospam@HOWLES.COM"><nospam@HOWLES.COM></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">On Fri, 14 Jul 2006 09:58:41 -0400, Michael Ni <a class="moz-txt-link-rfc2396E" href="mailto:michael.ni@COGNIGENCORP.COM"><michael.ni@COGNIGENCORP.COM></a>
wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Hi SAS experts,
I want to calculate the number of decimals. Since my code generates
NOTE:Numeric values have been converted to character. I am seeking better
solutions here.I am on 8.2. Thanks!
data one;
x=3.555; output;
x=4; output;
run;
data two;
set one;
y=indexc(compress(x),'.');
z=length(compress(x));
if y eq 0 then num=0;
else num=z-y;
run;
proc print;
run;
Thanks for your help!
Michael
</pre>
</blockquote>
<pre wrap="">Request for clarification: let
x = 1 / 3 ;
What would be considered the correct number of decimal places?
</pre>
</blockquote>
<pre wrap=""><!---->
I'll try a different approach.
X in Michael's original example is a *numeric* variable. SAS stores and
manipulates numeric variables using *binary* representations. Thus, the
concept of *decimal* places does not exist in this context. It only arises
when values are converted to character strings, usually for presentation.
So the answer (the number of decimals) depends very much on how that
conversion is done. Consider:
data var;
var = 1/3;
put var 5.3;
put var;
length charvar $ 13;
charvar = var;
put charvar;
output;
var = 1e5;
output;
run;
proc print data=var; var var; run;
proc print data=var(obs=1); var var; run;
The first PUT statement has a format, so it explicitly controls the
conversion and specifies the number of decimal places. The other conversions
are automatic. The 3 PUT statements and 2 PROC PRINTS present 1/3 as:
0.333
0.3333333333
0.33333333333
0.33
0.33333
So generally there is no single answer to the question.
</pre>
</blockquote>
<br>
</body>
</html>