Hi! I am trying to figure out the proper way to complete an array do loop. I have 10 variables within a larger table that start with DX (DX1, DX2, DX3...to DX10). I need to know if any values that start with '493' are in the DX5-DX10 variable range. If yes, then output the observation.
Here is what I have so far (but it may all be wrong!). I can't figure out how to output the observation...
data test_dxcheck;
set have;
array dx(*) DX5-DX10;
do i= 5-10;
if dx(i)=: '493' then do;
????
Thanks!
You haven't described exactly what you want to do when found. Output a value, write a message to the log or what.
Also what do you do if more than one of the variables has the behavior? Do the action only once or repeat for each match.
Your basically there though you would get an array out of bounds for i = 6 as written. Suppose you want to write a message to the log:
data test_dxcheck;
set have;
array dx(*) DX5-DX10;
do i= 1 to dim(dx);
if dx(i)=: '493' then do put "Value found at position " I dx[I];
end;
run;
if you want to output the observation only once for the first match
data test_dxcheck;
set have;
array dx(*) DX5-DX10;
do i= 1 to dim(dx);
if dx(i)=: '493' then do ;
output;
leave;
end;
end;
run;
if you want to output the data for each match remove the LEAVE statement.
You haven't described exactly what you want to do when found. Output a value, write a message to the log or what.
Also what do you do if more than one of the variables has the behavior? Do the action only once or repeat for each match.
Your basically there though you would get an array out of bounds for i = 6 as written. Suppose you want to write a message to the log:
data test_dxcheck;
set have;
array dx(*) DX5-DX10;
do i= 1 to dim(dx);
if dx(i)=: '493' then do put "Value found at position " I dx[I];
end;
run;
if you want to output the observation only once for the first match
data test_dxcheck;
set have;
array dx(*) DX5-DX10;
do i= 1 to dim(dx);
if dx(i)=: '493' then do ;
output;
leave;
end;
end;
run;
if you want to output the data for each match remove the LEAVE statement.
@ballardw Thank you! Just needed to output the observation 🙂
With only 6 variables to worry about, it's borderline whether you would want to bother with arrays. You could code it simply:
data want;
set have;
n_493 = (dx5 =: '493') + (dx6 =: '493') + (dx7 =: '493') + (dx8 =: '493') + (dx9 =: '493') + (dx10 =: '493');
if n_493 > 0;
run;
Who knows, you might even find that the variable n_493 is useful later.
data have;
input (dx1 dx2 dx3 dx4 dx5 dx6 dx7 dx8 dx9 dx10)(:$5.);
cards;
22 33 44 55 4830 4931 77 88 99 500
;
proc print;
data test_dxcheck;
set have;
array dx $5. DX1 - DX10;
do i= 5 to 10;
if dx{i}=: '493' then output;
end;
proc print; run;
If I understand you correctly, you could try this:
data test_dxcheck;
set have;
length flag $100;
retain flag;
array dx DX5-DX9;
do i=1 to dim(dx);
if dx(i)=:'493' then flag=catx(' ',flag,vname(dx(i)));
end;
drop i;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Save $200 when you sign up by March 14!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.