## output from array do loop

Solved
Regular Contributor
Posts: 182

# output from array do loop

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!

Accepted Solutions
Solution
‎11-19-2015 05:10 PM
Super User
Posts: 13,583

## Re: output from array do loop

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.

All Replies
Solution
‎11-19-2015 05:10 PM
Super User
Posts: 13,583

## Re: output from array do loop

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.

Regular Contributor
Posts: 182

## Re: output from array do loop

@ballardw Thank you!   Just needed to output the observation

Super User
Posts: 6,785

## Re: output from array do loop

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.

Frequent Contributor
Posts: 102

## Re: output from array do loop

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;

Super Contributor
Posts: 275

## Re: output from array do loop

[ Edited ]

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; ``````
🔒 This topic is solved and locked.