Q:

Connect IDs based on values in rows, ignoring connection between identical IDs

This is a follow-up to my previous question: Connect IDs based on values in rows. 
I would now like to consider the case, where connections between identical idb's should be classified as 0.
The output is similar to the matrix in my previous post but with diagonal elements equal to 0:
      62014 62015 62016 62017 62018
62014   0     1     0     1     1
62015   1     0     0     0     0
62016   0     0     0     0     1
62017   1     0     0     0     1
62018   1     0     1     1     0

How can I do this in Stata?

A:

You can easily change the values in the diagonal of a matrix as follows:
: B
[symmetric]
       1   2   3   4   5
    +---------------------+
  1 |  1                  |
  2 |  1   1              |
  3 |  0   0   1          |
  4 |  1   0   0   1      |
  5 |  1   0   1   1   1  |
    +---------------------+

: _diag(B, 0)

: B
[symmetric]
       1   2   3   4   5
    +---------------------+
  1 |  0                  |
  2 |  1   0              |
  3 |  0   0   0          |
  4 |  1   0   0   0      |
  5 |  1   0   1   1   0  |
    +---------------------+

In the context of your question, you can simply do the following:
mata: B = foo1(A)
mata: _diag(B, 0)

getmata (idb*) = B

list

     +------------------------------------------------------------------------+
     |   idb      idd1      idd2      idd3   idb1   idb2   idb3   idb4   idb5 |
     |------------------------------------------------------------------------|
  1. | 62014    370490    879271   1112878      0      1      0      1      1 |
  2. | 62015    457013   1112878    370490      1      0      0      0      0 |
  3. | 62016    341863   1366174    533773      0      0      0      0      1 |
  4. | 62017    879271    327069    341596      1      0      0      0      1 |
  5. | 62018   1391443   1366174    879271      1      0      1      1      0 |
     +------------------------------------------------------------------------+

