I am using SAS for Windows 9.4. Below is the log for what I thought was simple SAS/IML code. But I cannot understand why I am receiving an error. Can anyone explain why and how to fix?
proc iml;
a1={1 2 3 4 0 0};
a2={1 1 1 1 1 1};
a3=j(1,6,0);
a4=j(1,6,0);
n_cols=ncol(a1);
PRINT a1 a2 a3 a4 n_cols;
do i=1 to n_cols;
if a1[i] ^= 0 then do; * Condition [1] *;
a3[i]=1;
a4[i]=a2/a1; *Why is this line processed when Condition [1] not met *;
end;
else do;
a3[i]=9;
a4[i]=9;
end;
end;
PRINT a3 a4;
quit;
The log file is attached.
Many of us cannot (or will not) download attachments.
Please include the log in your reply AND FORMAT IT PROPERLY by following these instructions: in the editor here at SAS Communities, click on the </> icon and paste the log into the window that appears. DO NOT SKIP THIS STEP.
Many of us cannot (or will not) download attachments.
Please include the log in your reply AND FORMAT IT PROPERLY by following these instructions: in the editor here at SAS Communities, click on the </> icon and paste the log into the window that appears. DO NOT SKIP THIS STEP.
Why not post it at IML forum and calling out @Rick_SAS .
proc iml; a1={1 2 3 4 0 0}; a2={1 1 1 1 1 1}; a3=j(1,6,0); a4=j(1,6,0); n_cols=ncol(a1); PRINT a1 a2 a3 a4 n_cols; do i=1 to n_cols; if a1[i] ^= 0 then do; * Condition [1] *; a3[i]=1; a4[i]=a2[i]/a1[i]; *Why is this line processed when Condition [1] not met *; end; else do; a3[i]=9; a4[i]=9; end; end; PRINT a3 a4; quit;
As KSharp points out, the expression
a4[i] = a2/a1;
is incorrect for two reasons:
Thanks both for the explanation.
By the way, you don't need any loops for this computation. The following is a vectorized way to write this program:
proc iml;
a1={1 2 3 4 0 0};
a2={1 1 1 1 1 1};
a3= choose(a1^=0, 1, 9);
a4=j(1,6,9);
idx = loc(a1^=0);
if ncol(idx)>0 then
a4[idx] = a2[idx] / a1[idx];
PRINT a3 a4;
If you are not familiar with the CHOOSE function, you can read about the CHOOSE function here.
If you are not familiar with the LOC function, you can read about the LOC function here.
Is there anything else we can help with? If we have answered your questions, you can mark an answer and close the thread.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.