SAS-通过DDE在Excel文件中执行查找和替换

SAS-通过DDE在Excel文件中执行查找和替换,excel,sas,dde,Excel,Sas,Dde,我正在尝试编写一个SAS程序,通过DDE在excel文件中进行查找和替换。具体地说,我试图在标题行中搜索字符串之间的空格(即“”),并将其替换为无空格(即“”) 例如,如果我有一个包含“TestName”的单元格,我想进行查找和替换,使其成为“TestName” 这就是我所拥有的: options noxwait noxsync; /* Open Excel */ x '"C:\Program Files (x86)\Microsoft Office\Office14\excel.exe"';

我正在尝试编写一个SAS程序,通过DDE在excel文件中进行查找和替换。具体地说,我试图在标题行中搜索字符串之间的空格(即“”),并将其替换为无空格(即“”)

例如,如果我有一个包含“TestName”的单元格,我想进行查找和替换,使其成为“TestName”

这就是我所拥有的:

options noxwait noxsync;

/* Open Excel */
x '"C:\Program Files (x86)\Microsoft Office\Office14\excel.exe"';
filename cmds dde 'excel|system';
data _null_;
    x=sleep(5);
run;

/* Open File and Manipulate*/
data _null_; 
    file cmds; 
    put '[open("C:\filename.xls")]'; 
    put '[select("R1")]';
    put '[formula.replace(" ","",1,,false,false)]'; 
run;

/*Close File*/
data _null_;
    file cmds;
    put '[FILE-CLOSE("C:\filename.xls")]';
    put '[QUIT()]';
run;
查找和替换功能不工作。在日志读取该语句后,我在日志中得到以下信息:

NOTE: The file CMDS is:
    DDE Session,
    SESSION=excel|system,RECFM=V,LRECL=256

ERROR: DDE session not ready.
FATAL: Unrecoverable I/O error detected in the execution of the DATA step program.  
    Aborted during the EXECUTION phase.
NOTE: 2 records were written to the file CMDS.
    The minimum record length was 21.
    The maximum record length was 70.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
    real time           0.63 seconds
    cpu time            0.00 seconds
有什么建议吗


还有,有人知道formula.replace语句中的参数是什么吗?我只知道第一个和第二个是你想找到的,你想用什么来取代它。我正在努力寻找任何文档。

我的建议是编写一个excel(VBA)宏来实现这一点,然后调用该宏;这不仅为您提供了编写代码、获取上下文线索等方面高得多的IDE,而且还为您提供了更多的控制。您可以从单独的工作簿中调用宏,因此,即使您必须重复执行此操作(我假定是这样),也可以将其放入“宏”模板工作簿中,并与自动文件分开打开。

如果输入的路径或文件名不正确,Excel将无法打开工作簿。您将在SAS日志中收到如下错误消息:

NOTE: The file DDECMD is:
      DDE Session,
      SESSION=excel|system,RECFM=V,LRECL=256
ERROR: DDE session not ready.
FATAL: Unrecoverable I/O error detected in the execution of the data step program.
       Aborted during the EXECUTION phase.
NOTE: 0 records were written to the file DDECMD.
NOTE: The SAS System stopped processing this step because of errors.

这就是我刚刚得出的结论。很高兴知道是什么导致了错误消息!我最终使用VBA程序来完成这项工作,并保存为csv,然后导入SAS。我在这里发布了另一篇关于VBA程序的帖子: