BookmarkSubscribeRSS Feed
wkossack_nspirehealth_com
Calcite | Level 5

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

8 REPLIES 8
Haikuo
Onyx | Level 15

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

data_null__
Jade | Level 19

What about integers?

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
Haikuo
Onyx | Level 15

Hi DN,

Guess I am not a very good student. Smiley Wink

Haikuo

DBailey
Lapis Lazuli | Level 10

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.

TomKari
Onyx | Level 15

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

hey_tc
Calcite | Level 5

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.

jeppesj
Calcite | Level 5

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;

ankit___gupta
Quartz | Level 8

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.

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5293 views
  • 0 likes
  • 8 in conversation