I found similar topics on the forum but the solutions provided do not seem to fit my case.
I have 3 string variables:
- an invention identifier "case"
- a variable with the inventors collaborating on an invention "invid"
- a variable with the main collaborator of each inventor "main_collab"
There is one fixed main collaborator for each inventor (the relationship can be mutual or not), I would like to create a variable "common" identifying inventions on which each inventor-collaborator couple co-invent.
Data are long and look as follows:
case | invid | main_collab | common |
1 | A | B | 1 |
1 | B | C | 1 |
1 | C | D | . |
2 | D | H | . |
2 | G | F | . |
The closest I got to obtain what I want is:
levelsof main_collab, local(levels)
gen common=.
foreach l of local levels {
replace common=1 if invid=="`l'"
}
However:
- common switches to 1 when the collaborator is in the invid column
- I would like to run the loop at "case" level but I don't know how to combine foreach and by.
Hopefully I managed to explain myself, thanks in advance for any suggestion!
Giovanna
0 Response to combining foreach and by with strings
Post a Comment