```Date: Thu, 17 Feb 2011 10:00:54 -0500 Reply-To: Arthur Tabachneck Sender: "SAS(r) Discussion" From: Arthur Tabachneck Subject: Re: Counting digits Slight correction: it turns out to be 50% faster than the solution I proposed. Art -------- On Thu, 17 Feb 2011 09:58:28 -0500, Arthur Tabachneck wrote: >Nat, > >However, it does work when one adds an abs function, e.g.: > result = int( log10( abs(y) ) ) + 1; > >and turns out to be twice as fast as the solution I proposed. > >Art >-------- >On Thu, 17 Feb 2011 09:55:35 -0500, Nat Wooding wrote: > >>Art >> >>You are correct. A log of a negative number is missing. >> >>Nat >> >>-----Original Message----- >>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Arthur >>Tabachneck >>Sent: Thursday, February 17, 2011 9:42 AM >>To: SAS-L@LISTSERV.UGA.EDU >>Subject: Re: Counting digits >> >>Lorne, >> >>I don't think the Nat/Howard solution will work with negative numbers >unless >>you add the abs function as well. >> >>Art >>------- >>On Thu, 17 Feb 2011 09:30:36 -0500, Lorne Klassen wrote: >> >>>I like this (from Nat and modified by Howard S): >>> >>>numbers = int( log10( y ) ) + 1; >>> >>>thanks >>> >>> >>>On Thu, 17 Feb 2011 07:20:23 -0500, Nat Wooding >>wrote: >>> >>>>Lorne >>>> >>>>The INT would be a definite improvement to my code. >>>> >>>>Nat >>>> >>>>-----Original Message----- >>>>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of >Lorne >>>>Klassen >>>>Sent: Wednesday, February 16, 2011 11:51 PM >>>>To: SAS-L@LISTSERV.UGA.EDU >>>>Subject: Re: Counting digits >>>> >>>>Thanks Tom and Nat. Be careful with this though: >>>> >>>>length(left(put(abs(y),20.))) >>>> >>>>which won't work in rare cases. E.g., if y=99999.99 then PUT will round >>and >>>>return 100000 and the result will be 6 instead of the correct value, 5. >>>> >>>>That's why I apply the INT function first. >>>> >>>> >>>>On Wed, 16 Feb 2011 22:55:23 -0500, Tom Abernathy >> >>>>wrote: >>>> >>>>>To deal with the negative numbers add an ABS() function call. >>>>> >>>>>Also could just use >>>>> >>>>> length(left(put(abs(y),20.))) >>>>> >>>>>Which does not seem to get confused by 10,000,000. >>>>> >>>>>On Wed, 16 Feb 2011 22:07:46 -0500, Nat Wooding >>>>wrote: >>>>> >>>>>>Lorne >>>>>> >>>>>>The following seems to work ok. >>>>>> >>>>>>Nat Wooding >>>>>> >>>>>> >>>>>>data num; >>>>>>input y ; >>>>>>numbers = ceil( log10( y + .1 ) ); >>>>>>format y comma15.; >>>>>>* the addition of the .1 was needed to get the 10000000 to work; >>>>>>cards; >>>>>>7 >>>>>>76 >>>>>>768 >>>>>>6000 >>>>>>6500 >>>>>>6900 >>>>>>7687 >>>>>>76877.234 >>>>>>800000 >>>>>>899999 >>>>>>9000000 >>>>>>10000000 >>>>>>100000000 >>>>>>run; >>>>>> >>>>>>-----Original Message----- >>>>>>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of >>>Lorne >>>>>>Klassen >>>>>>Sent: Wednesday, February 16, 2011 6:47 PM >>>>>>To: SAS-L@LISTSERV.UGA.EDU >>>>>>Subject: Counting digits >>>>>> >>>>>>Simple question: I want to get the number of digits to the left of the >>>>>>decimal place from a value in a numeric variable (excluding negative >>sign >>>>>>if there is one). >>>>>> >>>>>>I can live with the following which works but I'm wondering if it can >be >>>>>>even simpler than this? I thought there might be a numeric function to >>do >>>>>>it directly but couldn't find one. >>>>>> >>>>>>y = 76877.234; >>>>>>result = countc(put(int(y),32.),,"d"); >>>>>> or >>>>>>result = length(compress(put(int(y),32.)," -")); ```

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