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

Hi Experts,

 

I would like to replace the 'UN' and 'UNK' two strings with '--' and '---' with prxchange. Could you please help me with the code. 

 

I tried 

 

new=prxchange('s/(\bUN\b)|(\bUNK\b)/---/',-1,date);

 

data have;
input date &$100.;
cards;
UN UNK 2018
;
Thanks,
Jag
1 ACCEPTED SOLUTION

Accepted Solutions
Shemp
Obsidian | Level 7

Like this...

 

data have;
input date &$100.;
length new $ 100;
/*new = prxchange('s/\bUNK\b/---/', -1, date);*/
/*new = prxchange('s/\bUN\b/--/', -1, new);*/
new = prxchange('s/\bUN\b/--/', -1, prxchange('s/\bUNK\b/---/', -1, date));
cards;
UN UNK 2018
;

View solution in original post

7 REPLIES 7
Shemp
Obsidian | Level 7

data have;
input date &$100.;
length new $ 100;
new = prxchange('s/\bUNK\b/---/', -1, date);
new = prxchange('s/\bUN\b/--/', -1, new);
cards;
UN UNK 2018
;

Jagadishkatam
Amethyst | Level 16

Thanks for your response.

 

Isn't possible to do it in a single line in the same prxchange rather than in two separate lines.

Thanks,
Jag
Shemp
Obsidian | Level 7
I don't believe it is possible to do in a single line unless you embed a prxchange function as the 3rd argument to a prxchange function.
Shemp
Obsidian | Level 7

Like this...

 

data have;
input date &$100.;
length new $ 100;
/*new = prxchange('s/\bUNK\b/---/', -1, date);*/
/*new = prxchange('s/\bUN\b/--/', -1, new);*/
new = prxchange('s/\bUN\b/--/', -1, prxchange('s/\bUNK\b/---/', -1, date));
cards;
UN UNK 2018
;

Jagadishkatam
Amethyst | Level 16

I was working on the prxchange and thought we could simplify the code when compared to accepted solution.  Instead of using two prxchange we can do it with one prxchange as below

 

data have;
input date &$100.;
date2=prxchange('s/(UN\s)(UNK\s)(\d*)/-- --- $3/i',-1,strip(date));
cards;
UN UNK 2018
;
Thanks,
Jag
Shemp
Obsidian | Level 7

data have;
input date &$100.;
date2=prxchange('s/(UN\s)(UNK\s)(\d*)/-- --- $3/i',-1,strip(date));
cards;
UN UNK 2018
UN 2018
UNK 2018
UN
UNK
;

 

In the above, the first line in the CARDS statement works, but the next 4 fail.

ChrisNZ
Tourmaline | Level 20

>Isn't possible to do it in a single line in the same prxchange rather than in two separate lines.

It is possible if the replacement string is not changing, for example always ---.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 3246 views
  • 2 likes
  • 3 in conversation