In this example page, we demonstrate how to add leading zeros to a numeric variable in SAS. The first choice to do so for many SAS programmers is some kind of concatenation. This is reasonable from a logical standpoint. However, there is a much simpler and faster way to do so. The Z. Format.

### Add Leading Zeros to Numeric Variable in SAS with the Z. Format

Let us see a simple example of how to add leading zeros to a numeric variable. Simply by applying the Z. Format. In most cases, we want to add leading zeros up to some specified width. Naturally, we have to consider up to what width we wish to apply the leading zeros. In this case, I choose a width of 10.

data want; set have; format x z10.; run; |

If you open the data set, you will see that x has leading zeros up to the specified width of 10. Like below.

x 0000000001 0000000012 0000000123 0000001234 0000012345 0000123456 0001234567 0012345678 0123456789 |

Obviously, we can apply the format directly in reporting procedures as well.

proc print data = have; format x z10.; run; |

A classic use of the Z. Format is to create a new variable. A character representation of the numeric value with a fixed width. Again, the Z. Format is the perfect tool for this purpose.

data want; set have; y = put(x, z10.); run; |

### Summary

In this short example page, we demonstrate how to add leading zeros to a numeric variable in SAS. The solution to this problem is simpler than most SAS programmers think at first. We simply apply the Z. format, which does the work for us.

You can download the entire code from this example page here.