turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- counting significant digits

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-27-2013 02:51 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-27-2013 03:08 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-27-2013 04:18 PM

What about integers?

var=

var=

var=

var=

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-27-2013 04:35 PM

Hi DN,

Guess I am not a very good student.

Haikuo

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-27-2013 05:02 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-28-2013 02:22 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

03-28-2013 02:44 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-27-2017 05:30 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-27-2017 05:45 AM

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.