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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 806 views
  • 0 likes
  • 4 in conversation