我一直在拼命地尝试从一个数据集创建一个邻接矩阵(我在R中有一个等价的),但是在SAS (初学者熟练程度)中无法做到这一点。如果你能帮我解决这个问题会很有帮助的。另外,请建议在SAS (没有SNA)中是否可以使用此矩阵和稀疏矩阵?
data test;
input id id_o;
cards;
100 300
600 400
200 300
100 200
;
run;
我找到所有唯一id和id_o的联合来创建一个列表。
proc sql;
create table test2 as
select distinct id from
(select id as id from test
union all
select id_o as id from test);
quit;
Test2看起来就像
100 600 200 300 400
现在,我想要一个邻接矩阵,当Test2 (100和id_o (300)从原始数据集中)之间有一个链接时,在一个位置分配一个1。假设Test2是i,对应的j处有一个1。
所以,邻接矩阵看起来就像
100 600 200 300 400
100 0 0 1 1 0
600 0 0 0 0 1
200 0 0 0 1 0
300 0 0 0 0 0
400 0 0 0 0 0
发布于 2015-01-08 11:19:37
这里有一种方法,扩展当前的代码。首先,您需要创建一个包含所有选项的空表,然后填写1/0。第二,将表转换成所需的格式。也许有一种方法可以用proc距离或其他proc来实现这一点,但我不确定。
*get all the 1's for matrix;
proc freq data=test;
table id*id_o/sparse out=dist1;
run;
*Fill into matrix with all options;
proc sql;
create table test3 as
select a.id, b.id as id_o, coalesce(c.count, 0) as count
from test2 as a
cross join test2 as b
left join dist1 as c
on a.id=c.id
and b.id=c.id_o
order by a.id, b.id;
quit;
*Transpose to desired format.
proc transpose data=test3 out=test4 prefix=id_;
by id;
id id_o;
var count;
run;
https://stackoverflow.com/questions/27847644
复制相似问题