SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

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

SAS® Abbreviations Are Your Friends; Use a Template Method

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2010-11-4 21:20:43 | 只看该作者

SAS® Abbreviations Are Your Friends; Use a Template Method

From LCChien's blog on blogspot

原文載點:<i><a href="http://support.sas.com/resources/papers/proceedings09/073-2009.pdf">SAS® Abbreviations Are Your Friends; Use a Template Method to Code!</a></i><br /><br />SAS 造成許多初學者學習障礙的主要原因是因為語法很多,對於沒有程式撰寫基礎的人來說,門檻可說是相當的高。坊間有許多不同的 SAS 參考書,但內容程度不一,不過有個共同的特點就是,頁數都很多,內容也很雜。通常參考書一開始會從資料格式與輸入開始教,再來是敘述統計量的輸出方法,還有一些簡單的 Proc 程序,然後是作圖,最後通常會用一些函式和 macro 的介紹。印象中自己也買過兩本中文的 SAS 參考書,但必須承認的一件事是:我從來沒有從頭到尾看完過一遍,通常都是要用到某些語法時才去翻。即便自己對 SAS 已屬稍微熟悉,但仍舊沒有辦法記住所有語法,每隔一段時間,某些語法就會從腦海裡面消失,等到要用的時候再去翻書。後來網路發達了,可以用孤狗去搜尋語法,看似比翻書快了一些,但網路搜尋的結果有時候一開始並不是自己真正想要去尋找的目標,也許需要翻個幾頁,開過幾個連結看一下才能找到真正想要的內容。這種情況所消耗的時間可能跟翻書是差不多的。此外,即便是已經熟悉語法了,但可能因為分析內容的需要,必須大量重複使用一些相當簡單比方說 Proc sort 或 Proc print 等程序。Macro 雖然提供了簡化程式行數的功能,但像 Proc sort 或 Proc print 程序本來就不需要花什麼行數,先宣告 macro 並沒有特別節省掉多少程式碼。如果有一種功能,就是當寫程式寫到一半,知道某個語法但忘記裡面要放什麼參數,SAS 能適時給予提醒,或者是利用更簡短的自訂名稱來取代頻繁使用的簡單程序,便可以讓程式撰寫的過程更省時省力。這種功能在 SAS 裡面,叫做「<b>SAS abbreviation</b>」。<br /><a name='more'></a><br />假設我們要在一個 data step 裡面使用 tranwrd function,結果打到一半.....<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/6830934526.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="164" src="http://easycaptures.com/fs/uploaded/422/6830934526.png" width="640" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />發現忘記 tranwrd function 所要用到的第二個和第三個語法,結果只好趕快去翻書或是求救於孤狗大神。但畢竟這個函式並不是每天在使用,所以隔了一個星期後突然又要用到,又得再一次去翻書或上網搜尋。為了避免這種情況,便可以開啟 SAS Abbreviation的功能,來把這些參數的定義註明起來,若下次再遇到時,SAS 便可以馬上提醒該函式的參數用法。方法如下:<br /><br />首先,點選 SAS 工具列的「Tools」 ,再點選裡面的「Add Abbreviation」:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/9247028027.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/9247028027.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> <br /><br />接著會開啟一個新的對話框:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/7734074180.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/7734074180.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />其中,「Abbreviation」欄位可填上函式名稱 tranwrd,「Text to insert for abbreviation」欄位可填上 tranwrd function 的參數註解。如下所示:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/2899258505.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/2899258505.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />輸入好點選 <b>OK</b> 就大功告成了。<br /><br />下次當你要再次使用 tranwrd function 時,打出 tranwrd 則 SAS Abbreviation 會自動啟動,彈出之前新增過的 tranwrd function 註解,如下圖所示:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/3987783419.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/3987783419.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />如果你是照著 tranwrd function 的格式去加註解,則看到註解跳出來時,可以直接按 <b>Enter</b> 或 <b>Tab</b> 鍵,SAS 會把註解直接輸入到 Editor 裡面。如果你只是要看看,或已經記得了正確完整的寫法,那就可以繼續輸入,不用管拿個字動彈跳出來的註解。<br /><br />若你已經完全記住了這個語法,並且保證永遠都不會忘記,那就可以刪除這個註解。刪除的方法是,一樣到 SAS 工具列點選「<b>Tools</b>」,然後在下拉選單裡面點選「<b>Keyboard Macros</b>」,再點選「<b>Macros...</b>」:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/8958964113.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/8958964113.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />此時會有一個新的對話視窗彈出,可以看到 tranwrd在一個框框裡面,點選 tranwrd 後再按 <b>Delete</b> 按鈕即可刪除。如果想要編輯的話,點 <b>Edit</b> 按鈕即可進行編輯,以下是一個例子。<br /><br />假設我們今天有一個模版程式會經常使用,但還沒有需要用到 macro 來寫,如下所示:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/4672814791.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/4672814791.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />我們也可以使用上述的方法把這個模版寫進 <b>SAS Abbreviation</b> 裡面,並且命名為 <i>DataReport</i>。但用了一陣子之後,發現 PROC REPORT 程序裡面的 Define statement 還滿常用到的,因此想要加入這一行,則一樣操作「Tools -&gt; Keyboard Macros -&gt; Macros -&gt; 選擇 DataReport -&gt; 點 Edit」,之後會彈出下面這個視窗:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/2216263997.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/2216263997.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />視窗下方的 <b>Modify</b> 按鈕會亮起來,點擊下去會彈出第二個視窗:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/7228005714.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/7228005714.png" /></a>&nbsp; <br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />使用者便可以在裡面將 Define的語法輸入到 PROC REPORT 裡面,完畢後按 <b>OK</b> 鍵。此後,當再一次於 Editor 視窗輸入 <i>DataReport</i> 並按下 <b>Enter </b>鍵時,便會出現更新後的模版程式:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/4447993189.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/4447993189.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />如果你經常需要使用到某個 SAS Abbreviation,而且你又是個超級懶人的話,SAS 提供熱鍵功能,讓你免於重複輸入,而只需要按個熱鍵便可把模版整個貼上數次。<br /><br />定義熱鍵的方法,一樣是「Tools -&gt; Keyboard Macros -&gt; Macros」,然後在視窗內選擇想要設定熱鍵的 SAS Abbreviation,接著點擊右邊的「<b>Assign Keys</b>」按鈕,會彈出一個新的視窗:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/8865134339.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/8865134339.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />接著在「<b>Press new shortcut key</b>」底下的空白欄位填上你愛用的熱鍵,如「<b>Alt+E</b>」等等。輸入完畢後,旁邊的「<b>Assign</b>」按鈕會亮起來,點擊下去後,便會看到這個熱鍵出現在上方的「<b>Current keys</b>」的空白欄位裡面。點擊下方的 OK 按鈕,便可以在 Keyboard Macros 視窗裡面看到設定好的熱鍵出現在 Keys 的欄位裡面:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://easycaptures.com/fs/uploaded/422/1185306965.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="http://easycaptures.com/fs/uploaded/422/1185306965.png" /></a><br /></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />關閉這個對話視窗後,該熱鍵就算是正式可以啟用了。<br /><br />原文內還有教導如何在 SAS Enterprise 4.2 裡面使用 SAS Abbreviation 的功能,請有需要的人自行參考原文。<br /><br /><b>CONTACT INFORMATION&nbsp; </b><br />Elizabeth Ceranowski <br />Student Programs Manager <br />SAS Institute Inc. <br />SAS Campus Drive <br />Cary, NC 27513 <br />Work Phone:&nbsp; (919) 531-9347 <br />E-mail: <!-- e --><a href="mailto:Elizabeth.Ceranowski@sas.com">Elizabeth.Ceranowski@sas.com</a><!-- e --><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6268919072942670865-1815028904733537829?l=sugiclub.blogspot.com' alt='' /></div>
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-3 20:09 , Processed in 0.078806 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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