Hello All,
There are two matrices with missing values. I try to do the horizontal direct product (HDIR) in SAS, but I am receiving the following error: "ERROR: Matrix c has not been set to a value." It seems that most matrix operators and functions in SAS do not support missing values. Could you please help me figure this out? Any assistance that can be provided is much appreciated.
Here is an example:
proc iml;
a = {1 .,
2 4,
3 6};
b = {0 2,
. 1,
0 -1};
c = hdir(a, b);
print a b c;
Here is the Log:
As Paige said, use the definition to write your own function. You didn't say what you expect the result to be, but I assume you want to propagate the missing like this:
proc iml;
start HDir2(a, b);
c = j(nrow(a), ncol(a)*ncol(b), .);
do i = 1 to ncol(a);
startCol = (i-1)*ncol(b) + 1;
endCol = i*ncol(b);
c[ , startCol:endCol] = a[ ,i] # b;
end;
return c;
finish;
a = {1 .,
2 4,
3 6};
b = {0 2,
. 1,
0 -1};
c = HDir2(a, b);
print c;
You'd have to write your own Function in IML to do what you want.
Although SAS/IML propagates missing values for elementwise operations, missing values do not propagate for vector and matrix operations. For a discussion and some workarounds, see "Matrix multiplication with missing values in SAS."
What is the application you are trying to compute? Are you creating a design matrix of interaction effects, or something else? Also, what answer do you EXPECT to get from your example?
Thanks very much for your reply. I am doing my research on interaction effects. We have a large dataset with 17 interested predictors. All the predictor variables have missing values. I need to create a new data set with all the 2-way interaction effects. I think horizontal direct product (HDIR) in SAS can help me create those interaction effects. First, I have successfully read all the predictor variables into a matrix (denoted by matrix_L18_Q34) in SAS. Then I try to do use the code "interaction_matrix_L18_Q34 = hdir(matrix_L18_Q34, matrix_L18_Q34)". However, it shows that "ERROR: (execution) Matrix has not been set to a value."
Here is the log:
As Paige said, use the definition to write your own function. You didn't say what you expect the result to be, but I assume you want to propagate the missing like this:
proc iml;
start HDir2(a, b);
c = j(nrow(a), ncol(a)*ncol(b), .);
do i = 1 to ncol(a);
startCol = (i-1)*ncol(b) + 1;
endCol = i*ncol(b);
c[ , startCol:endCol] = a[ ,i] # b;
end;
return c;
finish;
a = {1 .,
2 4,
3 6};
b = {0 2,
. 1,
0 -1};
c = HDir2(a, b);
print c;
How about this one ?
proc iml;
a = {1 .,
2 4,
3 6};
b = {0 2,
. 1,
0 -1};
aa=a;
bb=b;
aa[loc(a=.)]=0;
bb[loc(b=.)]=0;
c = hdir(aa, bb);
a1=(a=.);
b1=(b=.);
a2=j(nrow(a),ncol(a));
b2=j(nrow(b),ncol(b));
a1b2=hdir(a1,b2);
a2b1=hdir(a2,b1);
c[loc(a1b2=1)]=.;
c[loc(a2b1=1)]=.;
print c;
quit;
SAS Output
c | |||
---|---|---|---|
0 | 2 | . | . |
. | 2 | . | 4 |
0 | -3 | 0 | -6 |
Assuming the matrices you are working with are not large, it might be easier to calculate more than necessary and then throw away the unwanted parts.
proc iml;
a = {1 .,
2 4,
3 6};
b = {0 2,
. 1,
0 -1};
n = nrow( a );
c = (a @ b)[ do(1, n#n, n+1), ];
print a b c;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.