Have you ever tried to add leading zeros to a SAS variable, but you didn’t succeed? You are not the only one. The question “How to add leading zeros in SAS” is one of the most Googled questions regarding SAS.
So, how should you add leading zeros to a variable in a proper way? How to add leading zeros in SAS depends on the type of your variable. If your variable is numeric, you can simply use the PUT statement and the Zw. format. However, if you work with a character variable, you need a combination of the REPEAT function and CAT functions to add leading zeros.
In this article, we discuss how to add leading zeros to a numeric variable and how to add leading zero to a character variable. We also show a method of how you can remove leading zeros in SAS.
How to add Leading Zeros Numeric Variables in SAS
As mentioned before, adding leading zeros to a numeric variable is fairly easy. First, you need the PUT function to transform the numeric variable into a character variable. Then, with the Z format, you can define the final length of your new variable. That is to say if you use the Z5. format, your new variable will have a length of 5. The number of leading zeros depends on the original variable’s length.
In the example below, we have 3 numeric values with different lengths. The goal is create a new variable of length 5.
data work.ds_numeric; input code; datalines; 12 456 9 ; run;
data work.zeros_numeric; set work.ds_numeric; zeros_code = put(code, z5.); run;
How to Add Leading Zeros Character Variables in SAS
For character variables, it’s slightly more difficult to add leading zeros. Firstly, you need to know how many zeros you need to add to your variable. Secondly, you need to create a new character string of these zeros with the REPEAT function. And, finally, you need the CAT function to concatenate this new string of zeros with your original variable. The example below demonstrates how to do this.
In the example below, we have a character variable with values of different lengths. The goal is to create a new variable of length 5.
data work.ds_char; input code $; datalines; 1A2 B3C 4D ; run;
data work.zeros_char; set work.ds_char; zeros_code = cat(repeat('0', 5-length(code)-1),code); run;
The second argument of the repeat function might look a bit confusing. The length of the value in the first row (1A2) is 3. Now 5 – 3 – 1 = 1. However, we need to add 2 leading zeros to 1A2. Since the REPEAT function returns a character value that consists of the first argument repeated n+1 times the second argument needs to be 1.
How to Add Leading Zeros to Number Stored as Character Variable
In the example above, the values of the character variable contain a combination of characters and numbers (e.g., “1A2”). However, if your character variable consists only of numbers, but is stored as a character variable, then adding leading zeros is much easier.
First, you need to convert the character variable into a numeric variable with the INPUT function. Then, you can easily add leading zeros with the PUT function and the Zw. format. See the example below.
data work.ds_num_as_char; input code $; datalines; 12 900 5 ; run;
data work.zeros_num_as_char; set work.ds_num_as_char; zeros_code = put(input(code, best12.), z5.); run;
How to Remove Leading Zeros in SAS
Although it probably happens less often than adding leading zeros, you sometimes need to remove leading zeros. Suppose we have the following dataset.
data work.ds_leading_zeros; input code $; datalines; 0012 0900 01AB 00YXZ ; run;
This dataset has one character variable that contains leading zeros. There are two methods to remove these leading zeros in SAS. The easiest way is to convert this column into a numeric column is by multiplying each value by 1. However, the values that contain characters (“01AB” and “00YXZ”) are set to a missing value.
So, if you have a character variable with leading zeros that contains numbers and characters, then you need another solution. You could use the compress function to remove leading zeros if you are 100% sure that your string only contains leading zeros. If you use the compress function and your string contains zeros somewhere in the middle of the string, then these zeros will be removed too.
A more robust method to only remove leading zeros is with a pattern-matching replacement. In SAS, the PRXCHANGE function does this for you.
data work.remove_leading_zeros; set work.ds_leading_zeros; code_num = 1 * code; code_char_compress = compress(code, "0"); code_char_regex = prxchange('s/^0+//o',-1,code); run;
Do you know? 5 Easy Ways to Remove Leading Zeros
The table below provides a summary of how to add and remove leading zeros in SAS. The x represents the name of your variable, and the w the length of the new variable when adding leading zeros.
|Add Leading Zeros||Numeric||Numbers||PUT(x, Zw.)|
|Add Leading Zeros||Character||Characters (+ Numbers)||CAT(REPEAT(‘0’, w-LENGTH(x)-1),x)|
|Add Leading Zeros||Character||Numbers||PUT(INPUT(x, best12.), Zw.)|
|Remove Leading Zeros||Character||Numbers||x * 1|
|Remove Leading Zeros||Character||Characters (+ Numbers)||PRXCHANGE(‘s/^0+//o’,-1,x)|