tags:

views:

38

answers:

2

I have the following macro:

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

My problem is that sometimes this macro doesn't delete the tab if it already exists. Is there anyway, from SAS, that I can check if a tab exists and delete it if it does?

+3  A: 
proc sql; drop table <NAME OF EXCEL TAB>; quit;
rkoopmann
this does not physically delete the tab, but it does erase all of the data on it. good enough for me. thanks!
oob
+3  A: 

Hi oob

You can use the below macro to delete a worksheet in Excel via DDE. It requires that the workbook that you want to delete the sheet from is the currently active workbook in 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);

BTW - be sure to check out www.runsubmit.com. It's just like stackoverflow but dedicated to SAS.

Cheers Rob

Rob Penridge