我可能也会在基本 SAS 中这样做,但在 IML 中也很可行。我使用unique-loc方法将您的 ABC 转换为数值,然后使用该FULL
函数对矩阵进行平方:
data have;
input P1 $ P2 $ Score;
datalines;
A A 1
A B 2
A C 5
B B 4
B C 9
C A 3
C B 6
;;;;
run;
proc iml;
use have;
read all var _NUM_ into have_mat;
read all var _CHAR_ into p_mat;
p1_unique = unique(p_mat[,1]); *get unique values of p1;
p2_unique = unique(p_mat[,2]); *get unique values of p2;
num_mat = j(nrow(p_mat),3,0); *generate empty matrix to populate;
do i = 1 to ncol(p1_unique); *code the p1 values;
idx = loc(p_mat[,1] = p1_unique[i]);
num_mat[idx,2] = i; *sparse matrix format requires 2nd col be row values;
end;
do i = 1 to ncol(p2_unique); *code the p2 values;
idx = loc(p_mat[,2] = p2_unique[i]);
num_mat[idx,3] = i; *sparse matrix format requires 3rd col be col values;
end;
num_mat[,1] = have_mat; *and first col is the values for the matrix itself;
final_mat = full(num_mat); *FULL() function creates a square matrix and fills with 0s;
print final_mat;
quit;