SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 7 replies
  • 3966 views
  • 2 likes
  • 3 in conversation