Good afternoon,
I start with two variables idvariable and score.
I run a small code to rank, get max score, get difference from max score
I need the 'wanted' var to return (if the max_score_diff value is 0) the second lowest value in the max_score_diff and reverse the sign (turn it into a negative)
In the example; the second lowest value in the max_score_diff variable is 2. So I need -2 to be returned in the wanted column (adjacent to the max_score_diff value of zero). All other values in the wanted var will stay the same as the adjacent value in max_score_diff.
Thank you
bysort idvariable: egen rank = rank(-score),track
egen max_score = max(score), by(idvariable)
gen max_score_diff = (max_score-score)
I start with two variables idvariable and score.
I run a small code to rank, get max score, get difference from max score
I need the 'wanted' var to return (if the max_score_diff value is 0) the second lowest value in the max_score_diff and reverse the sign (turn it into a negative)
In the example; the second lowest value in the max_score_diff variable is 2. So I need -2 to be returned in the wanted column (adjacent to the max_score_diff value of zero). All other values in the wanted var will stay the same as the adjacent value in max_score_diff.
Thank you
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte(idvariable score) float(rank max_score max_score_diff wanted) 1 77 1 77 0 -2 1 75 2 77 2 2 1 75 2 77 2 2 1 75 2 77 2 2 1 74 5 77 3 3 1 74 5 77 3 3 1 74 5 77 3 3 1 73 8 77 4 4 1 70 9 77 7 7 1 66 10 77 11 11 end
egen max_score = max(score), by(idvariable)
gen max_score_diff = (max_score-score)
Comment