SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 1268|回复: 0
打印 上一主题 下一主题

Transform a SAS data set to an Excel pivot table by VBA

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2012-5-11 05:46:32 | 只看该作者

Transform a SAS data set to an Excel pivot table by VBA

From Dapangmao's blog on sas-analysis

<div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/-AYXdRz94wnk/T6wtgH_ktvI/AAAAAAAABFo/x6o5lwQkUdM/s1600/2012-05-08_100252.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="355" src="http://3.bp.blogspot.com/-AYXdRz94wnk/T6wtgH_ktvI/AAAAAAAABFo/x6o5lwQkUdM/s400/2012-05-08_100252.jpg" width="400" /></a></div><br />
Pivot Table in Excel is the popular data report format, which is similar to an OLAP cube that aggregates data at any dimensions. To create a pivot table for a table with lots of columns, it usually takes 100+ drags and clicks to get job done, which is somehow annoying. <br />
<br />
I didn't try the SAS’s Add-in for Microsoft Office or SAS Enterprise Guide yet. However, an easy solution to transform a SAS data set toward an Excel pivot table is possibly to use some VBA scripts under Excel. For example, SASHELP.PRDSAL2, which is a free data set shipped with SAS, records the furniture sales in 64 states of the three countries from 1995 to 1998, and has total 23,040 observations and 11 variables. This data set can be transformed to an Excel pivot table very quickly by two simple steps.<br />
<br />
<b>Step 1</b><br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://3.bp.blogspot.com/--vNJ_mPcWLA/T6wvNClRsuI/AAAAAAAABFw/ADXpEB8Wxxk/s1600/111111111111111.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="273" src="http://3.bp.blogspot.com/--vNJ_mPcWLA/T6wvNClRsuI/AAAAAAAABFw/ADXpEB8Wxxk/s400/111111111111111.jpg" width="400" /></a></div><b><br />
</b><br />
In SAS, a data set can be exported toward an XLS file through ODS destination. Although it is still based on HTLM format, it can still be opened by Excel.<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>ods html file = 'c:\tmp\prdsal2.xls' style = minimal;
title;
proc print data = sashelp.prdsal2 noobs;
run;
ods html close;
</code></pre><br />
<b>Step 2</b><br />
<div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-O0yRpySVb3w/T6wvTJfqLmI/AAAAAAAABF4/2VYS-yqrJKg/s1600/22222222222.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="273" src="http://4.bp.blogspot.com/-O0yRpySVb3w/T6wvTJfqLmI/AAAAAAAABF4/2VYS-yqrJKg/s400/22222222222.jpg" width="400" /></a></div><b><br />
</b><br />
Next step we click on this file, press ALT + F11 to enter VBA editor, paste the VBA code below and run it. Then the pivot table is created. The good thing about this method is that the pivot table can be replicated anywhere by such a VBA subroutine, and it is customizable for particular needs. The example was finished in Excel 2007.<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>
Sub createPT()
'   Set storage path for the pivot table
    myDataset = "sashelp.prdsal2"
    myFilepath = "c:\tmp\" &amp; myDataset &amp; "_" &amp; Format(Date, "dd-mm-yyyy") &amp; ".xlsx"
    Dim myPTCache As PivotCache
    Dim myPT As PivotTable

'   Delete the sheet containing the previous pivot table   
    Application.ScreenUpdating = False
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Pivot_Table_Sheet").Delete
    On Error GoTo 0
   
'   Create the cache
    Set myPTCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, SourceData:=Range("A1").CurrentRegion)
   
'   Add a new sheet for the pivot table
    Worksheets.Add
    ActiveSheet.Name = "Pivot_Table_Sheet"
   
'   Create the pivot table
    Set myPT = ActiveSheet.PivotTables.Add( _
        PivotCache:=myPTCache, TableDestination:=Range("A5"))
    With myPT
        .PivotFields("COUNTRY").Orientation = xlPageField
        .PivotFields("STATE").Orientation = xlRowField
        .PivotFields("PRODTYPE").Orientation = xlRowField
        .PivotFields("PRODUCT").Orientation = xlRowField
        .PivotFields("YEAR").Orientation = xlColumnField
        .PivotFields("QUARTER").Orientation = xlColumnField
        .PivotFields("MONTH").Orientation = xlColumnField
        .PivotFields("ACTUAL").Orientation = xlDataField
        .PivotFields("PREDICT").Orientation = xlDataField
        .DataPivotField.Orientation = xlRowField
        '  Add a calculated field to compare the predicted value and the actual value
        .CalculatedFields.Add "DIFF", "=PREDICT-ACTUAL"
        .PivotFields("DIFF").Orientation = xlDataField
        ' Specify a number format
        .DataBodyRange.NumberFormat = "$#, ##0.00"
        ' Apply a style for pivot table
        .TableStyle2 = "PivotStyleLight18"
    End With
    Range("A1").FormulaR1C1 = "Pivot table made from data set" &amp; " " &amp; myDataset
    Range("A2").FormulaR1C1 = "Prepared by <!-- w --><a class="postlink" href="http://WWW.SASANALYSIS.COM">WWW.SASANALYSIS.COM</a><!-- w --> on " &amp; Date
    ActiveWorkbook.SaveAs Filename:=myFilepath, _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
</code></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3256159328630041416-3416200398584197968?l=www.sasanalysis.com' alt='' /></div><img src="http://feeds.feedburner.com/~r/SasAnalysis/~4/plZNc1IzmbY" height="1" width="1"/>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-5-6 13:52 , Processed in 0.068731 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表