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.);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.