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
;
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
;
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
;
Thanks for your response.
Isn't possible to do it in a single line in the same prxchange rather than in two separate lines.
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
;
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
;
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.
>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 ---.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.