The easiest way to remove leading zeros in a character variable in SAS is to use the INPUT function to convert the variable to a numeric variable, which automatically removes leading zeros.
This function uses the following basic syntax:
data new_data; set original_data; no_zeros = input(some_column, comma9.); run;
The following example shows how to use this syntax in practice.
Example: Remove Leading Zeros in SAS
Suppose we have the following dataset in SAS that shows the total sales made by various retail stores:
/*create dataset*/ data original_data; input store $ sales $; datalines; A 055 B 145 C 199 D 0000443 E 0093 F 00004302 G 38 H 0055 ; run; /*view dataset*/ proc print data=original_data;
We can use the following code to remove all leading zeros from values in the sales column:
/*remove leading zeros in sales column*/ data new_data; set original_data; no_zeros = input(sales, comma9.); run; /*view results*/ proc print data=new_data;
Notice that all leading zeros have been removed from the values in the no_zeros column.
Note that the new no_zeros column is a numeric column.
If you would instead like to keep it as a character column, you can wrap the PUT function around the INPUT function as follows:
/*remove leading zeros in sales column*/ data new_data; set original_data; no_zeros = put(input(sales, comma9.), 8.); run; /*view results*/ proc print data=new_data;
If we use use proc contents to view the data type of each variable in the dataset, we’ll see that no_zeros is a character variable:
/*view data type of each variable in new dataset*/ proc contents data=new_data;
The following tutorials explain how to perform other common tasks in SAS:
How to Normalize Data in SAS
How to Identify Outliers in SAS
How to Use Proc Summary in SAS
How to Create Frequency Tables in SAS