Adding leading zeros is one of the most frequently encountered data problem. Sometimes it is essential to add leading zeros in SAS while doing data cleaning. This makes data looks much cleaner, easy to read and consistent. In this tutorial, I will show you how easy it is to add 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, all you have to do is to add a z format to the variables. The Z format writes a 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.
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.
Here, is the example dataset, I have used. The column zip should be 5 digits and the value column doesn’t look consistent as well.
data customer; set ds.customer; format zip z5. value z5.2; run; proc print;
format zip z5. tells SAS to add 5 number of leading zeros to the variable zip to maintain 5 as a length of the zip variable. In the first observation, it has not added any zeros as the number of digits is already 4. However, it added 1 leading zeros in second observation as it has 4 values.
format value z5.2 tell SAS to keep the total number of digits to 5 including the decimal point and .2 is to add 2 decimal values to the number.
Add leading zeros to the Character Variable
To add leading zeros to character variable you can use the combination of REPEAT and CATS function.
For the purpose of this example, I have converted the existing customerid numeric variable to a character.
data test; set ds.customer; customerid2=put(customerid, 10. -L); customerid2=cats(repeat('0', 16-length(customerid2)-1), customerid2); drop customerid; rename customerid2=customerid; run; proc print;
Here, I have added leading 0s to make the total digits 16. CATS function is used to concatenate 0s with the variable customerid. 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
Sometimes your data may already 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); run;
Character to Numeric conversion, keeping the leading zeros
As you know Input function is used to convert character to numeric and it requires an informat so z format will not work here. All you have to do is read the give any informat or best format then associate the format z to your variable.
data cust1; set cust; num_zip=input(char_zip,best.); format num_zip z5.; run;
SAS Essentials introduces a step-by-step approach to mastering SAS software for statistical data analysis. It’s also a valuable reference tool for any researcher currently using SAS.
Designed for those new to SAS and filled with illustrative examples, the book shows how to read, write and import data; prepare data for analysis; use SAS procedures; evaluate quantitative data; analyze counts and crosstabulation tables, and compare means using the t-test.
The book also provides instruction and examples on analysis of variance, correlation and regression, nonparametric analysis, logistic regression, creating graphs, controlling outputs using ODS, as well as advanced topics in SAS programming.
From John Wiley & Sons. Author – Alan C. Elliott