Contributor
Posts: 27

# DO loops and Array processing

Hi,

I have values for the following variables that are either 0 or 1 :
q1a_1 - q1a_14
q1b_1 - q1b_14
q1c_1 - q1c_14
q1d_1 - q1d_14

There are a few new variables I want to create:

q1before_1 - q1before_14: for each iteration 1 - 14, this variable will check if q1a_{i} OR q1c_{i} contains a 1 (or sum of GE 1), if GE 1 then q1before_{i} = 1 .

q1after_1 - q1after_14: for each iteration 1 - 14, this variable will check if q1b_{i} OR q1d_{i} contains a 1 (or sum of GE 1), if GE 1 then q1after_{i} = 1 .

I can write out each iteration, but I'm looking for a solution (if possible) that will condense my code with DO loops and arrays (or other solutions).

Super Contributor
Posts: 366

## Re: DO loops and Array processing

Hello KDang,

Is this the code are you looking for?
[pre]
data a;
array q1a {14} q1a_1-q1a_14;
array q1b {14} q1b_1-q1b_14;
array q1c {14} q1c_1-q1c_14;
array q1d {14} q1d_1-q1d_14;
array q1before {14} q1before_1-q1before_14;
array q1after {14} q1after_1- q1after_14;
/* Array Initialization */;
do i=1 to 14;
q1a[i]=RAND('BERNOULLI',0.1);
q1b[i]=RAND('BERNOULLI',0.2);
q1c[i]=RAND('BERNOULLI',0.3);
q1d[i]=RAND('BERNOULLI',0.4);
end;
/* Calculation */;
do i=1 to 14;
if q1a[i]+q1c[i] GE 1 then q1before[i]=1;
if q1b[i]+q1d[i] GE 1 then q1after[i]=1;
end;
drop i;
run;
[/pre]
Sincerely,
SPR
Contributor
Posts: 27

## Re: DO loops and Array processing

Yes thank you SPR, it worked perfectly.

Although, I didnt include the array initialization step, just because I dont really understand it..
i'm just curious what it does?
Super Contributor
Posts: 366

## Re: DO loops and Array processing

Hello KDang,

The Initialization step has been included to create initial data for your input arrays a1q, etc. It is not necessary in your program.

Sincerely,
SPR
Valued Guide
Posts: 2,191

## Re: DO loops and Array processing

> q1before_1 - q1before_14: for each iteration 1 - 14,
> this variable will check if q1a_{i} OR q1c_{i}
> contains a 1 (or sum of GE 1), if GE 1 then
> q1before_{i} = 1 .

BEFORE = (ASET or CSET);

> q1after_1 - q1after_14: for each iteration 1 - 14,
> this variable will check if q1b_{i} OR q1d_{i}
> contains a 1 (or sum of GE 1), if GE 1 then
> q1after_{i} = 1 .

AFTER = ( BSET or DSET);

the SAS code could be as simple if you use the (less-well-documented) array style called IMPLICIT arrays[pre]
array aset q1a_1 - q1a_14 ;
array bset q1b_1 - q1b_14 ;
array cset q1c_1 - q1c_14 ;
array dset q1d_1 - q1d_14 ;
array before q1before_1 - q1before_14 ;
array after q1after_1 - q1after_14 ;

do over before ;
AFTER = (BSET or DSET);
BEFORE = (ASET or CSET);
end ;[/pre]
For those who prefer nothing remotely undocumented or like to see more syntax, I reccommend the exercise of providing the demonstration..
;-)
peterC
Contributor
Posts: 27

## Re: DO loops and Array processing

Thanks PeterC that worked perfectly as well, really simple and easy to understand.
Discussion stats
• 5 replies
• 195 views
• 0 likes
• 3 in conversation