stata:合并merge时如何处理关键变量外的同名变量的值分三种情况
use ceshi1,clear list // // +------------------------------------------------------+ // | id ks2019 ks2020 ks2021 ks2022 ks2023 ab | // |------------------------------------------------------| // 1. | 1 1 0 1 2 0 1 | // 2. | 2 2 0 2 3 2 . | // 3. | 3 3 0 3 4 4 3 | // 4. | 4 4 0 4 5 6 . | // 5. | 5 5 0 5 6 8 5 | // |------------------------------------------------------| // 6. | 6 6 0 6 7 10 . | // 7. | 7 7 1 7 8 12 7 | // 8. | 8 8 1 8 9 14 . | // 9. | 9 9 1 9 10 16 9 | // 10. | 10 10 1 10 11 18 . | // |------------------------------------------------------| // 11. | 11 11 1 11 12 20 11 | // 12. | 12 12 1 12 13 22 . | // 13. | 13 13 1 13 14 24 13 | // +------------------------------------------------------+ use ceshi2,clear list // +---------+ // | id ab | // |---------| // 1. | 1 2 | // 2. | 2 3 | // 3. | 3 4 | // 4. | 4 5 | // 5. | 5 6 | // |---------| // 6. | 6 7 | // 7. | 7 8 | // 8. | 8 9 | // 9. | 9 10 | // 10. | 10 11 | // |---------| // 11. | 11 12 | // 12. | 12 13 | // 13. | 13 14 | // +---------+ // 合并时,当两个数据集包含相同的变量时,主数据集中的那些被保留下来。这是默认的 use ceshi1,clear merge 1:1 id using ceshi2 list // +--------------------------------------------------------------------+ // | id ks2019 ks2020 ks2021 ks2022 ks2023 ab _merge | // |--------------------------------------------------------------------| // 1. | 1 1 0 1 2 0 1 Matched (3) | // 2. | 2 2 0 2 3 2 . Matched (3) | // 3. | 3 3 0 3 4 4 3 Matched (3) | // 4. | 4 4 0 4 5 6 . Matched (3) | // 5. | 5 5 0 5 6 8 5 Matched (3) | // |--------------------------------------------------------------------| // 6. | 6 6 0 6 7 10 . Matched (3) | // 7. | 7 7 1 7 8 12 7 Matched (3) | // 8. | 8 8 1 8 9 14 . Matched (3) | // 9. | 9 9 1 9 10 16 9 Matched (3) | // 10. | 10 10 1 10 11 18 . Matched (3) | // |--------------------------------------------------------------------| // 11. | 11 11 1 11 12 20 11 Matched (3) | // 12. | 12 12 1 12 13 22 . Matched (3) | // 13. | 13 13 1 13 14 24 13 Matched (3) | // +--------------------------------------------------------------------+ // merge 1:1 关键变量 using 被调用数据集,update 功能:允许主数据集中出现的任何缺失值由调用数据集中的非缺失值进行替换 use ceshi1,clear merge 1:1 id using ceshi2,update list // +--------------------------------------------------------------------------------+ // | id ks2019 ks2020 ks2021 ks2022 ks2023 ab _merge | // |--------------------------------------------------------------------------------| // 1. | 1 1 0 1 2 0 1 Nonmissing conflict (5) | // 2. | 2 2 0 2 3 2 3 Missing updated (4) | // 3. | 3 3 0 3 4 4 3 Nonmissing conflict (5) | // 4. | 4 4 0 4 5 6 5 Missing updated (4) | // 5. | 5 5 0 5 6 8 5 Nonmissing conflict (5) | // |--------------------------------------------------------------------------------| // 6. | 6 6 0 6 7 10 7 Missing updated (4) | // 7. | 7 7 1 7 8 12 7 Nonmissing conflict (5) | // 8. | 8 8 1 8 9 14 9 Missing updated (4) | // 9. | 9 9 1 9 10 16 9 Nonmissing conflict (5) | // 10. | 10 10 1 10 11 18 11 Missing updated (4) | // |--------------------------------------------------------------------------------| // 11. | 11 11 1 11 12 20 11 Nonmissing conflict (5) | // 12. | 12 12 1 12 13 22 13 Missing updated (4) | // 13. | 13 13 1 13 14 24 13 Nonmissing conflict (5) | // +--------------------------------------------------------------------------------+ // merge 1:1 关键变量 using 被调用数据集 ,update replace // 功能:主数据集中的任何取值在与调用数据集存在不同时,将由被调用数据集的非缺失值进行替换 use ceshi1,clear merge 1:1 id using ceshi2,update replace list // +--------------------------------------------------------------------------------+ // | id ks2019 ks2020 ks2021 ks2022 ks2023 ab _merge | // |--------------------------------------------------------------------------------| // 1. | 1 1 0 1 2 0 2 Nonmissing conflict (5) | // 2. | 2 2 0 2 3 2 3 Missing updated (4) | // 3. | 3 3 0 3 4 4 4 Nonmissing conflict (5) | // 4. | 4 4 0 4 5 6 5 Missing updated (4) | // 5. | 5 5 0 5 6 8 6 Nonmissing conflict (5) | // |--------------------------------------------------------------------------------| // 6. | 6 6 0 6 7 10 7 Missing updated (4) | // 7. | 7 7 1 7 8 12 8 Nonmissing conflict (5) | // 8. | 8 8 1 8 9 14 9 Missing updated (4) | // 9. | 9 9 1 9 10 16 10 Nonmissing conflict (5) | // 10. | 10 10 1 10 11 18 11 Missing updated (4) | // |--------------------------------------------------------------------------------| // 11. | 11 11 1 11 12 20 12 Nonmissing conflict (5) | // 12. | 12 12 1 12 13 22 13 Missing updated (4) | // 13. | 13 13 1 13 14 24 14 Nonmissing conflict (5) | // +--------------------------------------------------------------------------------+