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

I have a variable called 'Contact Name'. In this variable there are staff names and ID.

Contact name will have values of staff members name (both first name and surname, but sometimes just first name and sometimes no name)  and ID, 3 character and 3 numeric eg 

John Smith ABC123

Peter DEF345

Harry (GHI678)

Harry GHI678

 

What I'd like to do, is that when the ID does not have brackets around them, I'd like to have brackets around them.

 

Current State: Harry GHI678

Desired outcome: Harry (GHI678)

 

Cheers

Haydn

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try the perl regular expression as below

 

data have;
input name&$50.;
cards;
John Smith ABC123
Peter DEF345
Harry (GHI678)
Harry GHI678
;

data want;
set have;
new=prxchange('s/(.*)(\w{3}\d{3})$/$1($2)/oi',-1,strip(name));
run;
Thanks,
Jag

View solution in original post

1 REPLY 1
Jagadishkatam
Amethyst | Level 16

Please try the perl regular expression as below

 

data have;
input name&$50.;
cards;
John Smith ABC123
Peter DEF345
Harry (GHI678)
Harry GHI678
;

data want;
set have;
new=prxchange('s/(.*)(\w{3}\d{3})$/$1($2)/oi',-1,strip(name));
run;
Thanks,
Jag

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1 reply
  • 847 views
  • 1 like
  • 2 in conversation