There are three primary matrices found in social network analysis: 1) adjacency matrices, 2) nodelist matricies, and edgelist matrices. These matrices contain information concerning a given social network. For example, image below shows undirected graph for a small social network. The graph is considered undirected if each link is two directional, i.e., if Person A has a link to Person B then Person B also has a link to Person A.

This FAQ page will show examples of converting from one matrix type to another. Let’s begin by looking at each of the three types using the example social network shown above.

The adjacency matrix shows who has direct connections to who. The graph above shows that homer has direct links to lisa, bart, marge, maggie, grampa and jackie. The adjacency matrix below has one’s in columns two through seven for row one and one’s for rows two through seven in column one, These represent all of homer’s direct links.

Matrix 1: Adjacency Matrix c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 r1 0 1 1 1 1 1 1 0 0 0 r2 1 0 1 1 0 1 0 1 0 0 r3 1 1 0 0 1 0 1 1 0 0 r4 1 1 0 0 1 1 0 0 0 0 r5 1 0 1 1 0 0 1 0 0 0 r6 1 1 0 1 0 0 0 0 0 0 r7 1 0 1 0 1 0 0 0 0 0 r8 0 1 1 0 0 0 0 0 1 0 r9 0 0 0 0 0 0 0 1 0 1 r10 0 0 0 0 0 0 0 0 1 0

Next, we show the same information contained in a nodelist matrix. In a nodelist each row gives a list of all the nodes that that that row connects to. For example, row 1 is homer who has links to six other nodes (2,3,4,5,6 and 7). Row 8 milhouse has connects to only three other nodes (2, 3, and 9). The rest of row 8’s columns have a period indicating that there are no other links.

Matrix 2: Nodelist (Wide) Matrix c1 c2 c3 c4 c5 c6 r1 2 3 4 5 6 7 r2 1 3 4 6 8 . r3 1 2 5 7 8 . r4 1 2 5 6 . . r5 1 3 4 7 . . r6 1 2 4 . . . r7 1 3 5 . . . r8 2 3 9 . . . r9 8 10 . . . . r10 9 . . . . .

Next, we again show the same information but as an edgelist matrix. We will sometimes refer to the
edgelist matrix the long form of the data because there are only two columns (later there will be a
third column). The edgelist
matrix has **from** and **to** columns which give the link from each **from** to
each **to**. For example, node 10 nelson has only one link to node 9 ralph. And, because this
is an undirected graph 9 ralph has a link back to 10 nelson as well as one to 8 milhouse.

Matrix 3: Edgelist (Long) Matrix from to 1 2 1 3 1 6 1 7 1 4 1 5 2 1 2 3 2 6 2 4 2 8 3 1 3 2 3 7 3 5 3 8 4 1 4 2 4 5 4 6 5 1 5 4 5 7 5 3 6 1 6 2 6 4 7 1 7 5 7 3 8 2 8 3 8 9 9 8 9 10 10 9

So there are the three primary types of matrices. The Stata commands we have written will want the data in a adjacency matrix however other programs may want the data in a different form. Or, you may receive data from someone else and the data will more than likely be in a nodelist (wide) matrix or an edgelist (long) matrix. So we need to be able to convert from one form to another. We will do this with a combination of user written programs, from ATS Stat Consulting, and built-in Stata commands. Here is the list od ATS written programs:

sna_adj2long-- compute long matrix from adjanency matrixsna_long2adj-- compute adjacency matrix from long matrixsna_wide2adj-- compute adjacency matrix from wide data

And here are the built-in commands we will use:

reshape wide-- compute wide data from long datareshape long-- compute long data from wide datamkmat-- save Stata data as Stata matrixsvmat-- save Stata matrix as Stata datamatrix rownames-- give row names to a matrix

All of the IDRE social network analysis commands, both for data management and for data analysis, can be
downloaded by typing, **search sna** into Stata’s command window and following the instructions.

There are two important things you should know about the IDRE social network analysis commands:

1) These programs should be considered to be beta software until more rigorous testing and updating
has been conducted, and

2)You may need to enter the following command the first tiem you run the program
after downloading tl, **mata: mata mlib index**

Okay, its finally time to get started.

**Long to adjacency matrix**

We will begin by reading in the data as an edgelist (long form) and compute an adjacency matrix.

use sna_long, clear clist in 1/12 from name to 1. 1 Diane 2 2. 1 Diane 3 3. 1 Diane 6 4. 1 Diane 7 5. 1 Diane 4 6. 1 Diane 5 7. 2 Fernando 1 8. 2 Fernando 3 9. 2 Fernando 6 10. 2 Fernando 4 11. 2 Fernando 8 12. 3 Garth 1

If the data do not include a weight variable of all one’s we will need to generate it. The
**sna_long2adj** command will need it. Then we
place the long data into a matrix matrix.

generate wgt = 1 mkmat from to wgt, mat(edge)

Now, we can use the **sna_long2adj** command and list the adjacency matrix.

sna_long2adj, long(edge) adj(adj1) mat list adj1, nohalfsymmetric adj1[10,10] c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 r1 0 1 1 1 1 1 1 0 0 0 r2 1 0 1 1 0 1 0 1 0 0 r3 1 1 0 0 1 0 1 1 0 0 r4 1 1 0 0 1 1 0 0 0 0 r5 1 0 1 1 0 0 1 0 0 0 r6 1 1 0 1 0 0 0 0 0 0 r7 1 0 1 0 1 0 0 0 0 0 r8 0 1 1 0 0 0 0 0 1 0 r9 0 0 0 0 0 0 0 1 0 1 r10 0 0 0 0 0 0 0 0 1 0

**Wide to adjacency matrix**

If the data are in a nodelist (wide form) we can use the **sna_wide2adj** to compute an adjacency matrix.

use sna_wide, clear clistnum name t1 t2 t3 t4 t5 t6 1. 1 Diane 2 3 6 7 4 5 2. 2 Fernando 1 3 6 4 8 . 3. 3 Garth 1 2 7 5 8 . 4. 4 Andre 1 2 5 6 . . 5. 5 Beverly 1 4 7 3 . . 6. 6 Carol 1 2 4 . . . 7. 7 Ed 1 5 3 . . . 8. 8 Heather 2 3 9 . . . 9. 9 Ike 8 10 . . . . 10. 10 Jane 9 . . . . .

To use the **sna_wide2adj** we give a list of variables and an **id** along with an optional
name for the adjacency matrix.

sna_widedata2adj t1 t2 t3 t4 t5 t6, id(num) adj(adj2) mat list adj2, nohalfsymmetric adj2[10,10] c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 r1 0 1 1 1 1 1 1 0 0 0 r2 1 0 1 1 0 1 0 1 0 0 r3 1 1 0 0 1 0 1 1 0 0 r4 1 1 0 0 1 1 0 0 0 0 r5 1 0 1 1 0 0 1 0 0 0 r6 1 1 0 1 0 0 0 0 0 0 r7 1 0 1 0 1 0 0 0 0 0 r8 0 1 1 0 0 0 0 0 1 0 r9 0 0 0 0 0 0 0 1 0 1 r10 0 0 0 0 0 0 0 0 1 0

**Adjacency matrix to long**

sna_adj2long, adj(adj2) long(longmat) drop num-t6 svmat longmat, names(col) clist to from wgt in 1/12to from wgt 1. 1 1 0 2. 2 1 1 3. 3 1 1 4. 4 1 1 5. 5 1 1 6. 6 1 1 7. 7 1 1 8. 8 1 0 9. 9 1 0 10. 10 1 0 11. 1 2 1 12. 2 2 0

The dataset including weights of zero has 100 observations. If we don’t need the observagtions with weight zero we can just drop them, leaving us with 36 observations which is what we had in our first long dataset.

drop if wgt==0 clist to from wgt in 1/12to from wgt 1. 2 1 1 2. 3 1 1 3. 4 1 1 4. 5 1 1 5. 6 1 1 6. 7 1 1 7. 1 2 1 8. 3 2 1 9. 4 2 1 10. 6 2 1 11. 8 2 1 12. 1 3 1

**Adjacency matrix to wide**

We don’t have a command to go from an adjacency matrix to a wide (nodelist) matrix but we can use the adjacency matrix to compute a long matrix and then using Stata’s reshape command we can get our wide matrix. Since, we already have the data in a long matrix from a previous example, we will pick up things from that point.

drop wgt rename to t sort from t by from: generate j=_n reshape wide t, i(from) j(j) clistfrom t1 t2 t3 t4 t5 t6 1. 1 2 3 4 5 6 7 2. 2 1 3 4 6 8 . 3. 3 1 2 5 7 8 . 4. 4 1 2 5 6 . . 5. 5 1 3 4 7 . . 6. 6 1 2 4 . . . 7. 7 1 3 5 . . . 8. 8 2 3 9 . . . 9. 9 8 10 . . . . 10. 10 9 . . . . .

**Long to wide**

Hey, we just did that in the previous example.

**Wide to long**

We can use Stata’s **reshape long** to go from wide (nodelist) to long (edgelist).
We just need to remember to drop observations in which **t**
id missing, rename **t** to **to** and to drop **j** (because we don’t need it anymore).

use sna_wide, clear clistnum name t1 t2 t3 t4 t5 t6 1. 1 Diane 2 3 6 7 4 5 2. 2 Fernando 1 3 6 4 8 . 3. 3 Garth 1 2 7 5 8 . 4. 4 Andre 1 2 5 6 . . 5. 5 Beverly 1 4 7 3 . . 6. 6 Carol 1 2 4 . . . 7. 7 Ed 1 5 3 . . . 8. 8 Heather 2 3 9 . . . 9. 9 Ike 8 10 . . . . 10. 10 Jane 9 . . . . .reshape long t, i(num) j(j) drop if t==. rename t to rename num from drop j generat wgt=1 clist in 1/12from name to wgt 1. 1 Diane 2 1 2. 1 Diane 3 1 3. 1 Diane 6 1 4. 1 Diane 7 1 5. 1 Diane 4 1 6. 1 Diane 5 1 7. 2 Fernando 1 1 8. 2 Fernando 3 1 9. 2 Fernando 6 1 10. 2 Fernando 4 1 11. 2 Fernando 8 1 12. 3 Garth 1 1

The above data management was fairly straight forward mainly due to the fact that the values that identified each node were sequential and that the links were all bidirectional. Let’s try some data management with a more difficult set of data.

**Part 2: Wide to adjacency matrix**

This time the nodelist will have only identification numbers, no names.

use snawide clistnum t1 t2 t3 t4 t5 t6 1. 12 18 42 19 51 36 24 2. 18 12 42 19 36 29 . 3. 42 12 18 51 24 29 . 4. 36 12 18 24 19 . . 5. 24 12 36 51 42 . . 6. 19 12 18 36 . . . 7. 51 12 24 42 . . . 8. 29 18 42 61 . . . 9. 61 29 74 . . . .

You will note that not only are the values of the node identifier, **num**, not sequential
but that node 61 has a link to node 74 while node node 74 does not link to any place. Fortunately,
**sna_wide2ady** will still work with this network.

sna_wide2adj t*, id(num) adj(adj) names(nodes) mat lis adjadj[10,10] c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 r1 0 1 1 1 0 1 1 1 0 0 r2 1 0 1 0 1 1 1 0 0 0 r3 1 1 0 0 0 1 0 0 0 0 r4 1 0 0 0 0 1 1 1 0 0 r5 0 1 0 0 0 0 1 0 1 0 r6 1 1 1 1 0 0 0 0 0 0 r7 1 1 0 1 1 0 0 1 0 0 r8 1 0 0 1 0 0 1 0 0 0 r9 0 0 0 0 1 0 0 0 0 1 r10 0 0 0 0 0 0 0 0 0 0mat list nodesnodes[10,1] c1 r1 12 r2 18 r3 19 r4 24 r5 29 r6 36 r7 42 r8 51 r9 61 r10 74

As you can see the adjacency matrix has 10 rows and is not symmtric. The matrix nodes has the complete list of the 10 node values.

**Part 2: Wide to long**

We can also use Stata’s **reshape** command to go from wide (nodelist) to long (edgelist), just
as we did earlier.

reshape long t, i(num) j(j) drop if t==. rename t to rename num from drop j generate wgt=1from to wgt 1. 12 18 1 2. 12 42 1 3. 12 19 1 4. 12 51 1 5. 12 36 1 6. 12 24 1 7. 18 12 1 8. 18 42 1 9. 18 19 1 10. 18 36 1 11. 18 29 1 12. 19 12 1

We can turn the data into a matrix by using the **mkmat** command.

mkmat from to wgt, mat(long) mat list longlong[35,3] from to wgt r1 12 18 1 r2 12 42 1 r3 12 19 1 r4 12 51 1 r5 12 36 1 r6 12 24 1 r7 18 12 1 r8 18 42 1 r9 18 19 1 r10 18 36 1 r11 18 29 1 r12 19 12 1 r13 19 18 1 r14 19 36 1 r15 24 12 1 r16 24 36 1 r17 24 51 1 r18 24 42 1 r19 29 18 1 r20 29 42 1 r21 29 61 1 r22 36 12 1 r23 36 18 1 r24 36 24 1 r25 36 19 1 r26 42 12 1 r27 42 18 1 r28 42 51 1 r29 42 24 1 r30 42 29 1 r31 51 12 1 r32 51 24 1 r33 51 42 1 r34 61 29 1 r35 61 74 1

**Part 2: Long to adjacency matrix**

Now let’s try to got from our long (edgelist) matrix to an adjacency matrix.

sna_long2adj, long(long) adj(adj2) names(name2) mat list adj2adj2[10,10] c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 r1 0 1 1 1 0 1 1 1 0 0 r2 1 0 1 0 1 1 1 0 0 0 r3 1 1 0 0 0 1 0 0 0 0 r4 1 0 0 0 0 1 1 1 0 0 r5 0 1 0 0 0 0 1 0 1 0 r6 1 1 1 1 0 0 0 0 0 0 r7 1 1 0 1 1 0 0 1 0 0 r8 1 0 0 1 0 0 1 0 0 0 r9 0 0 0 0 1 0 0 0 0 1 r10 0 0 0 0 0 0 0 0 0 0mat list name2name2[10,1] c1 r1 12 r2 18 r3 19 r4 24 r5 29 r6 36 r7 42 r8 51 r9 61 r10 74

**Part 2: Adjacency matrix to long**

Finally, we will end up with converting an adjacency matrix to a long (edgelist) matrix. If we want
to keep our same identifiers that we have been using in Part 2, we will need a column vector
of the node identifiers. let’s begin by reading in the dataset **snaadjacency** and taking
a look at it.

use snaadjacency list, compress sep(0)+--------------------------------------------------------+ | num t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 | |--------------------------------------------------------| 1. | 12 0 1 1 1 0 1 1 1 0 0 | 2. | 18 1 0 1 0 1 1 1 0 0 0 | 3. | 19 1 1 0 0 0 1 0 0 0 0 | 4. | 24 1 0 0 0 0 1 1 1 0 0 | 5. | 29 0 1 0 0 0 0 1 0 1 0 | 6. | 36 1 1 1 1 0 0 0 0 0 0 | 7. | 42 1 1 0 1 1 0 0 1 0 0 | 8. | 51 1 0 0 1 0 0 1 0 0 0 | 9. | 61 0 0 0 0 1 0 0 0 0 1 | 10. | 74 0 0 0 0 0 0 0 0 0 0 | +--------------------------------------------------------+

We will create two matrices uing Stata’s **mkmat** command and follow that up with the
**sna_adj2long** command.

mkmat num, mat(num) mkmat t*, mat(adjmat) sna_adj2long, adj(adjmat) long(longmat) names(num) mat list longmatlongmat[100,3] from to wgt r1 12 12 0 r2 12 18 1 r3 12 19 1 r4 12 24 1 r5 12 29 0 r6 12 36 1 r7 12 42 1 r8 12 51 1 r9 12 61 0 r10 12 74 0 r11 18 12 1 r12 18 18 0 r13 18 19 1 r14 18 24 0 r15 18 29 1 (... output omitted)

So there you have some of the ins and outs of data management for social network analysis in Stata.

**See also**

Stata FAQ: Social network analysis in Stata

Social network analysis command syntax