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;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 7 replies
  • 5205 views
  • 2 likes
  • 6 in conversation