How to add leading zeros in SAS?

Published on:
written bySubhro
How To, SAS PROGRAMS, Tips & Tricks

Adding leading zeros is one of the most frequently encountered data problems. However, sometimes it is essential to add leading zeros in SAS while cleaning data. It makes data look much cleaner, easy to read and consistent. This article will show you how easy adding a leading zero’s to a numeric or character variable.

Add leading zeros in SAS to a Numeric Variable.

To add leading zeros in SAS, you have to add a z format to the variables. The Z format writes standard numeric data with leading 0s.


Z w. [d]

w specifies the width of the value.

d specifies the number of digits to the right of the decimal point in the numeric value.

TIP: Give enough width for the value including the decimal point or a minus sign, if necessary.

The Z w. d format writes negative numbers with leading minus signs. In addition, it also right aligns before writing and pads the output with leading zeros.

The column zip should be five digits in the customer dataset, and the value column doesn’t look consistent.

Here is a quick fix for this.

leading zeros

Here is a quick fix for this.

data customer;
set ds.customer;
format zip z5. value z5.2;
proc print;

add leading zeros in SAS

format zip z5. tells SAS to add five leading zeros to the variable zip to maintain five as a length of the zip variable.

In the first observation, it has not added any zeros as the number of digits is already five. However, SAS has added one leading zero in the second observation as it has four values.

Format value z5.2 tells SAS to keep the total number of digits to five, including the decimal point. .2 is for the two decimal values to the number.

Add leading zeros to the Character Variable.

To add leading zeros to the character variable, you can use the combination of REPEAT and CATS function.

We have converted the existing customerid numeric variable to a character for this example.

data test;
	set ds.customer;
	customerid2=put(customerid, 10. -L);
	customerid2=cats(repeat('0', 16-length(customerid2)-1), customerid2);
	drop customerid;
	rename customerid2=customerid;
proc print;

Here, we add leading 0s to make the total digits 16. CATS function is used to concatenate 0s with the variable customerid. Then, the REPEAT function is used to repeat 0s 16 times.

LENGTH function is used to determine the number of characters in the variable customerid 16 – length(customerid) -1, which returns to ( 16- number of letters and values in the variable customerid – 1).

Numeric to Character conversion, keeping the leading zeros

Occasionally your data may contain leading zeros, and you may need to convert the variable type to the character. To keep the leading zero’s, use the z format in the put statement.

data customer1;
set customer;
char_zip=put(zip,z5. -L);

Character to Numeric conversion, keeping the leading zeros

The input function is used to convert characters to numeric, and it requires an informat, so the z format will not work here. Instead, you have to read the give any informat or best format then associate the format z to your variable.

data cust1;
set cust;
format num_zip z5.;

So, this was our side on how to add leading zero’s to the character and numeric variables in SAS.

We hope that you must have found it helpful.

Moreover, if you have any other suggestions, suggest them below the comment section. We would take those lists in our further blog post.

Download the example dataset from here.

Thanks for reading!

If you liked this article, you might also want to read Variable conversion in SAS.

Do you have any tips to add? Let us know in the comments?

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

Every week we'll send you SAS tips and in-depth tutorials


Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

1 thought on “How to add leading zeros in SAS?”

  1. Thanks for the sensible critique. Me and my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more from this post. I’m very glad to see such great info being shared freely out there.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap
%d bloggers like this: