BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10

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

then do; ad=1;

leave;

end;

end;

run; 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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; 

View solution in original post

5 REPLIES 5
lillymaginta
Lapis Lazuli | Level 10

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 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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; 
lillymaginta
Lapis Lazuli | Level 10

Thank you! 

Reeza
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 810 views
  • 1 like
  • 4 in conversation