Suppose I have the following data in Stata:
clear
input id tna ret str2 name
1 2 3 "X"
1 3 2 "X"
1 5 3 "X"
1 6 -1 "X"
2 4 2 "X"
2 6 -1 "X"
2 8 -2 "X"
2 9 3 "P"
2 11 -2 "P"
3 3 1 "Y"
3 4 0 "Y"
3 6 -1 "Y"
3 8 1 "Z"
3 6 1 "Z"
end
I want to make an ID for new groups. These new groups should incorporate the observations with the same name (for example X), but should also incorporate all the observations of the same ID if the name started in that ID. For example:
X
is in the data set under two IDs: 1 and 2. The group ofX
should incorporate all the observations with the nameX
, but also the two observations of the nameP
(sinceX
started in ID 2 and the two observations with valueP
belong to groupX
)Y
started in ID 3, so the group should incorporate every observation with ID 3.