```Date: Mon, 17 Jul 2006 10:20:29 -0400 Reply-To: Michael Ni Sender: "SAS(r) Discussion" From: Michael Ni Subject: Re: How to calculate number of decimals Comments: To: "Howard Schreier " In-Reply-To: <200607170200.k6GJtFm6029008@mailgw.cc.uga.edu> Content-Type: text/html; charset=us-ascii Howard,

Thanks a lot for your  detailed explanation!

Best Regards,

Howard Schreier <hs AT dc-sug DOT org> wrote:
On Fri, 14 Jul 2006 12:45:48 -0400, Howard Schreier <hs AT dc-sug DOT org> <nospam@HOWLES.COM> wrote:
On Fri, 14 Jul 2006 09:58:41 -0400, Michael Ni <michael.ni@COGNIGENCORP.COM> wrote:
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
Request for clarification: let    x = 1 / 3 ;  What would be considered the correct number of decimal places?
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.

```

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