BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fastb
Fluorite | Level 6

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
robby_beum
Quartz | Level 8

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;

View solution in original post

8 REPLIES 8
Haikuo
Onyx | Level 15

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

robby_beum
Quartz | Level 8

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;

art297
Opal | Level 21

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

fastb
Fluorite | Level 6

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

art297
Opal | Level 21

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

MikeZdeb
Rhodochrosite | Level 12

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

GeoffreyBrent
Calcite | Level 5

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.

fastb
Fluorite | Level 6

Thank you to everyone for your help with this!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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