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

## Create new variable when Variable is between 2 numbers

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—25992619—28992921—2999 Australian Capital Territory ACT 0200—0299 (LVRs and PO Boxes only)2600—26182900—2920 Victoria VIC 3000—39998000—8999 (LVRs and PO Boxes only) Queensland QLD 4000—49999000—9999 (LVRs and PO Boxes only) South Australia SA 5000—57995800—5999 (LVRs and PO Boxes only) Western Australia WA 6000—67976800—6999 (LVRs and PO Boxes only) Tasmania TAS 7000—77997800—7999 (LVRs and PO Boxes only) Northern Territory NT 0800—08990900—0999 (LVRs and PO Boxes only
1 ACCEPTED SOLUTION

Accepted Solutions
Obsidian | Level 7

## Re: Create new variable when Variable is between 2 numbers

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";
2 REPLIES 2
Obsidian | Level 7

## Re: Create new variable when Variable is between 2 numbers

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

## Re: Create new variable when Variable is between 2 numbers

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.

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