BookmarkSubscribeRSS Feed
kk13
Calcite | Level 5

I would like to check if the array "a" contains any 1s.  The check1, check2, and "b" array are used to determine the dimension number I need to check in array "a."

 

For id=1, check1=11 and check2=17.  I found that 11<=b{i}<=17 is at i=2 (b2=15).  Hence, I need to check if a1 through a2 contains any 1s.  

 

data z;

input id a1-a4 b1-b4 check1 check2;

datalines;

 

1 1 . . 1 10 15 18 20 11 17

2 . . 1 1 17 18 19 21 16 18

3 . . . 1 18 19 21 22 16 23

;

data want;

input id a1-a4 b1-b4 check1 check2 HAS;

datalines;

 

1 1 . . 1 10 15 18 20 11 17 1

2 . . 1 1 17 18 19 21 16 18 0

3 . . . 1 18 19 21 22 23 23 1

;

data z1;

set z;

array a a1-a4;

array b b1-b4;

do i=1 to dim(a);

if check1<=b{i}<=check2 then do;

 

j=i;

end;

end;

run;

 

3 REPLIES 3
andreas_lds
Jade | Level 19

Sorry, but i don't understand the logic that you want applied: why is has = 0 when id = 2?

Please re-check the data you have posted, the values for check1 and check2 in "want" don't match those in "z". Also please post code using the "insert sas code" button.

PeterClemmensen
Tourmaline | Level 20

Try this

 

data z;
input id a1-a4 b1-b4 check1 check2;
datalines;
1 1 . . 1 10 15 18 20 11 17
2 . . 1 1 17 18 19 21 16 18
3 . . . 1 18 19 21 22 16 23
;

data want(drop = idx);
   set z;
   array a a1-a4;
   array b b1-b4;
   has = 0;

   do over b;
      if check1 <= b <= check2 then idx = _I_;
   end;

   do over a;
      if _I_ > idx then leave;
      if a = 1 then has = 1;
   end;
run;

 

Result:

 

id a1 a2 a3 a4 b1 b2 b3 b4 check1 check2 has 
1  1  .  .  1  10 15 18 20 11     17     1 
2  .  .  1  1  17 18 19 21 16     18     0 
3  .  .  .  1  18 19 21 22 16     23     1 
FreelanceReinh
Jade | Level 19

Hello @kk13,

 

Try this:

data want(drop=f i);
set z;
array a a1-a4;
array b b1-b4;
f=whichn(1, of a{*});
HAS=0;
if f then do;
  do i=f to dim(a) until(check1<=b{i}<=check2);
  end;
  if i<=dim(a) then HAS=1;
end;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1014 views
  • 0 likes
  • 4 in conversation