I would like to generate a variable newvar
which is equal to 1 for observations which are above the average for two variables (var1
and var2
), where this average is not the global dataset average, but the average of the observations within the group to which observation belongs (variable group
).
Here is a replicable example:
clear
input str59 group float(var1 var2)
"Algeria" 0 .000033156746
"Algeria" 0 .00017467902
"Algeria" 0 .00024518964
"Algeria" 0 .000624308
"Angola" 0 .0007729884
"Angola" 0 .0014512347
"Angola" 0 .001463664
"Angola" 0 .0015886982
end