Rhythm
Rhythm

Reputation: 682

Issue with my sas code using array. Outcome is not as expected

I have been trying to execute a sas code using arrays. Strangely, it is not working the way i was expecting. So i used another approach and code runs fine with second method. But still i want to know ehat is wrong with the first method. Following is my code:

data have;
input free_m prevention substitution oth;
datalines;
. . . .
. 0 0 0
1 1 0 0
;
run;

data test;
set have;
/*method1*/
array a1(*) prevention substitution oth;
do i=1 to dim(a1);
    if free_m=. and prevention=0 and substitution=0 and oth=0 then a1(i)=.;
end;

/*method2*/
/*
if free_m=. and prevention=0 and substitution=0 and oth=0 then 
do;
    prevention=.;
    substitution=.;
    oth=.;
end;
*/
drop i;
run;

proc sql;
select * from test;
quit;

The outcome with /method2/ is correct and it is what i want:

enter image description here

But with /method1/, i am getting following output:

enter image description here

Is there anything wrong in method1? Kindly help! Thanks a lot.

Upvotes: 0

Views: 34

Answers (1)

Tom
Tom

Reputation: 51581

You are cutting off the limb that you are sitting on.

The first method works fine for I=1 and I=2 but when you get to I=3 and I=4 the values of prevention has been change from 0 to missing by the earlier iterations of the do loop. prevention and a1(2) refer to the same thing.

Upvotes: 1

Related Questions