2013-05-09 134 views
0

我想写一个SAS程序来查找并通过DDE在Excel文件中进行替换。具体来说,我试图在标题行中搜索字符串之间的空格(即“”),并将它们替换为无空格(即“”)。SAS - 通过DDE在Excel文件中执行查找和替换

例如,如果我有一个单元格包含“测试名称”我想要做一个查找并替换,使其“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语句中的参数是什么?我只知道第一个和第二个是你想要找到的以及你想要替换的东西。我正在努力寻找任何文件。

回答

0

我的建议是编写一个excel(VBA)宏来做到这一点,然后调用该宏;这不仅为您提供了一个非常优秀的IDE来编写代码,以获取上下文相关的线索等,还为您提供了更多的控制。你可以从不同的工作簿调用宏,所以即使这是你必须重复做的事情(我是这么认为的),你可以把它放在一个“宏”模板工作簿中,它与自动文件分开开启。

+0

这就是我刚才的结论。 – ESmith5988 2013-05-09 17:55:42

1

http://www.lexjansen.com/wuss/2007/ApplicationsDevelopment/APP_SmithC_ImportingExcelFiles.pdf

如果输入的路径或文件名不正确,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. 
+0

很高兴知道是什么导致了错误信息!我结束了使用VBA程序来做到这一点,并保存为一个CSV然后导入到SAS。我有另一个关于VBA程序的帖子,我最终在这里使用:http://stackoverflow.com/questions/16469186/vba-open-excel-find-and-replace-delete-row-save-as-csv – ESmith5988 2015-02-09 15:20:24