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

I have some dataset like this:

KAWASAN PERINDTRIAN MASJID TANAH,78300 MASJID TANAH, MELAKA,MALAYSIA

MUKIM SERKAM 77300 MERLIMAU, MELAKA

PAYA MENGKUANG 78300 MASJID TANAH MELAKA

 

I want to remove the 5 digit postcode and string after postcode.

The output that I want is like this:

KAWASAN PERINDTRIAN MASJID TANAH

MUKIM SERKAM

PAYA MENGKUANG

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

Is it 100% that only one five-digit-number is in the data? If yes, then try:

data want;
   set have;
   length new $ 100;

   new = prxchange('s/(.*)\W+\d{5}.*/$1/', 1, string);
run;

View solution in original post

3 REPLIES 3
andreas_lds
Jade | Level 19

Is it 100% that only one five-digit-number is in the data? If yes, then try:

data want;
   set have;
   length new $ 100;

   new = prxchange('s/(.*)\W+\d{5}.*/$1/', 1, string);
run;
Shmuel
Garnet | Level 18

Alternative code:

newvar = substr(string,1,indexc(string,'0123456789')-1);

This code searches for the first digit in string, even if it is not a postcode of 5 digits.

Ksharp
Super User
data have;
input x $80.;
cards;
KAWASAN PERINDTRIAN MASJID TANAH,78300 MASJID TANAH, MELAKA,MALAYSIA
MUKIM SERKAM 77300 MERLIMAU, MELAKA
PAYA MENGKUANG 78300 MASJID TANAH MELAKA
;
data want;
 set have;
 want=substr(x,1,prxmatch('/\d{5}/',x)-1);
 run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1043 views
  • 2 likes
  • 4 in conversation