Hi, I am planning to replace a list of words with blank. For example, I would like to replace 'ABC' with '' and '(a)' with '' and many more.
However, i couldnt find any answer to it. Is there a special function that allows me to use for replacing a list of possible values with blank?
Could you please provide sample data and expected output to get the answer
data _null_;
x='I am planning to replace a abclist of words with blank. abc';
y=prxchange('s/\b(abc|words|with)\b//i',-1,x);
put x= / y=;
run;
Hi @Ksharp thanks for the code. I have a scenario where it is possible that the variable contains value such as '(a)' without the single quote and maybe '-' without single quote.
I tried to do this:
data _null_;
x='CB 123 (a) 456';
y=prxchange('s/\b(abc|CB |(a))\b//i',-1,x);
put x= / y=;
run;However, it doesnt work as in the open and close bracket wont be eliminated. Further, how do i make sure the code eliminate dash as well?
OK. try this one .
data _null_;
x='CB 123 (a) 456';
y=prxchange('s/\babc\b|\bCB\b|\(a\)//i',-1,x);
put x= / y=;
run;
Hi @Ksharp. i am not good in regular expression. If i plan to add more possible values to be excluded, is this how it should be done?
I want to add these into the regular expression:
1) (a)-<space>
2) #
3) <space>
4) PP
data _null_;
x='CB 123 (a) 456';
y=prxchange('s/\babc\b|\bCB\b|\(a\) |\#\|\bPP|\ \|\(a\)//i',-1,x);
put x= / y=;
run;Instead of using this method, can i also use this?
%let y1=CB ;
%let y2=PP;
%let y3=(a);
%do i=1 to 3
y=tranwrd(x,&&y&i..,'');
%end;It may sound very "beginner" and too MANUAL, but this is the easiest way that i can think of. I have also come across a do loop from other threads but i just couldnt make it work.
The code as below:
data want;
set have;
n_name = o_name;
length word $27 ;
do word='JR', 'SR', 'III', 'IV', 'DECD' ;
n_name = tranwrd(' '||n_name, ' '||strip(word)||' ', ' ');
end;
n_name = compbl(n_name);
drop word ;
run;I replace JR with PP and SR with CB<space>
I assume n_name is my original variables
I think it didnt replace or i hit error, i cannot remember and I am currently out from my workstation. Can i tweak it to cater for my criteria?
Can you give an example ? and you want change a WORD or just a string ?
If you want change a WORD, you need add '\b' as I did before.
data _null_;
x='CB 123 (a) 456 #';
y=prxchange('s/PP|\(a\)|#|\s+//i',-1,x);
put x= / y=;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.