## multiple sustr statements

Solved
Frequent Contributor
Posts: 136

# multiple sustr statements

Hi,

I have multiple subst statements and I want to run them all at once. Can some one help on the best way to combine all these statements in once (I have 30+ statements)

data a (drop=i); set b; array {x} dx:; ar=1=0;

do i=1 to dim(x); if substrn(x{i}, 1, 5) in ('12345', '23456', '30987')

then do; ar=1;

leave;

end;

end;

run;

data a (drop=i); set b; array {x} dx:; arf=1=0;

do i=1 to dim(x); if substrn(x{i}, 1, 4) in ('1234', '2432', '3987')

then do; arf=1;

leave;

end;

end;

run;

data a (drop=i); set b; array {x} dx:; ad=1=0;

do i=1 to dim(x); if substrn(x{i}, 1, 2) in ('12', '24', '39')

leave;

end;

end;

run;

Accepted Solutions
Solution
‎10-02-2017 11:06 AM
Super User
Posts: 9,397

## Re: multiple sustr statements

Your code is very hard to read.  What are you trying to do, if its just finding those strings, and setting a flag why not:

```data a (drop=i);
set b;
array a dx:;
ar=0;
arf=0;
do i=1 to dim(x);
ar=ifn(substrn(x{i}, 1, 5) in ('12345', '23456', '30987'),1,ar);
arf=ifn(substrn(x{i},1,4) in ('1234','2432','3987'),1,arf);
end;
run; ```

All Replies
Super User
Posts: 9,868

## Re: multiple sustr statements

Since the purpose seems to be to just create the flag variables, you can do all the if's in one do loop and omit the leave statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 136

## Re: multiple sustr statements

Thank you for the reply, I am getting an error (one unclosed loop block), would you mind to show me how would you combine them? Thanks

Solution
‎10-02-2017 11:06 AM
Super User
Posts: 9,397

## Re: multiple sustr statements

Your code is very hard to read.  What are you trying to do, if its just finding those strings, and setting a flag why not:

```data a (drop=i);
set b;
array a dx:;
ar=0;
arf=0;
do i=1 to dim(x);
ar=ifn(substrn(x{i}, 1, 5) in ('12345', '23456', '30987'),1,ar);
arf=ifn(substrn(x{i},1,4) in ('1234','2432','3987'),1,arf);
end;
run; ```
Frequent Contributor
Posts: 136

Thank you!

Super User
Posts: 23,237

## Re: multiple sustr statements

If you're just checking for the start of the string to match consider using the : operator.

``if x(i)  in: ('12345', '23456');``

If the length of your X variable can be smaller than the string this likely won't work though.

☑ This topic is solved.