BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Anuz
Quartz | Level 8

Hi,

 

Is there a SAS function to calculate the number of digits in a numeric field ? 

 

For example: I have a roll number numeric field. I want the count to show as below 

 

ROLL NUMBER    COUNT

1234567                 7

12345                     5

1212                       4

 

Thank you 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

If you're working with integers only, then try the approach in the blog post Compute the number of digits in an integer by @Rick_SAS.

 

data have;
input ROLLNUMBER;
count = ceil(log10(ROLLNUMBER + 1)); 
datalines;
1234567 
12345   
1212    
;

 

Result:

 

ROLLNUMBER count
1234567    7
12345      5
1212       4

 

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

If you're working with integers only, then try the approach in the blog post Compute the number of digits in an integer by @Rick_SAS.

 

data have;
input ROLLNUMBER;
count = ceil(log10(ROLLNUMBER + 1)); 
datalines;
1234567 
12345   
1212    
;

 

Result:

 

ROLLNUMBER count
1234567    7
12345      5
1212       4

 

Anuz
Quartz | Level 8

thank you @PeterClemmensen . That is exactly what I needed. 

Tom
Super User Tom
Super User

If you know the value is an integer (a whole number) then you could just convert it to a string and count the number of digits.

  digits=length(cats(whole_number));

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

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
  • 3 replies
  • 3028 views
  • 3 likes
  • 3 in conversation