| 
 | 
楼主
 
 
 楼主 |
发表于 2012-5-19 04:00:24
|
只看该作者
 
 
 
Use the set operator UNION in PROC SQL
From Dapangmao's blog on sas-analysis 
 
<div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-YLwF3Xdv6Lw/T7ZsWUQoEEI/AAAAAAAABGQ/4vpqHxt1U_s/s1600/2012-05-18_103518.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="327" width="400" src="http://2.bp.blogspot.com/-YLwF3Xdv6Lw/T7ZsWUQoEEI/AAAAAAAABGQ/4vpqHxt1U_s/s400/2012-05-18_103518.jpg" /></a></div>SQL syntax contains a few <a href="http://en.wikipedia.org/wiki/Set_operations_(SQL)">set operators</a>, such as UNION, EXCEPT and INTERSECT.  The UNION operator concatenates the results of multiple SQL queries vertically into a single table for all matching rows, which I found particularly useful in PROC SQL while using SAS to manage data. Here come two examples. <br /> 
<br /> 
<b>Example 1 – Transpose data </b><br /> 
PROC SQL can transform a dataset to any desired structure, without referring to DATA step or PROC TRANSPOSE. For example, SASHELP.CLASS can be transposed from wide to long by the UNION ALL clause, and reversely from long to wide by the MAX function and the GROUP clause.<br /> 
<br /> 
From wide to long<br /> 
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code> 
data wide; 
   set sashelp.class; 
   drop sex; 
run; 
 
proc sql; 
   create table long as 
   select name, 'Age' as var, age as col1 
   from wide 
   union all 
   select name, 'Weight' as var, weight as col1 
   from wide 
   union all 
   select name, 'Height' as var, height as col1 
   from wide 
;quit;</code></pre>From long to wide <br /> 
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code> 
proc sql; 
   create table wide as 
   select name, 
      max(case when var = 'Age' then col1 end) as Age, 
      max(case when var = 'Weight' then col1 end) as Weight, 
      max(case when var = 'Height' then col1 end) as Height 
   from long 
   group by name; 
quit;</code></pre><b>Example 2 – Aggregate data into a cube</b><br /> 
In SAS, PROC SQL doesn’t support the ROLLUP or CUBE clause. However, we can apply multiple UNION operators to simulate such functionality. For example, we can create a cube table to list all possible summations of the ACTUAL variable by STATE, PRODUCT, YEAR in the SASHELP.PRDSAL2 dataset. Afterward we can easily query this multi-dimensional data structure to look for interesting aggregation information, without running any other aggregating procedure again.<br /> 
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code> 
data prdsal2; 
   set sashelp.prdsal2; 
run; 
 
proc sql; 
   create table cube as 
   select state,product, year, 'total by state, prodcut and year' as category, 
   sum(actual) as actual 
   from prdsal2 
   group by state, product, year 
   union 
   select state, product, ., 'total by state and prodcuct', sum(actual) 
   from prdsal2 
   group by state, product 
   union 
   select state,'', year, 'total by state and year', sum(actual) 
   from prdsal2 
   group by state, year 
   union 
   select '',product, year, 'total by product and year', sum(actual) 
   from prdsal2 
   group by product, year 
   union  
   select '' ,'', year, 'total by year', sum(actual) 
   from prdsal2 
   group by year 
   union  
   select state, '',. , 'total by state', sum(actual) 
   from prdsal2    
   group by state 
   union  
   select '',  product, ., 'total by product', sum(actual) 
   from prdsal2 
   union 
   select '', '', ., 'grand total', sum(actual) 
   from prdsal2 
   order by state, product, year 
;quit; 
</code></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3256159328630041416-3828103287620356590?l=www.sasanalysis.com' alt='' /></div><img src="http://feeds.feedburner.com/~r/SasAnalysis/~4/A9ToVq_E-yc" height="1" width="1"/> |   
 
 
 
 |