title: Counting distinct values in a proc means statement?
body: <p>This is a follow-up question to this: <a href="https://stackoverflow.com/questions/21145616/cleaner-way-of-handling-addition-of-summarizing-rows-to-table">Cleaner way of handling addition of summarizing rows to table?</a></p>
  
  <p>However, this time we've got something slightly different I'm afraid. We have: a dataset with four independent variables, and two dependent variables. We want:</p>
  
  <p>a) a set distinct by three independent variables, with a count of distinct variable #4 and sums of variables #5 &amp; #6. This is easy.</p>
  
  <p>b) TOTAL entries created for each combination of the three independent variables, with a count of distinct variable #4 and sums of variables #5 and #6. This is not easy (to me). </p>
  
  <p>So the idea would be to modify this:</p>
  
  <pre><code>proc means data=have;
  class ind1 ind2 ind3;
  var dependent5 dependent6;
  output out=want sum=;
  run;
  </code></pre>
  
  <p>Such that it additionally could count the number of distinct values of variable #4, for each combination of variables #1,2, and 3, including ALL. </p>
  
  <p>Ideas I've had:</p>
  
  <p>1) Abandon hope all ye who entered here; go back to trying to do this in proc sql with a bunch of macro code, which allows you to use the count(distinct )) useful thang.</p>
  
  <p>2) Something with nlevels? </p>
  
  <p>3) Using convoluted, terrible macro code to generate however many hashes would be necessary to handle the uniques.</p>
  
  <p>4) ??</p>
  
  <p>Creating sample data here is kind of tricky; let me know if this makes no sense and I'll do my best to come up with some.</p>
  
  <p>--</p>
  
  <p>Edit: to be clear, the reason SQL queries require macro code (and would be slow) is because it would need to be something like the following (but expanded to many more levels)</p>
  
  <pre><code>select
  "ALL" as ind1,
  ind2
  ...
  group by ind1, ind2;
  
  select
  ind1,
  "ALL" as ind2
  ...
  group by ind1, ind2;
  
  select
  "ALL" as ind1,
  "ALL" as ind2
  ...
  group by ind1, ind2;
  </code></pre>
  
  <p>This will get very unwieldly as we add more and more independent variables.</p>