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

Hi all,

I guess the title is not informative. I have a list of words that I want to remove some strings from some character variables.

assume that I have the following data:

Header 1
MARK SMITH MARCH
JOHN END BROWN
BILL VICE GREEN MARCH

I want to remove the words (MARCH, END, and VICE) from the characters.

Normally, I shoud use a tranwrd function:

var2 = tranwrd(var1, 'MARCH',' ' );

var3 = tranwrd(var2, 'END',' ');

var4 = tranwrd(var3,'VICE',' ');

but, I feel that it is not an efficient way. Specially because the number of words that I want to remove are huge.

So, I am thinking of first defining some list:

list = ['MARCH', 'END', 'VICE']

and then some how write a loop in the list.

Can some one help me if there is an efficient way to do that?

thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Here is one possibility:

data have;

  informat header1 $80.;

  input Header1 &;

  cards;

BILL VICE GREEN MARCH

JOHN END BROWN

MARK SMITH MARCH

JOHN DOE

;

data want (drop=_:);

  set have;

  _patternID = prxparse('/VICE|MARCH|END/');

  do until (_position eq 0);

    call prxsubstr(_patternID, header1, _position, _length);

    header1=catt(substr(header1,1,_position-1),

                 substr(header1,_position+_length));

  end;

run;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

Here is one possibility:

data have;

  informat header1 $80.;

  input Header1 &;

  cards;

BILL VICE GREEN MARCH

JOHN END BROWN

MARK SMITH MARCH

JOHN DOE

;

data want (drop=_:);

  set have;

  _patternID = prxparse('/VICE|MARCH|END/');

  do until (_position eq 0);

    call prxsubstr(_patternID, header1, _position, _length);

    header1=catt(substr(header1,1,_position-1),

                 substr(header1,_position+_length));

  end;

run;

Jagadishkatam
Amethyst | Level 16

Alternatively, please try prxchange function

data have;

  input text &$100.;

  text=prxchange('s/MARCH|END|VICE//i',-1,text);

  cards;

MARK SMITH MARCH

JOHN END BROWN

BILL VICE GREEN MARCH

;

Thanks,

Jag

Thanks,
Jag
Reeza
Super User

Here's a way using temporary arrays, that loads the words in from a separate dataset.

Probably not as fast as PRX but easier to debug Smiley Happy

data have;

  informat header1 $80.;

  input Header1 &;

  cards;

BILL VICE GREEN MARCH

JOHN END BROWN

MARK SMITH MARCH

JOHN DOE

;

data word_search;

informat words_find $8.;

input words_find;

cards;

MARCH

END

VICE

;

run;

data want;

*load word list into temporary array;

array  words(3) $ _temporary_ ;

if _n_=1 then do i=1 to 3;

  set word_search;

  words(i)=words_find;

end;

*search for words;

set have;

var=header1;

do i=1 to dim(words);

  var=compbl(tranwrd(var, compress(words(i)), ' '));

end;

run;

stat_sas
Ammonite | Level 13

data want(drop=list);

set have;

Header1_=Header1;

length list $30;

do list = 'MARCH', 'END', 'VICE';

  Header1_ = tranwrd(strip(Header1_),strip(list),'');

end;

run;

Shayan2012
Quartz | Level 8

Thanks a lot guys,

I got really awesome ideas.

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
  • 5 replies
  • 1007 views
  • 10 likes
  • 5 in conversation