code year var2 top5
1100 201712 5 1
1100 201712 5 2
1100 201712 5 3
1100 201712 4.9 4
1100 201712 4.8 5
1200 201712 6 1
1200 201712 5.9 2
1200 201712 5.4 3
1200 201712 5.4 4
1200 201712 5.4 5
1300 201712 6 1
1300 201712 2 2
1300 201712 1 3
1300 201712 3 4
1300 201712 2 5
1300 201812 6 1
1300 201812 6 2
1300 201812 5 3
1300 201812 4 4
1300 201812 4 5

Dear all ,this is a part of my data.As you can see I sort by code and year.I want to generate a variable.
This variable requires a complicated calculation process. The process is as follows: First, I want to calculate {(1-2)^2+(2-3)^2+(3-4)^2+(4-5)^2}/100 in top5, 1 to 5in the formula must be brought into the value in var2.Thank u so much.