BookmarkSubscribeRSS Feed
KDang
Fluorite | Level 6
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).

Thanks for your help!
5 REPLIES 5
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
KDang
Fluorite | Level 6
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?
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
Peter_C
Rhodochrosite | Level 12
> 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
KDang
Fluorite | Level 6
Thanks PeterC that worked perfectly as well, really simple and easy to understand.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1645 views
  • 2 likes
  • 3 in conversation