I have a list of 23 variables that can have a value between 1 and 12. I would like to get the number of variables in a given row that have a certain value. I have tried using egen's anycount but this has not produced accurate results. For example
so if an observation has two variables between the above varlist (i.e. var_*) that have a value of 3, nbo_var_3 ought to be 2.
However, each of the variables generated above in my dataset has between one and three inaccuracies. The inaccuracies always count one more value than they ought to. The code below ought to be able to replicate the same thing but if at all possible, I would like to know why anycount is not working as specified above. All of my variables are integers.
Code:
local values 1 2 3 4 5 6 7 8 9 10 11 12 foreach i of local values { egen nbo_var_`i' = anycount(var_1 var_2 var_3 var_4 var_5 /// var_6 var_7 var_8 var_9 var_10 /// var_11 var_12 var_13 var_14 var_15 /// var_16 var_17 var_18 var_19 var_20 /// var_21 var_22 var_23), v(`i') }
However, each of the variables generated above in my dataset has between one and three inaccuracies. The inaccuracies always count one more value than they ought to. The code below ought to be able to replicate the same thing but if at all possible, I would like to know why anycount is not working as specified above. All of my variables are integers.
Code:
local vars 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 local values 1 2 3 4 5 6 7 8 9 10 11 12 foreach i of local vars { foreach j of local values { g var_`i'_val_`j' = (var_`i' == `j') } } foreach j of local values{ egen nbo_var_`j'_new = rowtotal(var_1_val_`i' var_2_val_`i' var_3_val_`i' var_4_val_`i' var_5_val_`i' /// var_6_val_`i' var_7_val_`i' var_8_val_`i' var_9_val_`i' var_10_val_`i' /// var_11_val_`i' var_12_val_`i' var_13_val_`i' var_14_val_`i' var_15_val_`i' /// var_16_val_`i' var_17_val_`i' var_18_val_`i' var_19_val_`i' var_20_val_`i' /// var_21_val_`i' var_22_val_`i' var_23_val_`i') }
Comment