The SAS Output Delivery System and reporting techniques

Looking for a function which counts the number of numeric digits in a variable

Reply
Frequent Contributor
Posts: 104

Looking for a function which counts the number of numeric digits in a variable

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
Super User
Posts: 2,062

Re: Looking for a function which counts the number of numeric digits in a variable

Posted in reply to RobertNYC

if your id is char, length function will do

count=length(id);

Frequent Contributor
Posts: 104

Re: Looking for a function which counts the number of numeric digits in a variable

Posted in reply to novinosrin

Sorry I should have specified ID is a numeric variable 

Trusted Advisor
Posts: 1,848

Re: Looking for a function which counts the number of numeric digits in a variable

Posted in reply to RobertNYC

AS the variable is char type you can use:

     count = length(strip(ID));

Super User
Posts: 2,062

Re: Looking for a function which counts the number of numeric digits in a variable

[ Edited ]
Posted in reply to RobertNYC

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

Esteemed Advisor
Posts: 5,626

Re: Looking for a function which counts the number of numeric digits in a variable

Posted in reply to RobertNYC
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
Super User
Posts: 8,218

Re: Looking for a function which counts the number of numeric digits in a variable

Posted in reply to RobertNYC

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

 

Super User
Posts: 10,850

Re: Looking for a function which counts the number of numeric digits in a variable

Posted in reply to RobertNYC
data have;
input id;
count=int(log10(id))+1;
cards;
17465	
571567	
29	
144	
5
;
run;
proc print;run;
Ask a Question
Discussion stats
  • 7 replies
  • 203 views
  • 0 likes
  • 6 in conversation