You probably have many SAS data sets that you regularly work with where numbers are stored as text for formatting purposes. A great example is the social security number, and many other customer, product, or invoice numbers are formatted similarly. They frequently utilize leading zeros in their storage and display. You probably also frequently receive data from colleagues where the same variable is formatted as a numeric value missing the leading zeros. The join that you were going to do with your data and the data received from the colleague just won’t work. Here is a little SAS code that can turn your colleague’s data into a match for yours, and I’ll use social security number (SSN) in this example:
|data DatasetName ;|
|set InputDataset ;|
|length ssn_char $9.||/* New Character var for SSN */|
|ssn_char = put(ssn,z9.) ;||/* Put the Numeric SSN with the Z9. format */|
|drop ssn ;||/* Housecleaning to drop the numeric SSN */|
|rename ssn_char = ssn ;||/* Renamed Char SSN to make our Join easier */|
By creating a new Character variable of SSN, I can simply put the Numeric SSN with Z9. format into the variable.
The format Z9. states the variable should be 9-bytes, and for each byte less than 9, put a leading zero in front of the var.
No strip or trim is necessary either.
I used this in the past with a transaction number I was creating (numeric) and later wanted to display it in a title with leading zeros [Example: had 123 as the value, put it with a Z10. and the display in the title was 0000000123].