title: How to keep all possible combinations in collapse by multiple variables in stata?
body: <p>When I <code>collapse</code> a dataset by multiple variables, by default, Stata keeps only the combinations for which there are observations and so statistics can be calculated. What can I do to keep all combinations, with a missing result for the statistics that can't be computed? In an example: </p>
  
  <pre><code>sysuse nlsw88, clear
  
  collapse (mean) grade, by(race occupation)
  
  table occupation race
  
  --------------------------------------------
                         |        race        
              occupation | white  black  other
  -----------------------+--------------------
  Professional/technical |     1      1      1
          Managers/admin |     1      1      1
                   Sales |     1      1      1
      Clerical/unskilled |     1      1       
               Craftsmen |     1      1       
              Operatives |     1      1      1
               Transport |     1      1       
                Laborers |     1      1      1
                 Farmers |     1              
           Farm laborers |     1      1       
                 Service |     1      1       
       Household workers |     1              
                   Other |     1      1       
  --------------------------------------------
  
  . list if race == 2 &amp; occupation == 9
  </code></pre>
  
  <p>You can see that there are no observations for black farmers. What I would like to have is:</p>
  
  <pre><code>. list if race == 2 &amp; occupation == 9
  
       +--------------------------+
       |  race   occupa~n   grade |
       |--------------------------|
   32. | black    Farmers       . |
       +--------------------------+
  </code></pre>