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

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
slchen
Lapis Lazuli | Level 10

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;

View solution in original post

3 REPLIES 3
ndp
Quartz | Level 8 ndp
Quartz | Level 8

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.

slchen
Lapis Lazuli | Level 10

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;
Astounding
PROC Star

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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1847 views
  • 2 likes
  • 4 in conversation