BookmarkSubscribeRSS Feed
Cat_Dreamer
Calcite | Level 5

I am trying to left-zero-pad an existing list of character-formatted numbers. The numbers vary from 6 digits to 20 digits. I want to zero-pad them to a length of 25.

 

The problem that I am having is that every method I can find to zero-pad involves using input() to change the character-formatted numbers to a number format. Because some of my numbers are greater than 16 digits, I am losing precision when I do that. It rounds up any numbers past the sas-recognized 16 limit. 

 

Is there any way to concatenate 0's to the left of a character-formatted field (up to 25 chars) while keeping it a character-formatted field??

3 REPLIES 3
heather_g
Fluorite | Level 6

I don't have time at the moment to write out all the SAS code, but yes, there are ways.

One way, in a data step - 

use the LENGTH() function to determine the original length of the character variable, then subtract that length from your total expected digits (fiddle with this, you probably have to add 1 in there). Then use the REPEAT() function to repeat zeroes to that number, and concatenate those zeroes with the original variable to create a new variable using one of the CAT() functions (forget if it's CATS or CATT or CATX). All of those functions can be found explained in the internet documentation.

 

ballardw
Super User

This link has several methods for a shorter string and a "less than" length requirement.

Should have somethings that are easy enough to make work with longer strings.

https://communities.sas.com/t5/SAS-Programming/convert-character-value-quot-1X11-quot-to-quot-000001...

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
  • 3 replies
  • 992 views
  • 0 likes
  • 4 in conversation