SAS Adjacency Matrix Creation

152 views Asked by At

I have created this table: Table

And from this I want to create an adjacency matrix which shows how many employee_id's the tables share. It would look like this (I think): enter image description here I'm not sure if I'm going about this the correct way. I think I may be doing it wrong. I know that this is probably easier if I have more SAS products but I only have the basic SAS enterprise guide to work with.

I really appreciate the help. Thank you.

3

There are 3 answers

1
Reeza On BEST ANSWER

Here's another way using PROC CORR that's still better than the solution above. And you don't need to filter - it doesn't matter regarding the variables, you only specify them in the PROC CORR procedure.

data id;
input id:$4. human alien wizard;
cards;
1005 1 1 0
1018 0 0 1
1022 0 0 1
1024 1 0 0
1034 0 1 0
1069 0 1 0
1078 1 0 0
1247 1 1 1
;;;;
run;

ods output sscp=want;
proc corr  data=id  sscp ;
    var human alien wizard;
run;

proc print data=want;
    format _numeric_ 8.;
run;

Results are:

                   Obs    Variable       human       alien      wizard

                   1      human             4           2           1
                   2      alien             2           4           1
                   3      wizard            1           1           3
2
data _null_ On

I think this is what you want but it does not give the thing you show as answer.

data id;
   input id:$4. human alien wizard;
   cards;
1005 1 1 0
1018 0 0 1
1022 0 0 1
1024 1 0 0
1034 0 1 0
1069 0 1 0
1078 1 0 0
1247 1 1 1
;;;;
   run;

proc corr noprint nocorr sscp out=sscp;
   var human alien wizard;
   run;
proc print;
   run;

enter image description here

4
hope288 On

I was able to get the answer using this, although it does not include the last cell I wanted (human_alien_wizard):

proc transpose data=FULL_JOIN_ALL3 out=FULL_JOIN_ALL3_v2;
  by employee_id;
  var human_table alien_table wizard_table;
run; 

proc sql;
  create table FULL_JOIN_ALL3_v3 as
  select distinct a._name_ as anm,b._name_ as bnm,
  count(distinct case when a.col1=1 and b.col1=1 then a.employee_id else . end) as smalln
  from FULL_JOIN_ALL3_v2 a, FULL_JOIN_ALL3_v2 b
  where a.employee_id=b.employee_id
  group by anm,bnm
; 

proc tabulate data=FULL_JOIN_ALL3_v3;
 class anm bnm;
 var smalln;
 table anm='',bnm=''*smalln=''*sum=''*f=best3. / rts=5;
run;

enter image description here