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;
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.
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
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.