Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Number of Decimal Places

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 07-30-2012 08:35 AM
(4342 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

**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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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**

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you to everyone for your help with this!

**Don't miss out on SAS Innovate - Register now for the FREE Livestream!**

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

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.