Quartz | Level 8

## How to read some varibales from a list in sas?

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:

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
Opal | Level 21

## Re: How to read some varibales from a list in sas?

Here is one possibility:

data have;

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);

end;

run;

5 REPLIES 5
Opal | Level 21

## Re: How to read some varibales from a list in sas?

Here is one possibility:

data have;

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);

end;

run;

Amethyst | Level 16

## Re: How to read some varibales from a list in sas?

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
Super User

## Re: How to read some varibales from a list in sas?

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

data have;

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;

do i=1 to dim(words);

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

end;

run;

Ammonite | Level 13

## Re: How to read some varibales from a list in sas?

data want(drop=list);

set have;

length list \$30;

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

end;

run;

Quartz | Level 8

## Re: How to read some varibales from a list in sas?

Thanks a lot guys,

I got really awesome ideas.

Discussion stats
• 5 replies
• 1007 views
• 10 likes
• 5 in conversation