First a sort of admin bit. Pasting code into a text/code box opened on the forum with the </> that appears above the message will preserve formatting such as:
data Array;
set MED;
array d(9)$ MEDICAL_PRIMARY_DIAGNOSIS_CODE MEDICAL_DIAGNOSIS_CODE_2-MEDICAL_DIAGNOSIS_CODE_9;
do i=1 to 9;
DM=0;
DM1=0;
DM2=0;
if substr (d(i),1,3) = 'E11' then DM1=1;
if substr (d(i),1,3) = '250' then DM2=1;
if DM1=1 or DM2=1 then DM=1;
else DM=0;
end;
drop i;
run;
If you use any indenting in the code to make it easier to follow, as shown, then the code box preserves that.
Your logic above fails in what I think you may have attempted because the result of the DO loop is always the result for the last variable in the array.
You can quit a loop on a condition, such as possibly the first time DM=1 by using the LEAVE instruction. Perhaps
data Array;
set MED;
array d(9)$ MEDICAL_PRIMARY_DIAGNOSIS_CODE MEDICAL_DIAGNOSIS_CODE_2-MEDICAL_DIAGNOSIS_CODE_9;
do i=1 to 9;
DM=0;
DM1=0;
DM2=0;
if substr (d(i),1,3) = 'E11' then DM1=1;
if substr (d(i),1,3) = '250' then DM2=1;
if DM1=1 or DM2=1 then do;
DM=1;
leave;
end;
else DM=0;
end;
drop i;
run;
Or do not keep resetting DM to 0. Only set it to 1 when you want. SAS does not require initializing variables.
You would have to show the entire data step where you attempted to use the IN operator to see but I suspect the exact same issue as above with having your DM set from the last variable in the array, which I also suspect is often missing.
Note if you get tired of
if substr (d(i),1,3) = 'E11'
the =: operator, not the : immediately after the =, is "begins with" and you could use
if d(i) =: 'E11'
The comparison will only use the number of characters on the right side of the =: so you do not need the explicit Substr call.