####
**Live Online Training :**
SAS Programming with 50+ Case Studies

- Explain Programming Concepts in Simple English

- Live Projects

- Scenario Based Questions

- Job Placement Assistance

- Get 20% off till July 14, 2017

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.

We would use the following dataset to demonstrate the way to add

**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 |

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

ReplyDeleteThank you for your appreciation.

DeleteGreat stuff. Keep it up!

ReplyDeleteThanks for that..!

ReplyDeleteIt saves me lot of time..!

Great..