title: How to denormalize data
body: <p>I have a proc means output that has rows for each summary statistics and columns for each variable. How can I "pivot" this so it is unique by the "by" variable?</p>
  
  <p>My workaround is to take the output and merge it onto itself using WHERE and RENAME commands. Is there an easier way to accomplish this?</p>
  
  <pre><code>proc means data=data1 n mean max min std noprint;
    var var1 var2;
    by month;
    output out=SummaryStats;
  run;
  data SummaryStatsUnique;
    merge SummaryStats (in=in1 keep=month var1 var2 _stat_ rename=(var1=var1_N var2=var2_N) where=(_stat_="N"))
          SummaryStats (in=in2 keep=month var1 var2 _stat_ rename=(var1=var1_MEAN var2=var2_MEAN) where=(_stat_="MEAN"))
          SummaryStats (in=in3 keep=month var1 var2 _stat_ rename=(var1=var1_MAX var2=var2_MAX) where=(_stat_="MAX"))
          SummaryStats (in=in4 keep=month var1 var2 _stat_ rename=(var1=var1_MIN var2=var2_MIN) where=(_stat_="MIN"))
          SummaryStats (in=in5 keep=month var1 var2 _stat_ rename=(var1=var1_STD var2=var2_STD) where=(_stat_="STD"))
    ;
    by month;
    if ^(first.month and last.month) then abort;
    if ^(in1 and in2 and in3 and in4 and in5) then abort;
  run;
  </code></pre>
  
  <p>This should take a table formatted like this:</p>
  
  <pre><code>MONTH     _STAT_     Var1     Var2
  2019.01   N          19       15
  2019.01   MEAN       17.5     13
  2019.01   MAX        21       16
  2019.01   MIN        15       12
  2019.01   STD        2.5      1.5
  2019.02   ...
  </code></pre>
  
  <p>and output like this:</p>
  
  <pre><code>MONTH     VAR1_N     VAR2_N     VAR1_MEAN    VAR2_MEAN   ...
  2019.01   19         15         17.5         13
  2019.02   ...
  </code></pre>