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!
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;
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.
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;
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;
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.
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.