Fluorite | Level 6

## Split a joined up UK post code

Hi,

I'm a fairly newbie to SAS coding so I apologise in advance for any errors.

I have a dataset where one of the variables (poscode) displays the UK postcode. However the post code is joined up i.e  it will display as BD181PZ and I would like it to display BD18 1PZ instead.

I have managed to get the last 3 characters by using the following code:

Post_Code= substr(Postcode,length(Postcode)-2);

Would anyone be able to help with splitting the post code please?

Nandeep

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Split a joined up UK post code

Assumes its is always 3 characters at end:

```data want;
pc='BD181PZ';
want=catx(' ',substr(pc,1,lengthn(pc)-2),substr(pc,lengthn(pc)-2,3));
run;```

You could also check lengthn() if it is 6 then split at char 4, if 7 then split at 7.  Various other methods.

5 REPLIES 5
Diamond | Level 26

## Re: Split a joined up UK post code

Assumes its is always 3 characters at end:

```data want;
pc='BD181PZ';
want=catx(' ',substr(pc,1,lengthn(pc)-2),substr(pc,lengthn(pc)-2,3));
run;```

You could also check lengthn() if it is 6 then split at char 4, if 7 then split at 7.  Various other methods.

Fluorite | Level 6

## Re: Split a joined up UK post code

@RW9 wrote:

Assumes its is always 3 characters at end:

```data want;
pc='BD181PZ';
want=catx(' ',substr(pc,1,lengthn(pc)-2),substr(pc,lengthn(pc)-2,3));
run;```

You could also check lengthn() if it is 6 then split at char 4, if 7 then split at 7.  Various other methods.

Thank you very much I just altered the above code to:

Post_Code= catx(' ',substr(Postcode,1,lengthn(Postcode)-3),substr(Postcode,lengthn(Postcode)-2,3));

and this seemed to have worked as having a -2 in the first set of brackets was bringing back too many characters.

thanks once again

Nandeep

Fluorite | Level 6

## Re: Split a joined up UK post code

@RW9 wrote:

Assumes its is always 3 characters at end:

```data want;
pc='BD181PZ';
want=catx(' ',substr(pc,1,lengthn(pc)-2),substr(pc,lengthn(pc)-2,3));
run;```

You could also check lengthn() if it is 6 then split at char 4, if 7 then split at 7.  Various other methods.

just out of interest what would the syntax be for the lenthn and split method?

Regards

Nandeep

Diamond | Level 26

## Re: Split a joined up UK post code

Something like:

```data want;
set have;
if lengthn(pc)=6 then do;
...
end;
else do;
...
end;
run;```

You may not need the do blocks, thats just for illustration.  To note, its sometimes better to code little bit each time, so break off part one and part two then combine in another step - just makes reading the code much simpler e.g.:

```data want (drop=last first);
set have;
last=substr(pc,lengthn(pc)-2);
first=substr(pc,1,lengthn(pc)-3);
want=catx(' ',first,last);
run;```
Fluorite | Level 6

## Re: Split a joined up UK post code

@RW9 wrote:

Something like:

```data want;
set have;
if lengthn(pc)=6 then do;
...
end;
else do;
...
end;
run;```

You may not need the do blocks, thats just for illustration.  To note, its sometimes better to code little bit each time, so break off part one and part two then combine in another step - just makes reading the code much simpler e.g.:

```data want (drop=last first);
set have;
last=substr(pc,lengthn(pc)-2);
first=substr(pc,1,lengthn(pc)-3);
want=catx(' ',first,last);
run;```

thank you very much for your help and explanation.

Regards

Nandeep

Discussion stats
• 5 replies
• 2064 views
• 0 likes
• 2 in conversation