'How to check common values in two lists and create a counter for repeats
I have a dataset of individuals, each with their own unique ID. We have information on who else they interact with in 3 different settings: at school, during recess and outside of school. The list of people cited by the individual is stored as one string, each element separated by a comma.
I have already used the
split, destring
command to create a unique variable for each friend cited in each situation.
clear
input int user_id strL coop_friends_list_1 int(RecessFriend1 RecessFriend2 RecessFriend3 RecessFriend4) strL liste_amis int(CloseFriend1 CloseFriend2 CloseFriend3 CloseFriend4)
79 "81, 80, 93, 92, 87, 94, 89, 88, 83, 84, 97" 81 80 93 92 "81, 92, 94, 83, 80, 88, 87" 81 92 94 83
80 "82, 83, 89, 88, 93, 92, 87, 81, 97, 84" 82 83 89 88 "81, 92" 81 92 . .
81 "82, 89, 93, 92, 87, 88, 79, 84, 80, 97, 83" 82 89 93 92 "80, 87, 92, 82, 88, 93, 98, 94, 89, 83, 85, 90, 95, 91, 96, 86, 79" 80 87 92 82
82 "80, 81, 87, 92, 93, 97" 80 81 87 92 "80, 81, 86, 87, 88, 89, 92, 93, 94, 97, 98" 80 81 86 87
83 "92, 80, 87, 81" 92 80 87 81 "87, 80, 92, 81" 87 80 92 81
84 "92, 97, 82, 87, 88, 93, 89, 80, 79, 83" 92 97 82 87 "91, 80, 90" 91 80 90 .
85 "95, 98, 94, 91, 86, 90, 96" 95 98 94 91 "95, 90, 98, 96, 91, 86, 93, 88, 82, 89" 95 90 98 96
86 "94, 96, 85, 91, 98, 95, 90" 94 96 85 91 "98, 85, 89, 82, 93" 98 85 89 82
87 "83, 81, 92, 88, 89, 93, 82, 80, 79, 84, 94" 83 81 92 88 "83" 83 . . .
88 "80, 81, 84, 87, 89, 92, 93, 94" 80 81 84 87 "84, 87, 80, 94, 89" 84 87 80 94
end
I would like to create a counter that counts the instances of repeat : in the list of peers during school hours, how many of these are also cited outside of school hours, that for each individual.
Solution 1:[1]
* Example generated by -dataex-. To install: ssc install dataex
clear
input int user_id strL(coop_friends_list_1 liste_amis)
79 "81, 80, 93, 92, 87, 94, 89, 88, 83, 84, 97" "81, 92, 94, 83, 80, 88, 87"
80 "82, 83, 89, 88, 93, 92, 87, 81, 97, 84" "81, 92"
81 "82, 89, 93, 92, 87, 88, 79, 84, 80, 97, 83" "80, 87, 92, 82, 88, 93, 98, 94, 89, 83, 85, 90, 95, 91, 96, 86, 79"
82 "80, 81, 87, 92, 93, 97" "80, 81, 86, 87, 88, 89, 92, 93, 94, 97, 98"
83 "92, 80, 87, 81" "87, 80, 92, 81"
84 "92, 97, 82, 87, 88, 93, 89, 80, 79, 83" "91, 80, 90"
85 "95, 98, 94, 91, 86, 90, 96" "95, 90, 98, 96, 91, 86, 93, 88, 82, 89"
86 "94, 96, 85, 91, 98, 95, 90" "98, 85, 89, 82, 93"
87 "83, 81, 92, 88, 89, 93, 82, 80, 79, 84, 94" "83"
88 "80, 81, 84, 87, 89, 92, 93, 94" "84, 87, 80, 94, 89"
end
// Create a local macro containing all user id's
numlist "1/100"
local all_users = r(numlist)
gen wanted = 0
foreach user of local all_users {
replace wanted = wanted + (ustrregexm(coop_friends_list_1, "\b`user'\b") & ustrregexm(liste_amis, "\b`user'\b"))
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Wouter |
