最终目标:在原数据集里为每一个id根据条件匹配到子样本(一对多匹配),并计算子样本的某一变量的加总值sum,最后重新将sum(var)重新赋值给每一个id。
- 目标一——样本匹配:待匹配的变量是id, 匹配条件是location,legalform,size_ms这个三个变量的数值。假设需要为id为1的样本匹配对对应的样本,目标即为寻找跟id为1的样本 有相同的location,legal form,size_ms的值的样本。
- 目标二——计算所匹配的样本的某一变量的加总值:根据匹配的样本结果,计算与id为1的相匹配的样本所对应的a_SocialRespon3的加总值sum:即,一对多匹配,并计算多个匹配对象的sum(a_SocialRespon3),sum( a_Management3) ,sum(a_FoodSafety)这三个变量的各自的加总值。
- 目标三———循环。使得每一个id都可以按照上面的步骤循环计算出相应的值。
匹配数据集和待匹配数据集是同一个数据集
- * Example generated by -dataex-. To install: ssc install dataex
- clear
- input int(id year) byte(location legalform size_ms) float(a_SocialRespon3 a_Management3 a_FoodSafety)
- 1 2014 14 4 1 0 0 0
- 1 2015 14 4 1 0 0 0
- 1 2016 14 4 1 0 0 2
- 1 2017 14 4 1 0 0 2
- 1 2018 14 4 1 0 0 2
- 1 2019 14 4 1 0 0 2
- 2 2013 1 4 1 0 0 0
- 2 2014 1 4 1 0 0 0
- 2 2015 1 4 1 0 0 0
- 2 2016 1 4 1 0 0 1
- 2 2017 1 4 1 0 0 1
- 2 2018 1 4 1 0 0 1
- 3 2013 6 4 1 0 0 0
- 3 2014 6 4 1 0 0 0
- 3 2015 6 4 1 0 0 0
- 3 2016 6 4 1 0 0 1
- 3 2017 6 4 1 0 0 1
- 3 2018 6 4 1 0 0 1
- 3 2019 6 4 1 0 0 1
- 4 2013 7 4 1 0 0 0
- 4 2014 7 4 1 0 0 0
- 4 2015 7 4 1 0 0 0
- 4 2016 7 4 1 0 2 0
- 4 2017 7 4 1 0 2 0
- 4 2018 7 4 1 0 2 0
- 4 2019 7 4 1 0 2 0
- 5 2014 11 4 0 0 0 0
- 5 2015 11 4 0 0 0 0
- 5 2016 11 4 0 0 1 0
- 5 2017 11 4 0 0 1 0
- 5 2018 11 4 0 0 1 0
- 5 2019 11 4 0 0 1 0
- 6 2017 13 4 1 0 0 0
- 6 2018 13 4 1 0 0 0
- 7 2014 9 4 1 0 0 0
- 7 2015 9 4 1 0 0 0
- 7 2016 9 4 1 0 1 0
- 7 2017 9 4 1 0 1 0
- 8 2016 13 4 0 0 0 0
- 8 2017 13 4 0 0 0 0
- 8 2018 13 4 0 1 1 2
- 8 2019 13 4 0 1 1 2
- 9 2014 16 4 1 0 0 0
- 9 2015 16 4 1 0 0 0
- 9 2016 16 4 1 0 0 2
- 9 2017 16 4 1 0 0 2
- 9 2018 16 4 1 0 0 2
- 9 2019 16 4 1 0 0 2
- 10 2015 4 5 0 0 0 0
- 10 2016 4 5 0 0 1 0
- 10 2017 4 5 0 0 1 0
- 10 2018 4 5 0 0 1 0
- 10 2019 4 5 0 0 1 0
- 11 2015 4 4 1 0 0 0
- 11 2016 4 4 1 0 0 1
- 11 2017 4 4 1 0 0 1
- 11 2018 4 4 1 0 0 1
- 11 2019 4 4 1 0 0 1
- 12 2015 16 4 1 0 0 0
- 12 2016 16 4 1 0 0 0
- 12 2017 16 4 1 0 0 0
- 13 2015 8 4 0 0 0 0
- 13 2016 8 4 0 0 0 1
- 13 2017 8 4 0 0 0 1
- 13 2018 8 4 0 0 0 1
- 13 2019 8 4 0 0 0 1
- 14 2014 11 5 1 0 0 0
- 14 2015 11 5 1 0 0 0
- 14 2016 11 5 1 1 1 2
- 14 2017 11 5 1 1 1 2
- 14 2018 11 5 1 1 1 2
- 14 2019 11 5 1 1 1 2
- 15 2012 11 4 1 0 0 0
- 15 2013 11 4 1 0 0 0
- 15 2014 11 4 1 0 0 0
- 15 2015 11 4 1 0 0 0
- 15 2016 11 4 1 0 0 0
- 16 2012 9 4 1 0 0 0
- 16 2013 9 4 1 0 0 0
- 16 2014 9 4 1 0 0 0
- 16 2015 9 4 1 0 0 0
- 16 2016 9 4 1 0 0 0
- 16 2017 9 4 1 0 0 0
- 16 2018 9 4 1 2 1 0
- 16 2019 9 4 1 2 1 0
- 17 2015 10 4 1 0 1 0
- 17 2016 10 4 1 0 1 0
- 17 2017 10 4 1 0 1 0
- 17 2018 10 4 1 0 1 0
- 17 2019 10 4 1 0 1 0
- 18 2014 13 4 1 0 0 0
- 18 2015 13 4 1 0 0 0
- 18 2016 13 4 1 0 0 1
- 18 2017 13 4 1 0 0 1
- 18 2018 13 4 1 0 0 1
- 19 2015 11 4 1 0 0 0
- 19 2016 11 4 1 0 0 0
- 19 2017 11 4 1 0 0 0
- 19 2018 11 4 1 1 0 1
- 19 2019 11 4 1 1 0 1
- end