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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.