18.7 C
London
Thursday, July 24, 2025
HomeSASData Munging in SASHow to Remove Leading Zeros in SAS (With Examples)

How to Remove Leading Zeros in SAS (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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;

SAS remove leading zeros

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;

SAS remove leading zeros

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;

Additional Resources

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories