|
楼主

楼主 |
发表于 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> %MACRO BATCH(dir=); </code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code> /*read in file names*/ </code></div><div class="separator" style="clear: both; text-align: left;"><code> FILENAME dirfile PIPE "dir ""&dir"" /b" LRECL=200; </code></div><div class="separator" style="clear: both; text-align: left;"><code> DATA in; </code></div><div class="separator" style="clear: both; text-align: left;"><code> INFILE dirfile LENGTH=len; </code></div><div class="separator" style="clear: both; text-align: left;"><code> INPUT char $varying200. len; </code></div><div class="separator" style="clear: both; text-align: left;"><code> RUN; </code></div><div class="separator" style="clear: both; text-align: left;"><code> </code></div><div class="separator" style="clear: both; text-align: left;"><code> /*only choose sas program names*/ </code></div><div class="separator" style="clear: both; text-align: left;"><code> DATA sasprg; </code></div><div class="separator" style="clear: both; text-align: left;"><code> SET in; </code></div><div class="separator" style="clear: both; text-align: left;"><code> IF INDEX(char,'.sas '); </code></div><div class="separator" style="clear: both; text-align: left;"><code> RUN; </code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code> /*create statement*/ </code></div><div class="separator" style="clear: both; text-align: left;"><code> DATA line; </code></div><div class="separator" style="clear: both; text-align: left;"><code> LENGTH name $200; </code></div><div class="separator" style="clear: both; text-align: left;"><code> SET sasprg; </code></div><div class="separator" style="clear: both; text-align: left;"><code> name="%include '"||trim(char)||"';"; </code></div><div class="separator" style="clear: both; text-align: left;"><code> DROP char; </code></div><div class="separator" style="clear: both; text-align: left;"><code> RUN; </code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code> /*write out to a file*/ </code></div><div class="separator" style="clear: both; text-align: left;"><code> FILENAME fileref "&dir.batch.sas"; </code></div><div class="separator" style="clear: both; text-align: left;"><code> DATA _null_; </code></div><div class="separator" style="clear: both; text-align: left;"><code> SET line; </code></div><div class="separator" style="clear: both; text-align: left;"><code> FILE fileref; </code></div><div class="separator" style="clear: both; text-align: left;"><code> PUT name; </code></div><div class="separator" style="clear: both; text-align: left;"><code> RUN; </code></div><div class="separator" style="clear: both; text-align: left;"></div><div class="separator" style="clear: both; text-align: left;"><code> FILENAME fileref clear; </code></div><div class="separator" style="clear: both; text-align: left;"><code> %MEND; </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;"> %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 /> 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> |
|