Stata:在不考虑缺失值的情况下跨变量查找重复项

Stata: find duplicates across variables without considering missing values

假设我有一个包含 2 个观察值和 7 个变量的数据集(其中 . 表示缺失值):

   crop1   crop2   crop3   crop4   nw1   nw2   nw3
   3       7       2       .       3     7     .
   5       9       .       .       5     .     .

现在我想通过生成一个名为 duplicates。那么新的数据集如下:

   crop1   crop2   crop3   crop4   nw1   nw2   nw3   duplicates
   3       7       2       .       3     7     .     2
   5       9       .       .       5     .     .     1

因为在第一次观察中发现 2 个值 (3, 7) 重复,在第二次观察中发现 1 个值 (5) 重复。

我可以使用什么样的命令来获取新数据集?

我不明白您在示例中有 4 个 crop 变量和 3 个 nw 变量时比较变量的规则。

您的问题从这里开始。如果您的规则不同,请准确解释。

我忽略 crop4 并将每次比较都算作重复,其中变量 具有相同数字后缀 的名称是相同的,只是它们可能不会丢失。

clear 
input crop1   crop2   crop3   crop4   nw1   nw2   nw3
   3       7       2       .       3     7     .
   5       9       .       .       5     .     .
end 

gen duplicates = 0 

forval j = 1/3 { 
   replace duplicates = duplicates + ((crop`j' == nw`j') & !missing(crop`j', nw`j')) 
} 

list 

     +------------------------------------------------------------+
     | crop1   crop2   crop3   crop4   nw1   nw2   nw3   duplic~s |
     |------------------------------------------------------------|
  1. |     3       7       2       .     3     7     .          2 |
  2. |     5       9       .       .     5     .     .          1 |
     +------------------------------------------------------------+