SAS : Add leading zeros to Variable

This tutorial describes how we can add leading zeros to a numeric or character variable in SAS. It's one of the most frequently encountered data problem. It generally happens when we have some product codes that need to have leading zeros if the product code has a length less than defined length (let's say 10 characters). It sometimes become a daunting task when we merge two tables and the primary key is of different types and leading zeros is missing in one of the tables.

Create Sample Data

We would use the following dataset to demonstrate the way to add leading zeros to the numeric variable 'x'
data xy;
input x y;
cards;
1234 33
123 44
1236 45
;
run;
If the variable in which you want to add leading zeros contains only numeric values, we can simply use Zw.d format. In this case, we made length of the newly transformed variable as 6.
data temp;
set xy;
xx = put(x, z6.);
run;
z6. tells SAS to add 'k' number of leading zeros to the variable 'x' to maintain 6 as a length of the newly transformed variable 'xx'. In this case, 'k' = (6 - number of values in each observation in variable 'x'). In the first observation, it would add 2 leading zeros as the number of values is 4. However, it would add 3 leading zeros in second observation as it has 3 values.

The output is shown below :
Output : Add leading zeros

Add leading zeros to the Character Variable

Suppose you have a character variable in which you want to add leading zeros. In this case, we cannot use zw.d format as it only works for numeric variable.
data xy;
input x$ y;
cards;
A1234 33
A123 44
A1236 45
;
run;
We need to keep 6 as length of the newly transformed variable.
data temp;
set xy;
xx = cats(repeat('0',6-length(x)-1), x);
proc print;
run;
CATS function is used to concatenate 0s with the variable 'x'. REPEAT function is used to repeat 0s. LENGTH function is used to determine the number of characters in the variable 'x'.  6 - length(x) -1 translates to ( 6- number of letters and values in the variable x - 1).

Output

Best Online Course : Practical SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English
- Live Projects & Case Studies
- Job Placement Assistance
- Money Back Guarantee


SAS Tutorials : 100 Free SAS Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

4 Responses to "SAS : Add leading zeros to Variable"

  1. Nice article, thank you man....keep up the good work..

    ReplyDelete
  2. Great stuff. Keep it up!

    ReplyDelete
  3. Thanks for that..!
    It saves me lot of time..!
    Great..

    ReplyDelete

Next → ← Prev