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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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