Fluorite | Level 6

## Number of Decimal Places

Does anyone know of a procedure where the input is a number and the output is the number of decimal places in that number?

For example, if I enter 10.8605, I need the output to be either 0.0001 or 4.

1 ACCEPTED SOLUTION

Accepted Solutions
Quartz | Level 8

## Re: Number of Decimal Places

This shoukld work as well.

DATA one ;
INPUT v1 ;
CARDS ;
10.1234
123.125
1.123456
;

data two;
set one;
first = substr(v1, 1, index(v1, '.') - 1);
second = left(substr(v1, index(v1, '.') + 1));
remainder = LENGTH(second);
run;

8 REPLIES 8
Onyx | Level 15

## Re: Number of Decimal Places

Hi, try a simple data step:

data have;

a=10.8605;

b1=lengthn(strip(a))-lengthn(strip(floor(a)))-1;

b2=1/10**b1;

put b2= b1=;

run;

Haikuo

Quartz | Level 8

## Re: Number of Decimal Places

This shoukld work as well.

DATA one ;
INPUT v1 ;
CARDS ;
10.1234
123.125
1.123456
;

data two;
set one;
first = substr(v1, 1, index(v1, '.') - 1);
second = left(substr(v1, index(v1, '.') + 1));
remainder = LENGTH(second);
run;

Opal | Level 21

## Re: Number of Decimal Places

Are you interested in number of digits in string or number of significant digits?  e.g., given the following numbers, what result would you expect for each?:

125.

125.0

125.00

125.000

125.0000

Fluorite | Level 6

## Re: Number of Decimal Places

Input                     Result

125.                   1.0 or 0

125.0                 0.1 or 1

125.00               0.01 or 2

125.000             0.001  or 3

125.0000           0.0001 or 4

Opal | Level 21

## Re: Number of Decimal Places

Then, whether you use Haikuo's or Robby's suggested code, make sure that your input variable is a character variable.

Rhodochrosite | Level 12

## Re: Number of Decimal Places

hi ... with all those zeroes (e.g. 125.0000) there won't be any decimal places since 125.0000 is a really an integer, yes/no?

data x;

input x @@;

format x 10.4;

datalines;

125 125.1 125.01 125.001 125.0001 125.0000

;

data x;

set x;

d = lengthn(scan(cat(x),2,'.'));

run;

Obs             x    d

1       125.0000    0

2       125.1000    1

3       125.0100    2

4       125.0010    3

5       125.0001    4

6       125.0000    0

Calcite | Level 5

## Re: Number of Decimal Places

Adding to Arthur's advice: even if you don't want to count trailing zeroes, the methods above should ONLY be used on character inputs.

On my computer, all three of the code examples above give the wrong answer for an input of 0.000001 or 10000000.1234. The reason for this is that their inputs are defined as numeric by default. When you perform a string operation (STRIP, INDEX, SUBSTR, CAT) on a numeric variable you force SAS to do an implicit conversion from num to char, and the format it selects may not be the one you're expecting. Watch out for that "NOTE: Character values have been converted to numeric values" in the log.

In this case, a numeric value of 0.000001 gets converted to "1E-6" not "0.000001" and 10000000.1234 gets converted to "1000000.123", both of which lead to the wrong answer. Two of these examples assume that there will be a decimal point, and so they give the wrong answer for an integer input.

Possibly worth adding a check in the program to return a warning/error message if this is supplied with numeric input, since it would be a VERY easy mistake to make.

Fluorite | Level 6

## Re: Number of Decimal Places

Thank you to everyone for your help with this!

Discussion stats
• 8 replies
• 4343 views
• 6 likes
• 6 in conversation