BookmarkSubscribeRSS Feed
RobertNYC
Obsidian | Level 7

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
7 REPLIES 7
novinosrin
Tourmaline | Level 20

if your id is char, length function will do

count=length(id);

RobertNYC
Obsidian | Level 7

Sorry I should have specified ID is a numeric variable 

Shmuel
Garnet | Level 18

AS the variable is char type you can use:

     count = length(strip(ID));

novinosrin
Tourmaline | Level 20

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).

PGStats
Opal | Level 21
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
PG
art297
Opal | Level 21

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

 

Ksharp
Super User
data have;
input id;
count=int(log10(id))+1;
cards;
17465	
571567	
29	
144	
5
;
run;
proc print;run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 6961 views
  • 2 likes
  • 6 in conversation