2007年2月24日 星期六

A Format to Make the _TYPE_ Field of PROC MEANS Easier to Interpret

原文載點:http://www2.sas.com/proceedings/sugi29/045-29.pdf

在 PROC MEANS 程序所產生的結果中,如果用 output 指令匯出成一個新的資料集,裡面會自動多出一個叫做「_Type_」的變數。這個變數主要是傳遞變數型態。如果只拿一個變數去跑,這個 _Type_ 就變成多餘了。如果使用很多變數,理論上 _Type_ 應該要顯示到底每一個項結果是從哪些變數算出來的,可是 SAS 只給了一些毫無意義的數字來做區別。Matt Pettis 在 2005 年的 SUGI 29 分享了一個 macro 程式,可以賦予 _Type_ 更明確的訊息給使用者。

舉例來說,在一般的情況下使用 PROC MEANS 時,程式碼如下:

PROC MEANS data=work missing noprint;
class Location Directory ResponseCode;
output out=temp;
run;
PROC PRINT data=temp;
Title '_TYPE_ WITHOUT formatting'; run;


這是在計算 Location、Directory 和 ResponseCode 三個變數的含有多少 missing data。列印出來的結果如下:



由上表可知 _Type_ 變數底下都是一些無意義的數字,從 0 到 7。如果改用 Matt 提供的 macro 程式,就可以看出一些和原本不一樣的地方。如下所示:

%TypeFormat(formatname=testtyp,var=Location Directory ResponseCode);
PROC PRINT data=temp;
var _type_ Location Directory ResponseCode _freq_;
format _type_ testtyp.;
title '_TYPE_ WITH formatting'; run;


參數解釋:
formatname=給定 _type_ 變數格式的一個代號,任取
var=所有要用在 PROC MEANS 裡面的變數

輸出報表如下:



可以看到 _Type_ 底下已經出現明確的變數名稱,這樣就比較容易去抓出我們真正要使用到的訊息,看錯行的機會也會減少。

Macro 程式檔可以到原文載點的 Appendix 裡面找到。

CONTACT INFORMATION
Matt Pettis
Thomson West Group
610 Opperman Dr.
Eagan, MN 55417
+1 (651) 848-3976
Matt.pettis@thomson.com

沒有留言:

張貼留言

要問問題的人請在文章下方的intensedebate欄位留言,請勿使用blogger預設的意見表單。今後用blogger意見表單留言的人我就不回應了。