如何检查两个列表中的公共值并为重复创建计数器

How to check common values in two lists and create a counter for repeats

我有一个个人数据集,每个人都有自己唯一的 ID。我们有关于他们在 3 种不同环境中与其他人互动的信息:在学校、课间休息期间和校外。个人引用的人物列表存储为一个字符串,每个元素用逗号分隔。

我已经用过

split, destring

为每种情况下引用的每个朋友创建一个唯一变量的命令。

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

我想创建一个计数器来计算重复的实例:在上课时间的同龄人列表中,其中有多少在上课时间外也被引用,每个人。

* 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"))
}