BookmarkSubscribeRSS Feed
imdickson
Quartz | Level 8

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?

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16

Could you please provide sample data and expected output to get the answer

Thanks,
Jag
Ksharp
Super User
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;
imdickson
Quartz | Level 8

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?

 

Ksharp
Super User

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;
imdickson
Quartz | Level 8

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?

 

 

Ksharp
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 6 replies
  • 1193 views
  • 1 like
  • 3 in conversation