Dear All, I was asked this question here. The data set is
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int(id exp) long roomnumber byte(feduc meduc)
53 1700 105111 4 5
43 1800 105111 8 3
57 1500 105211 5 6
56 2000 105211 5 6
60 2100 105211 3 3
58 1321 105211 4 4
63 2500 105211 7 7
59  900 105212 6 5
62 1200 105212 6 3
72 1200 105212 5 7
end
Firstly, for each room (`roomnumber'), there are alternative numbers of roommates (with different `id'). We want to obtain a new variable for each roommate, say `max_exp', which is the maximum of `exp' in each `roomnumber', excluding himself. I have done this by ( ssc install asrol)
Code:
bys roomnumber: asrol exp, stat(max) xf(focal)
with result as
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int(id exp) long roomnumber byte(feduc meduc) double max_exp
53 1700 105111 4 5 1800
43 1800 105111 8 3 1700
57 1500 105211 5 6 2500
56 2000 105211 5 6 2500
60 2100 105211 3 3 2500
58 1321 105211 4 4 2500
63 2500 105211 7 7 2100
59  900 105212 6 5 1200
62 1200 105212 6 3 1200
72 1200 105212 5 7 1200
end
Secondly, I want to generate two additional variable, say `feduc1' and`meduc1', which is the `feduc' and`meduc' from the one with maximum expenditure. Taking the average of `feduc' and`meduc' if there are ties in the maximum expenditures.Any suggestions? Thanks.