Have have a variable called P_Code which has postcodes for customers.
So something like.
if P_Code between 1000-1999 then State="NSW"
if P_Code between 2000-2599 then State="NSW"
Below is a table of Australian postcodes and their States.
Any help is appreciated.
State/Territory Abbreviation Postcode range
New South Wales | NSW | 1000—1999 (LVRs and PO Boxes only) 2000—2599 2619—2899 2921—2999 |
Australian Capital Territory | ACT | 0200—0299 (LVRs and PO Boxes only) 2600—2618 2900—2920 |
Victoria | VIC | 3000—3999 8000—8999 (LVRs and PO Boxes only) |
Queensland | QLD | 4000—4999 9000—9999 (LVRs and PO Boxes only) |
South Australia | SA | 5000—5799 5800—5999 (LVRs and PO Boxes only) |
Western Australia | WA | 6000—6797 6800—6999 (LVRs and PO Boxes only) |
Tasmania | TAS | 7000—7799 7800—7999 (LVRs and PO Boxes only) |
Northern Territory | NT | 0800—0899 0900—0999 (LVRs and PO Boxes only |
Is P_code a numeric or character variable?
This is the sort of thing that a Format works well for as formats take the logic out of the data stream and can be reused easily.
This assumes your value is character otherwise leading zero doesn't mean much. If your P_code is numeric then remove the $ from the name of the format: Code2State, and remove all of the quotes in value lists. Typically "between" can be problematic with character values but with all of these 4 characters there shouldn't be any problem. Note that you could also include a keyword Other to set a value if you get an unexpected value of P-code. That would be a simple Other = 'Not valid code' or similar.
proc format; value $code2state '1000'-'1999', '2000'-'2599', '2619'-'2899', '2921'-'2999' = 'NSW' '0200'-'0299', '2600'-'2618', '2900'-'2920' = 'ACT' '3000'-'3999', '8000'-'8999' = 'VIC' '4000'-'4999', '9000'-'9999' = 'QLD' '5000'-'5799', '5800'-'5999' = 'SA' '6000'-'6797', '6800'-'6999' = 'WA' '7000'-'7799', '7800'-'7999' = 'TAS' '0800'-'0899', '0900'-'0999' = 'NT' ; run;
And how to use to create new variable.
data want; set have; State = put(p_code,$code2state.); run;
Actually you could just use the format with P_code in procs to display the state abbreviation.
You could also make another format with the full name of the state for the times that my be useful.
I use our US Zip postal code to look up missing city names, county names and can create custom reporting areas such as "north side of city X" and "south side of city X" but does require having some information about the relationships.
Formats are a very powerful tool in SAS. You don't have to create new variables to change the text of reports or groups of an analysis procedure. The groups created by formats are used in report or analysis procedures and generally for graphing purposes.
Read the documentation for limits. The format must be available in a session. So either re-execute the code in each session or create a format catalog in a permanent library and point to that location with the FMTSEARCH option.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.