2010-10-04 90 views
2

我有以下宏:如何從SAS中刪除excel工作簿的選項卡?

%macro export_set_excel(data,tabname); 
    PROC EXPORT DATA= &data. OUTFILE= "&results." DBMS=EXCEL REPLACE; 
      SHEET=&tabname.; 
     RUN; 
%mend export_set_excel; 

我的問題是,有時候如果它已經存在該宏不刪除該選項卡。無論如何,從SAS,我可以檢查是否存在選項卡,如果它存在,刪除它?

回答

3
proc sql; drop table <NAME OF EXCEL TAB>; quit; 
+0

這並不物理上刪除標籤,但它確實擦除了其上的所有數據。對我來說足夠好。謝謝! – oob 2010-10-04 19:49:21

4

您可以使用下面的宏通過DDE在Excel中刪除工作表。它要求您要從中刪除工作簿的工作簿是Excel中當前活動的工作簿。

/****************************************************************************** 
** PROGRAM: MACRO.DDE_WORKSHEET_DELETE.SAS 
** 
** DESCRIPTION: DELETES THE SPECIFIED WORKSHEET FROM THE ACTIVE WORKBOOK IN 
**    EXCEL. 
** 
** PARAMETERS: iWORKSHEET: THE NAME OF THE WORKSHEET. INCLUDE ANY SPACES THAT 
**       MAY BE CONTAINED IN THE NAME OF THE WORKSHEET. 
**       
** NOTES: SEE OTHER DDE_MACRO* FILES FOR MORE INFORMATION. 
** 
******************************************************************************* 
** VERSION: 
** 1.0 ON: 01APR10 BY: RP 
**  CREATED. 
******************************************************************************/ 
%macro dde_worksheet_delete(iWorksheet=); 
    filename cmdexcel dde 'excel|system'; 
    data _null_; 
    file cmdexcel; 

    /* 
    ** DELETE WORKSHEET. NEED TO TEMPORARILY TURN 
    ** OFF ERROR CHECKING TO SUPPRESS PROMPT. 
    */ 
    put '[error(false)]'; 
    put "%str([workbook.delete(%"&iWorksheet%")])"; 
    put '[error(true)]'; 
    run; 
    filename cmdexcel clear; 
%mend; 


/* 
** EXAMPLE USAGE: 
*/ 
%dde_worksheet_delete(iWorksheet=Sheet1); 
相關問題