I have a dataset of identifiers that are hierarchical - e.g., a given identifier can have a parent identifier and/or be a parent identifier to another identifier, or not be related to any other identifier.

The dataset looks something like the following:

Code:
clear
input int id int parentid
1 42
2 42
3 42
42 .
29 1
15 1
30 .
end
For each identifier, I'd like to calculate the total number of children identifiers - that is, create a new variable, totalchildrenids, similar to the following:

Code:
clear
input int id int totalchildrenids
1 2
2 0
3 0
42 5
29 0
15 0
30 0
end
Working this out manually, id 42 would have five total children identifiers (ids 1, 2, and 3 are immediate children, and ids 29 and 15 are children of id 42 by virtue of being children of id 1). Id 2, 3, 29, 15, and 30 would have zero total children identifiers. Id 1 would have two children identifiers (ids 29 and 15 are immediate children).

I'd greatly appreciate any advice or ideas about how to do this using Stata.

Thanks,
Erika