"另存为"和"关闭文件"不使用宏

时间:2011-11-18 21:17:31

标签: sas sas-macro

我通常有一个sas宏代码,在运行sas代码并将数据填充到excel文件之后,在excel电子表格中自动运行宏“另存为”和“关闭文件”。

我遇到的问题是我现在拥有的excel文件有某些我无法编​​辑的宏,因为它们受密码保护,我也无法在其中添加任何“另存为”和“关闭文件”宏。有没有办法在sas的帮助下直接“另存为”和“关闭文件”,而不使用宏。

1 个答案:

答案 0 :(得分:4)

当然可以。您可以使用DDE来实现这一目标。下面是3个宏应该完全按照你的要求做。将它们添加到SAS autocall宏库中:

/******************************************************************************
** PROGRAM:  MACRO.DDE_SAVE_AND_CLOSE.SAS
**
** DESCRIPTION: SAVES THE CURRENT EXCEL FILE AND THEN CLOSES IT.  IF THE FILE
**              ALREADY EXISTS IT WILL BE OVERWRITTEN.
**
** PARAMETERS: iSAVEAS: THE DESTINATION FILENAME TO SAVE TO.
**             iType  : (OPTIONAL. DEFAULT=BLANK). 
**                      BLANK = XL DEFAULT SAVE TYPE
**                          1 = XLS DOC - OLD SCHOOL! PRE OFFICE 2007?
**                         44 = HTML - PRETTY COOL! CHECK IT OUT... 
**                         51 = XLSX DOC - OFFICE 2007 ONWARDS COMPATIBLE?
**                         57 = PDF
** 
** NOTES:  IF YOU ARE GETTING A DDE ERROR WHEN RUNNING THIS MACRO THEN DOUBLE
**         CHECK YOU HAVE PERMISSIONS TO SAVE WHERE YOU ARE TRYING TO SAVE THE
**         FILE.
** 
*******************************************************************************
** VERSION:
** 1.0 ON: 01APR10 BY: RP
**     CREATED.  
** 1.1 ON: 19MAY10 BY: RP
**     ADDED ITYPE OPTION.
** 1.2 ON: 20JUL10 BY: RP
**     DELAYED TURNING ERROR BACK ON AS WAS GETTING UNNECESSARY ERRORS IN 
**     OFFICE07
** 1.3 ON: 17AUG10 BY: RP
**     CHANGED TO FILE. CLOSE AS IT WOULD CLOSE TWO WORKBOOKS BEFORE.
** 1.4 ON: 21JUL11 BY: RP
**     STOPPED E-R-R-O-R LITERAL FROM SHOWING IN LOG
******************************************************************************/

%macro dde_save_and_close(iSaveAs=,iType=);
  %local iDocTypeClause;

  %let iDocTypeClause=;
  %if "&iType" ne "" %then %do;
    %let iDocTypeClause=,&iType;
  %end;

  filename cmdexcel dde 'excel|system';
  data _null_;
    length str_line $200;
    file cmdexcel;

    put '[error(false)]';
    put "%str([save.as(%"&iSaveAs%"&iDocTypeClause)])";
    put '[file.close(0)]';

    str_line = cats("[e","rror(true)]");
    put str_line;
  run;
  filename cmdexcel clear;

%mend;
/*%dde_save_and_close(iSaveAs=d:\rrobxltest, iType=44);*/


/******************************************************************************
** PROGRAM:  MACRO.DDE_SAVE_AS.SAS
**
** DESCRIPTION: SAVES THE CURRENT EXCEL FILE.  IF THE FILE
**              ALREADY EXISTS IT WILL BE OVERWRITTEN.
**
** PARAMETERS: iSAVEAS: THE DESTINATION FILENAME TO SAVE TO.
**             iType  : (OPTIONAL. DEFAULT=BLANK). 
**                      BLANK = XL DEFAULT SAVE TYPE
**                          1 = XLS DOC - OLD SCHOOL! PRE OFFICE 2007?
**                         44 = HTML - PRETTY COOL! CHECK IT OUT... 
**                         51 = XLSX DOC - OFFICE 2007 ONWARDS COMPATIBLE?
**                         57 = PDF
** 
** NOTES:  IF YOU ARE GETTING A DDE ERROR WHEN RUNNING THIS MACRO THEN DOUBLE
**         CHECK YOU HAVE PERMISSIONS TO SAVE WHERE YOU ARE TRYING TO SAVE THE
**         FILE.
** 
*******************************************************************************
** VERSION:
** 1.0 ON: 01APR10 BY: RP
**     CREATED.  
******************************************************************************/

%macro dde_save_as(iSaveAs=,iType=);
  %local iDocTypeClause;

  %let iDocTypeClause=;
  %if "&iType" ne "" %then %do;
    %let iDocTypeClause=,&iType;
  %end;

  filename cmdexcel dde 'excel|system';
  data _null_;
    length str_line $200;
    file cmdexcel;
    put '[error(false)]';
    put "%str([save.as(%"&iSaveAs%"&iDocTypeClause)])";
    str_line = cats("[e","rror(true)]");
    put str_line;
  run;
  filename cmdexcel clear;

%mend;
/*%dde_save_as(iSaveAs=d:\rrobxltest, iType=44);*/

/******************************************************************************
** PROGRAM:  MACRO.DDE_CLOSE_WITHOUT_SAVE.SAS
**
** DESCRIPTION: CLOSES EXCEL WITHOUT SAVING THE FILE
**
** PARAMETERS: NONE
** 
*******************************************************************************
** VERSION:
** 1.0 ON: 18MAY10 BY: RP
**     CREATED.  
******************************************************************************/

%macro dde_close_without_save();
  filename cmdexcel dde 'excel|system';
  data _null_;
    file cmdexcel;
    put '[close(0)]';
  run;
  filename cmdexcel clear;
%mend;
/*%dde_close_without_save;*/