Hello,
I feel like this should be easy, but I cannot figure it out myself based on the help files or answers to similar questions.
I have observations on countries, but some countries have duplicate observations. For example, Hong Kong is is my dataset three times. For some variables the save values are in every observation and for others only one observation has a value and the others are missing. To clarify, this looks like this (although I have many more countries and variables):
I need to combine these three observations for Hong Kong into one observation:
So, there are three situations: (1) all duplicate observations have the same value for a variable, (2) only one observation has a value and the others are missing and (3) all are missing.
One thing I though about was to use:
The problem with this is that I have many observations, so I think there should be an easier way of doing this than looping over all the variables.
Any help is much appreciated!
Best,
Alexander
I feel like this should be easy, but I cannot figure it out myself based on the help files or answers to similar questions.
I have observations on countries, but some countries have duplicate observations. For example, Hong Kong is is my dataset three times. For some variables the save values are in every observation and for others only one observation has a value and the others are missing. To clarify, this looks like this (although I have many more countries and variables):
ISO | var1 | var2 | var3 | var4 | var5 | var6 |
HKG | 4 | 5 | 1 | . | . | . |
HKG | 4 | 5 | . | 2 | . | . |
HKG | 4 | 5 | . | . | 3 | . |
I need to combine these three observations for Hong Kong into one observation:
ISO | var1 | var2 | var3 | var4 | var5 | var6 |
HKG | 4 | 5 | 1 | 2 | 3 | . |
So, there are three situations: (1) all duplicate observations have the same value for a variable, (2) only one observation has a value and the others are missing and (3) all are missing.
One thing I though about was to use:
Code:
egen var4_mean = mean(var4), by ISO replace var4 = var4_mean if ISO == "HKG" drop var4_mean
Any help is much appreciated!
Best,
Alexander
Comment