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
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
What about integers?
Hi DN,
Guess I am not a very good student.
Haikuo
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.
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
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.
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;
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.
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.
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.