title: Stata: Keep only observations with minimum, maximum and median value of a given variable
body: <p>In Stata, I have a dataset with two variables: <code>id</code> and <code>var</code>, and say 1000 observations. The variable <code>var</code> is of type <code>float</code> and takes distinct values for all observations. I would like to keep only the three observations where var is either the minimum of var, the maximum of var, or the median of var.</p>
  
  <p>The way I currently do this: </p>
  
  <pre><code>summarize var, detail
  local varmax = r(max)
  local varmin = r(min)
  local varmedian= r(p50)
  keep if inlist(float(var),float(`varmax') , float(`varmedian'), float(`varmin'))
  </code></pre>
  
  <p>The problem that I face is that sometimes the <code>inlist</code> condition will not match one of the value. E.g. I end up with two observations instead of three, for instance the one with min and the one with max, but not the one with median. I suspect this has to do with a precision problem. As you see, I tried to convert all numbers to float, but this is apparently not sufficient.</p>
  
  <p>Any fix to my solution, or alternative solution would be greatly appreciated (if possible without installing additional packages), thanks!</p>