BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Arick
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Ksharp
Super User

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;
Rick_SAS
SAS Super FREQ

As KSharp points out, the expression 
a4[i] = a2/a1;

is incorrect for two reasons:

  1. The expression on the right is a vector whose j_th component is a2[j]/a1[j].  You cannot assign the vector on the right to the scalar on the left. PROC IML reports "Matrices do not conform to the operation" to indicate that you are attempting an impossible operation because the sizes of the various objects do not make sense.
  2. Because the expression on the right is a vector, it attempts to divide EVERY element of a2 by the corresponding element of a1. When an element of a1 is 0, PROC IML reports the warning "Division by zero, result set to missing value."
Arick
Fluorite | Level 6

Thanks both for the explanation. 

Rick_SAS
SAS Super FREQ

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.

Rick_SAS
SAS Super FREQ

Is there anything else we can help with? If we have answered your questions, you can mark an answer and close the thread.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 6 replies
  • 725 views
  • 2 likes
  • 4 in conversation