This was very helpful, but my final concern is handling ZIP code information where there are leading zeros. How would you modify the code to ensure that the leading zeros are in place?
Store it as character from the beginning is the best bet. If you are reading data from a text file make sure to read the value as character. In particular do not let PROC IMPORT or Excel guess how to define the variable, because then it will become numeric. Which will not work once you have ZIP+4 values or values of postal codes from other countries.
If you have a numeric variable then the leading zeros do not matter. 100, 0100 and 00100 are all the same number.
If you want to create a character value from a numeric value use the PUT() function.
zipcode = put(zip_number,z5.);
If you have a character value and some are missing the leading zero then a quick method is to convert the string to a number and then use PUT().
zipcode = put(input(strip(zipcode),32.),z5.);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.