22.9 C
London
Monday, July 21, 2025
HomeSASData Munging in SASSAS: Convert Numeric to Character with Leading Zeros

SAS: Convert Numeric to Character with Leading Zeros

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

You can use the following basic syntax to convert a numeric variable to a character variable with a specific amount of leading zeros in SAS:

data new_data;
    set original_data;
    employee_ID = put(employee_ID, z10.);
    format employee_ID z10.;
run;

This particular example converts the numeric variable called employee_ID into a character variable with enough leading zeros to make employee_ID have a length equal to 10.

The following example shows how to use this syntax in practice.

Example: Convert Numeric to Character with Leading Zeros in SAS

Suppose we have the following dataset in SAS that shows the total sales made by various employees at some company:

/*create dataset*/
data original_data;
    input employee_ID sales;
    datalines;
4456 12
4330 18
2488 19
2504 11
2609 33
2614 30
2775 23
2849 14
;

/*view dataset*/
proc print data=original_data;

Now suppose we would like to convert the variable called employee_ID to a character variable with enough leading zeros to make each value in the column have a length of 10.

We can use the following syntax to do so:

/*create new dataset with employee_ID as character with leading zeros*/
data new_data;
    set original_data;
    employee_ID = put(employee_ID, z10.);
    format employee_ID z10.;
run;

/*view new dataset*/
proc print data=new_data;

We can see that the employee_ID variable in the new dataset contains enough leading zeros to make each of the values have a length of 10.

To add a different number of leading zeros, simply change z10 to a different value.

For example, we could use z15 to add enough leading zeros to make each of the values in the employee_ID column have a length of 15:

/*create new dataset with employee_ID as character with leading zeros*/
data new_data;
    set original_data;
    employee_ID = put(employee_ID, z15.);
    format employee_ID z15.;
run;

/*view new dataset*/
proc print data=new_data;

Each of the values in the employee_ID column now have a length of 15.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

SAS: How to Rename Variables
SAS: How to Convert Numeric Variable to Date
SAS: How to Convert Character Variable to Numeric

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