Hello,

I have a long dataset with multiple observations of a value (value) per person id (pid). I have created another variable (prepost) that indicates whether the value was before or after the pandemic. I am trying to create a variable that is the mean of values in the pre period and the post period for each person. I attempted to create these variables (meanpre and meanpost) using the following code:

bysort pid: egen meanpre=mean(value) if prepost==0
bysort pid: egen meanpost=mean(value) if prepost==1

but this is giving me the wrong mean (as you can see below in my dataex example). I can't figure out what I'm doing wrong. Any help much appreciated!

Sarah



Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str8 pid double value float(prepost meanpre meanpost)
"0101336" 101 0      102         .
"0101336" 103 0      102         .
"0101336" 102 1        .        98
"0101336"  99 1        .        98
"0101336"  93 1        .        98
"0101685"  84 1        .        84
"0103544" 115 0      115         .
"0103544" 114 1        .       114
"0105274"  98 0       98         .
"0114998"  13 0       13         .
"0114998"   4 1        . 74.666664
"0114998" 123 1        . 74.666664
"0114998"  97 1        . 74.666664
"0122801" 118 0      118         .
"0122801" 117 1        .       113
"0122801" 109 1        .       113
"0124839"  39 0       76         .
"0124839" 113 0       76         .
"0124839" 119 1        .       119
"0132642"   1 0     49.5         .
"0132642"  98 0     49.5         .
"0132642"  79 1        .      83.5
"0132642"  88 1        .      83.5
"0132723" 100 0    106.5         .
"0132723" 113 0    106.5         .
"0132723" 101 1        .       101
"0151290"   4 0       55         .
"0151290" 106 0       55         .
"0151290"  34 1        .      28.5
"0151290"  23 1        .      28.5
"0168292" 102 0      102         .
"0168292" 109 1        .     106.5
"0168292" 104 1        .     106.5
"0170373" 114 0      117         .
"0170373" 120 0      117         .
"0170373" 115 1        .       115
"0171953" 106 0   106.25         .
"0171953" 112 0   106.25         .
"0171953" 110 0   106.25         .
"0171953"  97 0   106.25         .
"0171953" 112 1        .       112
"0180529" 123 1        .     110.5
"0180529"  98 1        .     110.5
"0184361"  91 0       91         .
"0184361"  88 1        .        88
"0187877"  89 0       89         .
"0187877"  89 1        .        89
"0200362" 102 0      102         .
"0206173" 122 0      122         .
"0206173" 116 1        .       116
"0207043"  85 0       85         .
"0208721"  87 0       87         .
"0208721"  91 1        .        91
"0209427" 120 0       65         .
"0209427"  10 0       65         .
"0209427"   2 1        .      62.5
"0209427" 123 1        .      62.5
"0214641"  83 0       87         .
"0214641"  89 0       87         .
"0214641"  89 0       87         .
"0214641"  94 1        .        91
"0214641"  91 1        .        91
"0214641"  88 1        .        91
"0219477"   3 0     61.5         .
"0219477" 120 0     61.5         .
"0219477"   8 1        .         8
"0234158"  40 0       40         .
"0234158"  44 1        .        82
"0234158" 120 1        .        82
"0238102"   9 0        9         .
"0238102" 108 1        .       108
"0241930"  42 0       42         .
"0241930"  20 1        .        20
"0260315"  90 0       90         .
"0260315"  89 1        .        89
"0260523" 108 0      108         .
"0260523" 110 1        .       110
"0260523" 110 1        .       110
"0263862"  17 0       17         .
"0263862" 112 1        .       112
"0266398"  98 0       98         .
"0266398"  91 1        .        91
"0271549" 116 1        .       116
"0271549" 116 1        .       116
"0274223" 102 1        .        99
"0274223"  96 1        .        99
"0287104" 106 0      106         .
"0287104" 113 1        .       113
"0289246"  91 0 90.33334         .
"0289246"  92 0 90.33334         .
"0289246"  88 0 90.33334         .
"0289246"  90 1        .        90
"0289399"  83 0       83         .
"0289399"  80 1        .        80
"0290089" 101 0      101         .
"0290089" 103 1        .       103
"0290943" 101 0      101         .
"0290943" 108 1        . 104.66666
"0290943" 106 1        . 104.66666
"0290943" 100 1        . 104.66666
end
label values value result3
label def result3 1 "10.0", modify
label def result3 2 "10.1", modify
label def result3 3 "10.2", modify
label def result3 4 "10.3", modify
label def result3 8 "10.7", modify
label def result3 9 "10.8", modify
label def result3 10 "10.9", modify
label def result3 13 "11.2", modify
label def result3 17 "11.6", modify
label def result3 20 "11.9", modify
label def result3 23 "12.2", modify
label def result3 34 "13.3", modify
label def result3 39 "13.8", modify
label def result3 40 "13.9", modify
label def result3 42 "14.0", modify
label def result3 44 "14.2", modify
label def result3 79 "5.3", modify
label def result3 80 "5.4", modify
label def result3 83 "5.7", modify
label def result3 84 "5.8", modify
label def result3 85 "5.9", modify
label def result3 87 "6.1", modify
label def result3 88 "6.2", modify
label def result3 89 "6.3", modify
label def result3 90 "6.4", modify
label def result3 91 "6.5", modify
label def result3 92 "6.6", modify
label def result3 93 "6.7", modify
label def result3 94 "6.8", modify
label def result3 96 "7.0", modify
label def result3 97 "7.1", modify
label def result3 98 "7.2", modify
label def result3 99 "7.3", modify
label def result3 100 "7.4", modify
label def result3 101 "7.5", modify
label def result3 102 "7.6", modify
label def result3 103 "7.7", modify
label def result3 104 "7.8", modify
label def result3 106 "8.0", modify
label def result3 108 "8.2", modify
label def result3 109 "8.3", modify
label def result3 110 "8.4", modify
label def result3 112 "8.6", modify
label def result3 113 "8.7", modify
label def result3 114 "8.8", modify
label def result3 115 "8.9", modify
label def result3 116 "9.0", modify
label def result3 117 "9.1", modify
label def result3 118 "9.2", modify
label def result3 119 "9.3", modify
label def result3 120 "9.4", modify
label def result3 122 "9.6", modify
label def result3 123 "9.7", modify
label values prepost prepost
label def prepost 0 "Pre-COVID", modify
label def prepost 1 "Post-shelterinplace", modify