BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Haydo
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)
2000—2599
2619—2899
2921—2999
Australian Capital TerritoryACT0200—0299 (LVRs and PO Boxes only)
2600—2618
2900—2920
VictoriaVIC3000—3999
8000—8999 (LVRs and PO Boxes only)
QueenslandQLD4000—4999
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)
TasmaniaTAS7000—7799
7800—7999 (LVRs and PO Boxes only)
Northern TerritoryNT0800—0899
0900—0999 (LVRs and PO Boxes only
1 ACCEPTED SOLUTION

Accepted Solutions
Haydo
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

2 REPLIES 2
Haydo
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";
ballardw
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
'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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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