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
Code:
use original_dataset, clear sort id t merge m:1 id using mean_data
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 |
0 Response to missing values when merge
Post a Comment