BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nandeep
Fluorite | Level 6

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?

 

Thanks in advance

 

Nandeep

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Nandeep
Fluorite | Level 6

@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

 

Nandeep
Fluorite | Level 6

@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

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Nandeep
Fluorite | Level 6

@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

 

 

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
  • 5 replies
  • 2296 views
  • 0 likes
  • 2 in conversation