## Do Loop with multiple values

Solved
Regular Contributor
Posts: 182

# Do Loop with multiple values

Hi!  I'd like to know if there is a way to do an array / do loop checking for multiple values.

I have this code, but I'd like to check is any DX1-DX10 start with  these values:  7707, 748, 505

data OP_Exlusion5;

set asthma.OPssn_JoinFY14nd;

array dx(*) DX1-DX10;

do i= 1 to dim(dx);

if dx(i)=: '7707' then do;

output;

end;

end;

run;

When I include all the values in the if statement, I am getting an error.  Do I need multiple IF statements?

Thanks!

Accepted Solutions
Solution
‎12-07-2015 12:45 PM
Super Contributor
Posts: 275

## Re: Do Loop with multiple values

If I understand your question correctly, you could try this:

``````data OP_Exlusion5;
set asthma.OPssn_JoinFY14nd;
array dx(*) DX1-DX10;
do i= 1 to dim(dx);
if dx(i)=: '7707' or dx(i)=: '748' or dx(i)=: '505' then do;
output;
return;
end;
end;
run;``````

All Replies
Contributor
Posts: 61

## Re: Do Loop with multiple values

What error do you get? Even if there was no error it will create multiple records if more than one DX variables had one of the values you listed. To avoid this situation create a flag in the loop and then use the flag to output subset of the data.

Solution
‎12-07-2015 12:45 PM
Super Contributor
Posts: 275

## Re: Do Loop with multiple values

If I understand your question correctly, you could try this:

``````data OP_Exlusion5;
set asthma.OPssn_JoinFY14nd;
array dx(*) DX1-DX10;
do i= 1 to dim(dx);
if dx(i)=: '7707' or dx(i)=: '748' or dx(i)=: '505' then do;
output;
return;
end;
end;
run;``````
Super User
Posts: 6,768

## Re: Do Loop with multiple values

[ Edited ]

Also note, the IN comparison also knows how to use a colon, and understands that arguments may be of different lengths:

if dx{i} in : ('7707', '748', '505') then do;

🔒 This topic is solved and locked.