BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

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 WalesNSW1000—1999 (LVRs and PO Boxes only)
Australian Capital TerritoryACT0200—0299 (LVRs and PO Boxes only)
8000—8999 (LVRs and PO Boxes only)
9000—9999 (LVRs and PO Boxes only)
South AustraliaSA5000—5799
5800—5999 (LVRs and PO Boxes only)
Western AustraliaWA6000—6797
6800—6999 (LVRs and PO Boxes only)
7800—7999 (LVRs and PO Boxes only)
Northern TerritoryNT0800—0899
0900—0999 (LVRs and PO Boxes only

Accepted Solutions
Obsidian | Level 7
I have used the below. Don't know why I didn't think of this before posting. Sorry if I wasted anyone's time.

if P_Code >=0800 and P_Code <=0999 then State"NT";

View solution in original post

Obsidian | Level 7
I have used the below. Don't know why I didn't think of this before posting. Sorry if I wasted anyone's time.

if P_Code >=0800 and P_Code <=0999 then State"NT";
Super User

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
'2921'-'2999' = 'NSW'
'2900'-'2920' = 'ACT'

'8000'-'8999' = 'VIC'

'9000'-'9999' = 'QLD'

'5800'-'5999' = 'SA'

'6800'-'6999' = 'WA'

'7800'-'7999' = 'TAS'

'0900'-'0999' = 'NT'

And how to use to create new variable.

data want;
   set have;
   State = put(p_code,$code2state.);

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 open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

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.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1 like
  • 2 in conversation