title: How to sort by defined order in sas, not alphabetically?
body: <p>Say I have a dataset for a supermarket,  with a product category, price product name etc. I want to sort by the category but with a defined order as opposed to alphabetically. </p>
  
  <p>For instance if the categories are: canned, dairy, meat,  vegetable,  and I want to sort by when they may expire (I'll it's likely we'd have that information, so just play along please).  This means I want to sort in this order: Dairy, Meat, Vegetable, Canned. </p>
  
  <p>I wrote a macro with this signature:</p>
  
  <pre><code>key_sort(ds=, keys='canned, dairy, meat,  vegetable', field =category, sort_by=) 
  </code></pre>
  
  <p>This parses the keys so that the can be put in a macro loop,  then I use that much loop to write out a select statement like so:</p>
  
  <pre><code>Select(&amp;field. ) ;
      %do i=1 %to &amp;number_of_keys. ;
      %let current_key= %scan(&amp;keys., &amp;I., &amp;delim.) ;
      When(&amp;current_key. ) &amp;field._key=&amp;i. ;
      %end;
  End;
  </code></pre>
  
  <p>I then sort by the  <code>&amp;field._key</code><br>
  Is this the best method to take? Can this be done more succinctly or efficiently? </p>