全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学
16060 46
2007-01-21
SOURCE   : sastoxl.sas                                                     
PURPOSE  : To pour the contents of a SAS data set into a MS Excel spread-  
            sheet using DDE. The resulting spreadsheet receives some basic  
            formatting and is saved to a specified location. Values are     
            entered with their formats applied, whenever there is one      
            defined on the data set. An existing Excel file can be written  
            to, rather than a blank document. Custom sheetnames are sup-   
            ported, either existing ones or new ones ...                    
                                                                           
CREATED  : 08DEC1999 by Koen Vyverman                                      
                                                                           
MODIFIED : 31DEC2000 by Koen Vyverman                                      
              Re-wrote some of the macro decision code in a more canonical  
              form that will ensure proper SASv8 functioning.               
                                                                           
            04JAN2001 by Koen Vyverman                                      
              Added quite a slab of code to take care of custom sheet-names
              in the output spreadsheet. Introduced some long filenames in  
              the process, so this now no longer works with SAS v6.12 and   
              earlier ... Can be easily repaired though if necessary.      
              In the same go, removed all traces of the early WAPTWAP me-   
              thodology for resolving macro-variables before throwing them  
              at the DDE-link. Now, resolution is forced by means of dummy  
              text-variables (see DDECMD and MACCMD in the code ...)        
                                                                           
USAGE    : Macro parameters :                                             
                                                                           
            libin    (required): SAS library where the input SAS data set   
                                 lives.                                    
                                                                           
            dsin     (required): Name of the SAS data set (part of) which   
                                 needs to be dumped into Excel format.      
                                                                           
            cell1row (optional): The row number of the first cell of the XL
                                 spreadsheet where data should be inserted.
                                 i.e. the upper left corner of the output   
                                 data range.                                
                                 Default = 1.                              
                                                                           
            cell1col (optional): The column number of the first cell of the
                                 XL spreadsheet where data should be in-   
                                 serted.                                    
                                 i.e. the upper left corner of the output   
                                 data range.                                
                                 Default = 1.                              
                                                                           
            nrows    (optional): The number of rows/observations to be in-  
                                 serted. If none is specified, an attempt  
                                 will be made to insert all observations   
                                 into XL.                                 
                                 Needless to say, this number needs to be   
                                 smaller than the maximal number of rows   
                                 supported by Excel...                     
                                                                           
            ncols    (optional): The number of columns/variables to be in-  
                                 serted. If none is specified, an attempt   
                                 will be made to insert all variables into  
                                 XL.                                       
                                 Needless to say, this number needs to be   
                                 smaller than the maximal number of columns
                                 supported by Excel...                     
                                                                           
            tmplpath (optional): The full path to the directory where the   
                                 Excel spreadsheet resides to which the     
                                 data needs to be written. To be used in   
                                 conjunction with TMPLNAME. If none is spe-
                                 cified, a standard new XL workbook will be
                                 used. Do _not_ end the path with a back-   
                                 slash character.                           
                                                                           
            tmplname (optional): The filename of the Excel workbook in the  
                                 directory specified by TMPLPATH to which   
                                 the data needs to be written. To be used   
                                 in conjunction with TMPLPATH. If none is   
                                 specified, a standard new XL workbook will
                                 be used. Do _not_ end the name with a .xls
                                 filename extension.                        
                                                                           
            sheet    (optional): The name of the worksheet within the Excel
                                 workbook to which the data will be writ-   
                                 ten. When left blank, this defaults to a   
                                 name of the form 'SheetN' where N is the   
                                 smallest available positive integer not   
                                 yet in use in the Excel workbook. Just try
                                 some, pretty cool :-)                     
                                                                          
            savepath (optional): The full path to the directory where the   
                                 finalized Excel workbook needs to be      
                                 saved. May be used independently from      
                                 SAVENAME. Do _not_ end the path with a     
                                 back-slash character.                     
                                 Default = c:\temp                          
                                                                           
            savename (optional): The filename by which the finalized Excel
                                 workbook should be saved in the directory  
                                 specified by SAVEPATH. May be used inde-   
                                 pendently from SAVEPATH.                  
                                 Default = SASTOXL Output                  
                                                                           
            stdfmtng (optional): Standard formatting flag. Off by default.  
                                 Give a value of 1 to turn on. This will   
                                 apply some basic formatting to the         
                                 inserted data. The label row will be bol-  
                                 dened. Font will be set to Courier. Column
                                 width will be set to best fit. Freeze      
                                 panes will be turned on for the label row.
                                                                           
EXAMPLE  : 1) Suppose the data set WORK.SOMETHNG needs to be exported to   
               an Excel spreadsheet, and saved as 'c:\temp\Some data.xls'.  
               The data should end up in a worksheet with the default name  
               'Sheet1', and have the standard formatting applied to them.  
               To accomplish this, submit the following macro call:         
               %sastoxl(                                                   
                        libin=work,                                         
                        dsin=somethng,                                    
                        savepath=c:\temp,                                   
                        savename=Some Data,                                
                        stdfmtng=1                                         
                       );                                                  
                                                                           
            2) Suppose only the first 125 rows of data set WORK.SOMETHNG   
               need to be exported to an existing Excel spreadsheet, and   
               saved as 'c:\temp\Some data.xls'. Suppose the full path and  
               name of the document in which the data need to be inserted   
               is 'n:\sasok\data\blank dox\Serious Fun.xls', and the block  
               of data is wanted at row 37, column 3 of a worksheet named   
               'Stuff from SAS'. If 'Stuff from SAS' does not exist yet on  
               'Serious Fun.xls', then it should be added as an extra sheet.
               To accomplish this, submit the following macro call:         
               %sastoxl(                                                   
                        libin=work,                                         
                        dsin=somethng,                                      
                        cell1row=37,                                       
                        cell1col=3,                                         
                        nrows=125,                                          
                        tmplpath=n:\sasok\data\blank dox,                  
                        tmplname=Serious Fun,                              
                        sheet=Stuff from SAS,                              
                        savepath=c:\temp,                                   
                        savename=Some Data,                                 
                        stdfmtng=1                                          
                       );                                                   
                                                                           
CAVEAT   : Specifying either TMPLPATH or TMPLNAME without the other will   
            result in both values being reset to their default settings. As
            a consequence, a standard new document will be used.            
                                                                           
           Specifying a SHEET that already exists on the target workbook   
            will result in the content thereof (if any) being at least      
           partially over-written by the exported SAS data. Pay attention.
                                                                           
           The names of worksheets in an Excel workbook are _not_ case     
           sensitive, even if they look as if they are because one is      
           allowed to use mixed case in the naming. The same goes for the  
            filenames under Windows by the way ...
                                   
86064.rar
大小:(10.33 KB)

只需: 10 个论坛币  马上下载

本附件包括:

  • sastoxl.sas



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2007-1-21 09:00:00
<P>相信有不少人用sas时都要有个sas数据集输出到EXCEL和ACCESS的过程。相信有不少人都是用proc export来完成的,有的还会用鼠标点点点来输出。这也是个方法,不过不是一个好方法。如果能通过程序直接自动化+标准化输出到EXCEL或者ACCESS这就方便的多了,运行一些大型程序时就可以实现无守值运行了。</P>
<P>上面的这个macro %sastoxl就是用来输出数据集到EXCEL文件的。</P>
<P>这个macro是通过EXCEL的一个很古老的macro方式DDE来实现这个过程的。基本上office6.0以上的EXCEL都能使用这个。</P>
<P>虽然这个方法是一个古老的方法,但是效率相当的高。这个macro的容错性、兼容性还有灵活性都相当的好。如果觉得面的英文解释不好理解就跟贴,我会写个中文版的。</P>

<P>注意:使用时最好安装全版本的 SAS V8/V9 ,而excel能也最好是安装全版本的EXCEL2003。我以前安装的是2000的,而且是我自己选择了部分安装的。所以就出现了 DDE session Error的错位。安装了EXCEL2003后还修理了一下 SAS后就没问题了。</P>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2007-1-22 17:43:00
SAS输出到EXCEL已经很方便 了.不知道这个有什么好的?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2007-1-22 19:04:00
<P>SASv8 functioning</P>

<P>看来用老版本没什么好处:)</P>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2007-1-22 21:37:00
<P>回<STRONG><FONT face=Verdana color=#61b713>hookzhang:</FONT></STRONG></P>
<P><FONT face=Verdana color=#000000><STRONG>你下载来用用就知道啦~~</STRONG></FONT></P>
<P></P>
<P>这个pgm还是有好多可取之处的。学习一下人家老外的coding是不错的哦。</P>
<P>这个程序没有向上兼容行的问题。换了新版本的SAS和EXCLE都不会影响到输出的效果。如果你研究一下这个程序你就会知道其实SAS在这个过程中只是充当一个写DDEmacro的角色~~所以甚至porc export的licesne过了期都不会影响到输出的~~</P>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2007-1-24 17:10:00
呵呵,看来老兄没有用SAS做过报表,如果同时要求输出几十个EXCEL,你那种手动的EXPORT肯定是不行的.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群