Hi,
I have a dataset similar to the following table where I want to calculate the mean values of x1, x2, and x3 by each id. I'm using the following code to get the average value for x1.

Code:
collapse avg_x1 = x1, by(id)
save mean_data, replace
However, when I merge them to the original dataset (the following table), I get missing values for some id's (id = 2 in this dataset). I'm using the following code to merge them:

Code:
use original_dataset, clear
sort id t
merge m:1 id using mean_data
the "_merge" variable corresponding to id=2 says "master only (1)", while its value corresponding to id=1 is "matched (3)".

I would appreciate any insights.

I also appreciate it if anyone can also address how I can get all x1, x2, and x3 averages by id, without needing to use -preserve-, -restore-, and -save- command for each variable separately.

Thanks,
.
id x1 x2 x3 t
1 210 1 1 1
1 0 0 1 2
1 465 3 1 3
1 948 3 1 4
1 0 0 1 5
1 1519 2 1 6
2 0 0 1 1
2 0 0 1 2
2 0 0 1 3
2 475 2 1 4
2 145 1 1 5
2 215 1 1 6