Contributor
Posts: 73

# counting significant digits

here is a fun project.  I can think of many brute force approaches but I'm wondering if there is a better way.

I need to be able to count how many decimal places have been entered into into a field

So if they enter 1.234 I need the output to say 3 and if it is 1.0 I need the output to say 1

Posts: 3,167

## Re: counting significant digits

Posted in reply to wkossack_nspirehealth_com

Not sure what you are really after, but if the variable is char, it is easy; if it is number, then you need to make some decision and convert it to char, but it is still not that hard:

/*if char*/

data have;

input var \$20.;

call scan (var,-1,p,length);

cards;

1.234

1.0

25.65

;

Length is supposed the ONE.

Haikuo

Posts: 3,852

## Re: counting significant digits

16         GOPTIONS ACCESSIBLE;
17         data _null_;
18           input var \$20.;
19           call scan (var,-1,p,length);
20           d = findC(var,'.','T',-length(var));
21           if d
22               then dec=length(var)-d;
23               else dec=0;
24           put (_all_) (=);
25           cards;

var=
1.234 p=3 length=3 d=2 dec=3
var=
1.0 p=3 length=1 d=2 dec=1
var=
25.65 p=4 length=2 d=3 dec=2
var=
156 p=1 length=3 d=0 dec=0
Posts: 3,167

## Re: counting significant digits

Posted in reply to data_null__

Hi DN,

Guess I am not a very good student.

Haikuo

Super Contributor
Posts: 578

## Re: counting significant digits

Posted in reply to wkossack_nspirehealth_com

If I remember my number theory correctly, that's not the technical definition of significant digits which has to do with numeric precision.  There are rules around how to count them.  For example, 0.000052 has two significant digits while 0.520000 has 6.

but...I may not be remember the theory correctly...and it may be more information than you actually need.

PROC Star
Posts: 1,307

## Re: counting significant digits

I vote with you. Given that trailing zeroes can be significant, and that because of decimal/hexadecimal conversion issues single decimal digits may blow out to multiple digits in internal representation, I suspect there's no real way to do this.

Tom

New Contributor
Posts: 2

## Re: counting significant digits

Posted in reply to wkossack_nspirehealth_com

As already mentioned, there is a difference between significant digits (as in the title of your question) and the number of decimal places (as in the body of your question). My thoughts on this are that you need to know something about the data in order to determine how many significant digits should be used, regardless of how many decimals have been entered. However, if you do actually want to count decimal places and the field is numeric, I would query the entire dataset and take the maximum decimal places seen across all related records.

New User
Posts: 1

## Re: counting significant digits

If you want to count the number of significant digits, you need to combine the information of the precision (how many digits does the number have) and the order of magnitude (how large is the number).

You may use the formula:

significant_digits = lengthn(scan(number,2,'.')) + floor(log10(input(number,best.))) + 1;

Or in code:

data sign_digits;
input number \$20.;
precision = lengthn(scan(number,2,'.'));
magnitude = floor(log10(input(number,best.)))+1;
significant_digits = lengthn(scan(number,2,'.')) + floor(log10(input(number,best.))) + 1;
cards;
1.234
1.0
25.65
14
0.00002400
run;

Contributor
Posts: 46

## Re: counting significant digits

Posted in reply to wkossack_nspirehealth_com

This should suffice.

``````data sample;
input Values;
datalines;
9.999
3.1245
919.0001
123.9999999
10
;
run;

data sample;
set sample;
cc=(scan(Values,2,"."));

if cc="" then
count=0;
else count=length(cc);
drop cc;
run;``````

-Hope it Helps.

Discussion stats
• 8 replies
• 1926 views
• 0 likes
• 8 in conversation