Hi,

This was supposed to be easy, but its not, for me anyway. I need to generate a new variable that contains the 2nd highest value by raceid. Here its 87 and 116. Using bysort raceid : gen second_highest = (dyha[_N-1]) is not providing those returns. any assistance appreciated please.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str27 raceid int dyha
"Ascot435860.618055555555556"  86
"Ascot435860.618055555555556"  89
"Ascot435860.618055555555556"  89
"Ascot435860.618055555555556"  84
"Ascot435860.618055555555556"  80
"Ascot435860.618055555555556"  84
"Ascot435860.618055555555556"  84
"Ascot435860.618055555555556"  87
"Ascot435860.618055555555556"  82
"Ascot435860.642361111111111" 116
"Ascot435860.642361111111111" 122
"Ascot435860.642361111111111" 107
"Ascot435860.642361111111111" 122
"Ascot435860.642361111111111"  95
"Ascot435860.642361111111111" 108
"Ascot435860.642361111111111" 114
end