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