Sunday, April 2, 2017

Adding Leading Zeroes (SAS)


It happens to all of us, you were importing a .CSV file into SAS, and for whatever reason, somewhere in the process, your leading zeroes disappeared.

For example: 000000334 became 334.

This problem isn’t difficult to resolve, just run the following lines of code.

Data NEWSET (drop = VARA);

/* Creates a new data set and drops the original variable that needed to be corrected. The original variable is dropped after the data set is finished compiling */

Set OLDSET;

/* The name of the old data set containing the original variable */

VARB=put(input(VARA,best32.),z9.);

/* VARB is the name of the new variable, VARA is the name of the old variable that needs to be corrected. First, the data is input and assigned the best32. format. The z9. format then re-formats the variable to include leading zeroes.

Use Z(# total length of number). to specify how long you would like the number to be. So, for example, if the total maximum length of a number within the VARA variable column needed to be 7 characters long, you would change Z9 to Z7.

For 334 to become 0000334 use z7. For 334 to become 00000000334 use z11.

Finally, the PUT statement tells SAS to change the entire variable format to characters, thus, holding the leading zeroes */

Run;

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.