SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 724|回复: 0
打印 上一主题 下一主题

A Macro to Create a Batch Submit SAS Program

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-3-8 00:14:46 | 只看该作者

A Macro to Create a Batch Submit SAS Program

From LCChien's blog on blogspot

<div class="separator" style="clear: both; text-align: left;">在一些很特殊的情況下,我們需要大量執行不同的 SAS 程式。若一個一個從 SAS 裡面開啟再執行,不但相當消耗時間,也不沒有太大的效率。Helen Sun 和 Cindy Wong 在 SAS Global Forum 2010 發表了一個簡易的巨集程式,讓 SAS 使用者在 PC 的視窗系統底下可以輕鬆批次地將同一個目錄底下的 SAS 程式碼給一次執行完畢。</div><div class="separator" style="clear: both; text-align: left;"></div><a name='more'></a>假設有一堆 SAS 程式被儲存在 C:\temp 的目錄底下,如下圖所示:<br /><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="http://www.webpagescreenshot.info/img/376504-32201161003pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.webpagescreenshot.info/img/376504-32201161003pm.png" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;">先執行以下的巨集程式一次:</div><code></code><br /><pre><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp;%MACRO BATCH(dir=);&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp;/*read in file names*/&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp;FILENAME dirfile PIPE "dir ""&amp;dir"" /b" LRECL=200;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp;DATA in;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;INFILE dirfile LENGTH=len;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;INPUT char $varying200. len;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp;RUN;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; /*only choose sas program names*/&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; DATA sasprg;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; SET in;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; IF INDEX(char,'.sas ');&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; RUN;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; /*create statement*/&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; DATA line;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; LENGTH name $200;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; SET sasprg;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; name="%include '"||trim(char)||"';";&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; DROP char;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; RUN;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; /*write out to a file*/&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; FILENAME fileref "&amp;dir.batch.sas";&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; DATA _null_;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; SET line;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; FILE fileref;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; &nbsp; PUT name;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; RUN;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; &nbsp; FILENAME fileref clear;&nbsp;</code></div><div class="separator" style="clear: both; text-align: left;"><code>&nbsp;&nbsp; %MEND;&nbsp;</code></div><code><br /><div class="separator" style="clear: both; text-align: left;"></div></code></pre><br /><div class="separator" style="clear: both; text-align: left;">然後再把路徑輸進 %BATCH 裡面:</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;">&nbsp;%BATCH(dir=C:\temp\);</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;">接著就會在這個路徑看到一個名為 batch.sas 的程式,打開來一看如下所示:</div><div class="separator" style="clear: both; text-align: center;"><a href="http://www.webpagescreenshot.info/img/375366-32201161033pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.webpagescreenshot.info/img/375366-32201161033pm.png" /></a></div><br />也就是說 %BATCH 幫使用者把該路徑底下的程式名稱一一放進 %include 裡面,然後我們就可以直接執行 batch.sas 這個程式來批次執行該目錄底下所有 SAS 程式。<br /><br /><b>CONTACT INFORMATION </b><br />Helen Sun<br />SAS Programmer<br /><!-- e --><a href="mailto:hsun@robarts.ca">hsun@robarts.ca</a><!-- e --><br />Robarts Clinical Trials<br />Robarts Research Institute<br />&nbsp;P.O. Box 5015, 100 Perth Drive<br />London, Ontario, Canada<br />N6A 5K8<br />Tel: 519-931-5700x24254<br />Fax: <span class="gc-cs-link" id="gc-number-3" title="Call with Google Voice">519-931-5284</span><br /><!-- w --><a class="postlink" href="http://www.robartsclinicaltrials.ca">www.robartsclinicaltrials.ca</a><!-- w --><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6268919072942670865-6043539532481275182?l=sugiclub.blogspot.com' alt='' /></div>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-6-11 11:03 , Processed in 0.073002 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表