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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 648 views
  • 2 likes
  • 4 in conversation