在SAS 9.3中,我们可以使用函数distance
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
dist=distance(mat);
run;
quit;
我无法在SAS 9.2中找到此功能 我怎么能在9.2中做到?
谢谢。
答案 0 :(得分:4)
Rick Wicklin在blog post中回答了这个问题,他在其中介绍了distance
函数,但也告诉你如何以其他两种方式完成。
pairwisedist()
模块,用于定义如何计算矩阵的欧几里德距离。基本上,您可以确定矩阵中所有行组合之间的差异。然后求出差异的平方并取平方根。 PROC DISTANCE
的一个例子:
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
create matdata from mat [colname={'x1' 'x2' 'x3' 'x4' 'x5'}];
append from mat;
close matdata;
quit;
proc distance data=matdata out=dist method=Euclid nostd;
var interval(x1 x2);
run;
答案 1 :(得分:1)
感谢@Joe,我可以在proc iml
中使用Rick Wicklin的答案:
proc iml;
n=5;
seed=123456789;
lat= J(n,1,0); lon= J(n,1,0);
i = 1; do while (i <= n);
lat[i] = uniform(seed);
lon[i] = uniform(seed);
i = i + 1; end;
mat=lat||lon;
start PairwiseDist(x, y);
if ncol(x)^=ncol(y) then return (.); /* Error */
p = nrow(x); q = nrow(y);
idx = T(repeat(1:p, q)); /* index matrix for x */
jdx = shape(repeat(1:q, p), p); /* index matrix for y */
diff = abs(X[idx,] - Y[jdx,]);
return( shape( sqrt(diff[,##]), p ) );
finish;
start EuclideanDistance(x); /* in place of 12.1 DISTANCE function */
y=x;
return( PairwiseDist(x,y) );
finish;
distance=EuclideanDistance(mat);
print distance;
run;
quit;
谢谢Joe和Tick Rick Wicklin。