title: Stata:Comparison within a variable but between individuals
body: <p>I have a variable <code>ranking</code> which consists of many groups of different sizes which are all ranked. So one group might be 1-6 the next 1-4 and the next 1-52.</p>
  
  <p>I know want to create two variables that sum the differences between an individual and all individuals above and below him respectively. </p>
  
  <p>For a group of 5 individuals and individual 1 that means I want to get</p>
  
  <p>UP: SUM(1-1) =0
  DOWN: SUM((1-5)+ (1-4)+ (1-3)+ (1-2)) = -10 </p>