Hi all,
Just like the tile says, I'm Looking for a function which counts the number of numeric digits in a variable. I know there is an easy answer to this question but can't seem to find it. Thanks
ID | Number count |
17465 | 5 |
571567 | 6 |
29 | 2 |
144 | 3 |
5 | 1 |
if your id is char, length function will do
count=length(id);
Sorry I should have specified ID is a numeric variable
AS the variable is char type you can use:
count = length(strip(ID));
Well in that case @Shmuel solution will work for both. However with a log note-->NOTE: Numeric values have been converted to character
values at the places given by: (Line) : (Column).
data have;
input x;
format x best.;
nbDigits = lengthn(compress(put(x, best. -l)," ","kd"));
datalines;
1
12
123
0000123
0.00123
-123
123000
;
proc print; run;
nb Obs x Digits 1 1 1 2 12 2 3 123 3 4 123 3 5 0.00123 6 6 -123 3 7 123000 6
Depends upon what you want! Should 000123 count as 3 digits, or six?. If it's the latter, you could use:
data have;
input x $;
nbDigits = lengthn(compress(x,,"kd"));
datalines;
1
12
123
123 4
0000123
0.00123
-123
123000
;
Art, CEO, AnalystFinder.com
data have;
input id;
count=int(log10(id))+1;
cards;
17465
571567
29
144
5
;
run;
proc print;run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.