Good morning,

I would like to generate a newvar that returns the average of the 'rating' variable, for the first half of the 'run_no'

So in the example provided the 'run_no' goes in ascending order of date from 1-7.
The first half of the run_no is therefore 3.5, so in this case with an odd number, the first half of the run_no can be rounded up to the first 4 'run_no' values.
Therefore in this case I need the average 'rating' for the first half of the 'run_no' which is the average of 76-75-70-65, giving an average of 71.5 which can be rounded down to 71.

If there were for example 36 'run_no', then the average 'rating' for the first 18 of these would be required etc...

Thank you.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str9 ddate byte(run_no rating)
"01-Aug-21" 7 98
"26-Sep-20" 6 80
"16-Aug-20" 5 93
"31-Jul-20" 4 76
"21-Jul-20" 3 75
"30-Jun-20" 2 70
"12-Jun-20" 1 65
end