SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

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

Practicalities of Using ESTIMATE and CONTRAST Statements

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2011-5-24 04:40:48 | 只看该作者

Practicalities of Using ESTIMATE and CONTRAST Statements

From LCChien's blog on blogspot

原文載點:http://support.sas.com/resources/papers/proceedings10/269-2010.pdf<br /><br />在 PROC GLM 和 PROC MIXED 裡面,最令一般 SAS 使用者感到頭痛的不是模型的檢定而是事後分析(post hoc test)。這牽涉到 contrast 和 estimate 兩個語法。在我接觸過所有非統計科班出身但需要用到線性模型以及事後分析的人,沒有人不對這兩個語法感到聞風喪膽。多年人我曾經試圖用各種方法來教人如何使用這兩種語法,但效果都不怎麼顯著。簡單來講,Contrast 和 estimate 無論是在 PROC GLM 還是 PROC MIXED 裡面,功能完全一樣,語法內的選項也一樣,而兩者之間唯一的差別是,contrast 可以只能算出 p-value,而 estimate 可以把指定的點估計量及其 p-value 一次輸出。David J. Pasta 在 SAS Global Forum 2010 發表了一篇技術文件將這兩種語法做一個詳盡的介紹,堪稱一大貢獻。在本文中,我會介紹幾個比較實用的範例。<br /><a name='more'></a>原文作者套用一個醫學資料並詳述其背景,但為了簡化初學者的時間,我將會以程式來主。第一個範例資料只有三個變數,分別是作為應變數的離散型變數 sex (兩層) 和 race (五層),而依變數則為一個連續型變數 y1。有了對資料的基本概念,首先來看一個最簡單的例子:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class sex;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = sex / solution;<br />&nbsp;&nbsp; &nbsp; &nbsp;lsmeans sex / stderr tdiff e;<br />run;</code></pre>這個例子只單獨分析 sex 對 y1 的影響,而 lsmeans 是用來算出每個性別對 y1 的最小平方估計量。後面的 tdiff 則是去求兩個性別的差異。這段程式會跟用 PROC TTEST 做出來的結果一模一樣。如果不用 lsmeans 而改用 estimate 語法,則可以這樣寫:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class sex;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = sex / solution;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'male' intercept 1 sex 0 1;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female' intercept 1 sex 1 0;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male' sex 1 -1;<br />run;</code></pre>頭兩個 estimate 語法可算出不同性別在 y1 上的平均值,後面接著 intercept 和 sex 是為了要告訴 SAS 估計值是從哪些變數來的。一般使用者最常忘記的就是把 intercept 1 加上去,因為除非在 model 的選項上面加上 noint 抑制掉截距項的計算,否則一切用 estimate 求算的點估計量都要加上 intercept 1。不過你也不用擔心不加會怎麼樣,因為 log 視窗裡面自然會出現紅色的錯誤訊號提醒你加上。而 sex 後面跟著「0 &nbsp;1」和「1 &nbsp;0」則是要準確地將要計算的性別定位出來,當然你也要對自己如何將 sex 編碼要非常了解,而不是亂安插一通。在本例中,女性的編碼順位是排在男生前面(若真的不清楚編碼順位,看一下輸出報表一開始的 Class Level Information 裡面就會清清楚楚地把所有類別變數的編碼順位列出來),所以當 sex 後面寫「1 &nbsp;0」時,自然是去算女生的值,反之則是算男生的值。<br /><br />而當要比較女生和男生的差異時,則有兩點要注意。一是 intercept 可以不用寫了,因為兩兩相減的情況下,截距項自然就被減掉了。二是 sex 後面要改成「1 &nbsp;-1」,很明顯地這就是指用女生的估計值去減男生的估計值。如果要算男生的估計值減女生的估計值,把「1 &nbsp;-1」改成「-1 &nbsp;1」即可。偷懶的方法是不改,但把 output 輸出的估計值變號即可,至於 p-value 是不會改變的。<br /><br />當考慮兩個應變數時,程式如下:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class race sex;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = race sex / solution e;<br />&nbsp;&nbsp; &nbsp; &nbsp;lsmeans race sex / stderr tdiff e;<br />&nbsp;&nbsp; &nbsp; &nbsp;lsmeans race sex / stderr tdiff e om;<br />run;</code></pre>第一個 lsmeans 語法會一次把兩個變數的最小平方估計量通通算出來,並且會把成對比較的結果也列印出來。第二個 lsmeans 語法和第一個的不同是後面多了 om 這個選項。它的功用是去計算不同類別的權重,並在計算平均數的時候考量到權重大小。簡單來講就是去計算加權的估計量。以 sex 為例,在沒有考量加權的情況下,每個 race 的權重都會是 0.2。而考量權重的話,每個 race 的權重則等於每個種族在全部樣本裡面所佔的比例。<br /><br />若用 estimate 語法則程式如下:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class race sex;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = race sex / solution e;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'male' intercept 1 sex 0 1;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female' intercept 1 sex 1 0;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male' sex 1 -1;<br />run;</code></pre>寫法跟第一個程式範例一樣,但我會習慣加上「race 0.2 0.2 0.2 0.2 0.2」,只是說不加的話也沒關係,因為程式會自動用等同的權重去平均。寫到這邊只有兩點要強調:一是類別變數有幾層,estimate 語法內變數後面要跟的數字就有幾個。有人習慣將 1 後面可能還會出現的 0 都省略掉,但我強烈建議補上,因為多寫幾個 0 不會花太多時間,而且有可好處是可以把所有的 estimate 語法排列的工工整整,要 debug 也比較方便。二是腦筋一定要清楚知道變數的編碼順位,在寫之前最好再去對一下 Class Level Information 表格內的訊息。<br /><br />當考慮交互作用項時,estimate 語法就沒有辦法簡化了:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class sex race;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = race sex race*sex / solution;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'male' &nbsp; intercept 1 sex 0 1 race .2 .2 .2 .2 .2 &nbsp;race*sex 0 0 0 0 0 &nbsp;.2 .2 .2 .2 .2;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female' intercept 1 sex 1 0 race .2 .2 .2 .2 .2 &nbsp;race*sex .2 .2 .2 .2 .2 &nbsp;0 0 0 0 0;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male' sex 1 -1 race*sex .2 .2 .2 .2 .2 &nbsp;-.2 -.2 -.2 -.2 -.2;<br />run;</code></pre>在 sex 的點估計中,除了「race 0.2 0.2 0.2 0.2 0.2」一定要加上外,交互作用項 race*sex 後面要寫的數字向量變將整個程式複雜程度提昇了許多。由於 race 有五層,sex 有兩層,所以交互作用項就會是 5X2 = 10 層。因此開始寫這段數字向量時,先要知道到底該寫幾個數字進去,這是第一個要注意的事情。其次,將 sex 的向量和 race 的向量用&nbsp;Kronecker&nbsp;product 的方式去算。如果不清楚&nbsp;Kronecker product,我在這邊提供一個小程式碼:<br /><pre><code>proc iml;<br />&nbsp;&nbsp; &nbsp; &nbsp;a = {0 1};<br />&nbsp;&nbsp; &nbsp; &nbsp;b = {0.2 0.2 0.2 0.2 0.2};<br />&nbsp;&nbsp; &nbsp; &nbsp;ab = a@b;<br />&nbsp;&nbsp; &nbsp; &nbsp;print ab;<br />quit;</code></pre>自行替換 a 和 b 裡面的數字就可以算出兩個變數的交互作用項的向量。然後就直接從 output 視窗裡面複製貼上即可。在成對比較上面,交互作用的數字向量寫法也是一樣可以用這個小程式去算,只要把{0 1}改成{1 -1}即可。但在寫程式時,「race 0.2 0.2 0.2 0.2 0.2」因為在相減的過程中被扣掉了,所以不用寫上去。一個比較白痴的方法是把第一個 estimate 和 第二個 estimate 排好,然後下面減上面,自然就得到最後一個的數字向量。只是當你有大量變數要比較時,這種作法就有點慢了。<br /><br />可是當考慮權重時,程式如下:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class sex race;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = race sex race*sex / solution e;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'male' intercept 1 &nbsp;sex 0 1 &nbsp;race &nbsp;<b><span class="Apple-style-span" style="color: red;">.104 .142 .128 .047 .579</span></b>&nbsp;race*sex 0 0 0 0 0 &nbsp;.104 .142 .128 .047 .579;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female' intercept 1 &nbsp;sex 1 0 race &nbsp;<span class="Apple-style-span" style="color: red;"><b>.104 .142 .128 .047 .579</b></span>&nbsp;race*sex .104 .142 .128 .047 .579 &nbsp;0 0 0 0 0;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male' sex 1 -1 race*sex .104 .142 .128 .047 .579&nbsp;-.104 -.142 -.128 -.047 -.579;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male (off)' sex 1 -1 race*sex <b><span class="Apple-style-span" style="color: blue;">.105 .142 .128 .047 .579&nbsp;-.105 -.142 -.128 -.047 -.579</span></b>;<br />run;</code></pre>紅色部分就是 race 在五個不同總族下的權重,這可以用 PROC FREQ 額外算出來。只要這個部分解決,後面交互作用項就簡單了,因為可以直接套用上面我提供的小程式,之後便是一連串的剪貼。兩兩比較的技巧也如同之前所述。最後一個標記為 'female-male (off)' 的程式,只是要提醒,在計算權重時,我們可能會使用到進位的權重,但進位後可能會造成無法估計的結果。本例中,由於進位的關係,race*sex 後面的數字總和雖然還是零,但是 race 權重的總和變成 1.001 而不是 1.000 了。因此你就會看到一段紅色錯誤訊息出現在 log 視窗告訴你這個事後比較無法估計。解決的方法沒有特別,就是手動去微調裡面的數據,東加一點西減一點,調到總合是 1 即可。<br />有時候事後比較的目的是要看某離散型的變數是否有線性趨勢,則程式如下:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class sex educat;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = sex educat / solution;<br />&nbsp;&nbsp; &nbsp; &nbsp;lsmeans sex educat / stderr e;<br />&nbsp;&nbsp; &nbsp; &nbsp;lsmeans sex educat / stderr e om;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'educat linear (no divisor)' educat -2 -1 0 1 2;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'educat linear (divisor=10)' educat -2 -1 0 1 2 / divisor=10;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'educat linear (divisor=40)' educat -4 -2 0 2 4 / divisor=40;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'educat by year (not centered)' educat 8 12 14 16 20;<br />&nbsp;&nbsp; &nbsp; &nbsp;estimate 'educat by year (centered)' educat -6 -2 0 2 6 / divisor=80;<br />run;</code></pre>此程式把 race 換成 educat,但一樣是五層。線性趨勢的事後檢定所使用的數字向量為「-2 -1 0 1 2」,這個比例大致上是不會改變的,當然不同數目的層次有不同的寫法,請自行孤狗。有時會有比較特殊的寫法,就比方'educat by year (not centered)'的寫法,不過情況比較少。大部分還是會寫成像'educat by year (centered)'所示,也就是讓數字向量內的數字是對秤的,而不是單調遞升或遞減的。至於 divisor 的用法,在此例沒有特殊意義,只是當做是分母把要用來估計的數字向量一次全部除掉,換句話說,以第二個'educat linear (divisor=10)'為例,你也可以寫成<br /><pre><code>estimate 'educat linear (divisor=10)' educat -0.2 -0.1 0 0.1 0.2;</code></pre>但是 divisor 的真正威力,是在下面這個範例:<br /><pre><code>proc glm data=anal;<br />&nbsp;&nbsp; &nbsp; &nbsp;class sex race;<br />&nbsp;&nbsp; &nbsp; &nbsp;model y1 = race sex race*sex / solution e;<br />&nbsp;&nbsp; &nbsp; &nbsp;<span class="Apple-style-span" style="color: #e69138;">estimate 'male (333)' intercept 1 &nbsp;sex 0 1 &nbsp;race &nbsp;.333 .333 .333&nbsp;race*sex 0 0 0 &nbsp;.333 .333 .333;&nbsp;</span><br /><span class="Apple-style-span" style="color: #e69138;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female (333)' intercept 1 &nbsp;sex 1 0 race &nbsp;.333 .333 .333&nbsp;race*sex &nbsp;.333 .333 .333 &nbsp;0 0 0;&nbsp;</span><br /><span class="Apple-style-span" style="color: #e69138;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male (333)' sex 1 -1 race*sex &nbsp;.333 .333 .333 &nbsp;-.333 -.333 -.333;&nbsp;</span><br /><span class="Apple-style-span" style="color: red;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'male (334)' intercept 1 &nbsp;sex 0 1 &nbsp;race &nbsp;.333 .333 .334 race*sex 0 0 0 &nbsp;.333 .333 .334;&nbsp;</span><br /><span class="Apple-style-span" style="color: red;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female (334)' intercept 1 &nbsp;sex 1 0 race &nbsp;.333 .333 .334&nbsp;race*sex &nbsp;.333 .333 .334 &nbsp;0 0 0;&nbsp;</span><br /><span class="Apple-style-span" style="color: red;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male (334)' sex 1 -1 race*sex &nbsp;.333 .333 .334 &nbsp;-.333 -.333 -.334;&nbsp;</span><br />&nbsp;<span class="Apple-style-span" style="color: blue;">&nbsp; &nbsp; &nbsp;estimate 'male (d)' intercept 3 &nbsp;sex 0 3 &nbsp;race &nbsp;1 1 1 &nbsp;race*sex 0 0 0 &nbsp;1 1 1 / divisor=3;&nbsp;</span><br /><span class="Apple-style-span" style="color: blue;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female (d)' intercept 3 &nbsp;sex 3 0 race &nbsp;1 1 1 &nbsp;race*sex 1 1 1 &nbsp;0 0 0 / divisor=3;&nbsp;</span><br /><span class="Apple-style-span" style="color: blue;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male (d)' sex 3 -3 race*sex 1 1 1 &nbsp; -1 -1 -1 / divisor=3;&nbsp;</span><br />&nbsp;&nbsp; &nbsp; &nbsp;<span class="Apple-style-span" style="color: #351c75;">estimate 'male (o)' intercept 849 &nbsp;sex 0 849 &nbsp;race &nbsp;142 128 579&nbsp;race*sex 0 0 0 &nbsp;142 128 579 / divisor=849;&nbsp;</span><br /><span class="Apple-style-span" style="color: #351c75;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female (o)' intercept 849 &nbsp;sex 849 0 race &nbsp;142 128 579&nbsp;race*sex 142 128 579 &nbsp;0 0 0 / divisor=849;&nbsp;</span><br /><span class="Apple-style-span" style="color: #351c75;">&nbsp;&nbsp; &nbsp; &nbsp;estimate 'female-male (o)' sex 849 -849 race*sex 142 128 579 -142 -128 -579 / divisor=849;&nbsp;</span><br />run;</code></pre>此例中,race 不再是五層,而是三層!所以當要計算 sex 內男女分別的點估計量,SAS 裡面是不允許寫分數而是得直接寫小數上去,造成 race 後面的數字向量不能是{1/3 &nbsp;1/3 &nbsp;1/3}而是{0.333 &nbsp;0.333 &nbsp;0.333}。這種情況使得三個數字的總和變成 0.999 而不是 1.000,然後就會看到 SAS 的 log 視窗給你錯誤的訊息了。有一種解決方法是改成{0.333 &nbsp;0.333 &nbsp;0.334},但一旦層數變多了,如七層、九層、十一層、二十一層....,則不但整條 estimate 又臭又長,每次都要去微調裡面的數字也很麻煩,所以此時就要靠 divisor 這個選項幫我們一次除盡所有數。首先,先把原本每個變數後面的數字都乘上 3,接著在最後面加上 / divisor = 3 即可。如果考量到 race 的權重,請回憶國小國中學過的最小公倍數。你可能會問,一對小數點怎麼去求最小公倍數呢?在此額外提供一個 SAS 小程式方便你的計算:<br /><pre><code>data _null_;<br />&nbsp;&nbsp; &nbsp;x = lcm(a1,a2,....,aN);<br />&nbsp;&nbsp; &nbsp;put x;<br />run;</code></pre>把你算出來的權重全部丟到 lcm function 裡面,這個函數顧名思義就是最小公倍數 least common multiple 的縮寫,馬上就可以算出 divisor 該填什麼數字進去了。<br /><br />以上就是很基本但卻很實用的 estimate 寫法。contrast 的用法一模一樣,所以沒有多做介紹。原文後面還有一些更進階的語法,但我個人的經驗是極少數的情況會使用到,因此大家熟練以上的程式就夠了。不過有時候離散變數內的層數很多,在建立 estimate 語法的時候,後面會跟著一長串的數字向量,非常驚人(恐怖)。原文作者建議將一些會一直重複用到的數字向量用 %let 先定義好,等到要用的時候再呼叫即可,比方說:<br /><pre><code>%let S_1 &nbsp; &nbsp;= 1 0 0 0 0 0 0 0 0 0;<br />%let S_2 &nbsp; &nbsp;= 0 1 0 0 0 0 0 0 0 0;<br />%let S_3 &nbsp; &nbsp;= 0 0 1 0 0 0 0 0 0 0;<br />%let S_4 &nbsp; &nbsp;= 0 0 0 1 0 0 0 0 0 0;<br />%let S_5 &nbsp; &nbsp;= 0 0 0 0 1 0 0 0 0 0;<br />%let S_6 &nbsp; &nbsp;= 0 0 0 0 0 1 0 0 0 0;<br />%let S_7 &nbsp; &nbsp;= 0 0 0 0 0 0 1 0 0 0;<br />%let S_8 &nbsp; &nbsp;= 0 0 0 0 0 0 0 1 0 0;<br />%let S_9 &nbsp; &nbsp;= 0 0 0 0 0 0 0 0 1 0;<br />%let S_10 &nbsp; = 0 0 0 0 0 0 0 0 0 1;<br />%let S_none = 0 0 0 0 0 0 0 0 0 0;<br />%let P_U &nbsp; &nbsp;= .1 .1 .1 .1 .1 .1 .1 .1 .1 .1;<br /><span class="Apple-style-span" style="color: red;">%let P_O &nbsp; &nbsp;= 0.0666048524 0.0776077886 0.0888270746&nbsp;</span><br /><span class="Apple-style-span" style="color: red;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.0955339206 0.1017462525 0.1059496214&nbsp;</span><br /><span class="Apple-style-span" style="color: red;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.1124710246 0.1164580436 0.1168907433&nbsp;</span><br /><span class="Apple-style-span" style="color: red;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.1179106784 ;</span><br />&nbsp;&nbsp;*** Intercept at the end of the BEFORE and at the beginning of the AFTER ***;<br />&nbsp;&nbsp;estimate 'LS:1_Before' &nbsp; intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;S_1.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_none.; &nbsp; &nbsp; &nbsp;<br />&nbsp;&nbsp;estimate 'LS:1_After' &nbsp; &nbsp;intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;S_1.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_1.; &nbsp; <br />&nbsp;&nbsp;estimate 'LS:2_Before' &nbsp; intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;S_2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_none.; &nbsp; &nbsp; &nbsp;<br />&nbsp;&nbsp;estimate 'LS:2_After' &nbsp; &nbsp;intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;S_2.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_2.; &nbsp; &nbsp; &nbsp;<br />&nbsp;. . . <br />&nbsp;&nbsp;estimate 'LS:10_Before' &nbsp;intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;S_10. <br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_none.; &nbsp; &nbsp; &nbsp;<br />&nbsp;&nbsp;estimate 'LS:10_After' &nbsp; intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;S_10.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_10.; &nbsp; &nbsp; &nbsp;<br />&nbsp;&nbsp;estimate 'LS:Before_U' &nbsp; intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;P_U.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_none.; <br />&nbsp;&nbsp;estimate 'LS:After_U' &nbsp; &nbsp;intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;P_U.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;P_U.; &nbsp;<br />&nbsp;&nbsp;estimate 'LS:Before_O' &nbsp; intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;P_O.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;S_none.; &nbsp; <br />&nbsp;&nbsp;estimate 'LS:After_O' &nbsp; &nbsp;intercept &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;P_O.<br />&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decile*tafter &nbsp; &nbsp; &nbsp; &nbsp; &amp;P_O.;</code></pre>試想,如果不用 %let 去額外定義巨集變數,而把那些 0 和 1 或 -1 等數字全部填回到 estimate 後面,則整串程式碼絕對會讓人暈眩。因此,我強烈建議使用 %let 來定義好 estimate 後面該用到的數字向量。此外,當要考量權重時,雖然可以自行算好寫上,但當層數多的時候(如本例),一個個複製貼上也是很麻煩,所以原文作者提供了一段程式碼讓 SAS 自動幫你把把這些權重貼到巨集變數裡面。<br /><pre><code>proc sort data=temp01(keep=patid_age decile) out=temp02 nodupkey;<br />&nbsp;&nbsp;by patid_age decile;<br />run;<br />data _null_;<br />&nbsp;&nbsp;set temp02;<br />&nbsp;&nbsp;by patid_age decile;<br />&nbsp;&nbsp;if not(first.patid_age and last.patid_age) <br />then error 'ERROR: decile varies within patid_age';<br />run;<br />ods listing close;<br />ods output onewayfreqs = temp03(keep=decile frequency cumfrequency);<br />proc freq data = temp02;<br />&nbsp;&nbsp;tables decile;<br />run;<br />ods listing;<br />data _null_;<br />&nbsp;&nbsp;set temp03;<br />&nbsp;&nbsp;call symput('N_'||left(trim(put(decile,2.))),frequency);<br />run;<br /><br />data _null_;<br />&nbsp;&nbsp;set temp03;<br />&nbsp;&nbsp;if (decile eq 10) then call symput('Ntot',cumfrequency);<br />run;<br />data _null_;<br />&nbsp;&nbsp;call symput('P_1', &amp;N_1. / &amp;Ntot.));<br />&nbsp;&nbsp;call symput('P_2', &amp;N_2. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_3', &amp;N_3. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_4', &amp;N_4. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_5', &amp;N_5. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_6', &amp;N_6. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_7', &amp;N_7. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_8', &amp;N_8. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_9', &amp;N_9. / &amp;Ntot.);<br />&nbsp;&nbsp;call symput('P_10', &amp;N_10. / &amp;Ntot.);<br />run;<br />*** Macro variable for model specifications ***;<br />%let P_O = &amp;P_1. &amp;P_2. &amp;P_3. &amp;P_4. &amp;P_5. &amp;P_6. &amp;P_7. &amp;P_8. &amp;P_9. &amp;P_10.;</code></pre>這段程式碼簡而言之,就是先用 PROC FREQ 把次數分配表的結果算出來,然後把這必要的變數(也就是 frequency 和 cumfrequency)存出來計算每一層的比例,再把這比例結果用 symput 函數存成不同的巨集變數,然後一次丟進 %let 裡面整合成一個巨集變數供 estimate 語法使用。不過我覺得原文作者寫的這段程式稍嫌複雜些,可能因為他用的資料結構比較複雜的因素。在一般的情況下,用 PROC FREQ 可以立刻求出類別變數內各層的比例:<br /><pre><code>proc freq data=xxx;<br />&nbsp;&nbsp; &nbsp; &nbsp;tables varcat;<br />&nbsp;&nbsp; &nbsp; &nbsp;ods output onewayfreqs = temp(keep=<b><span class="Apple-style-span" style="color: red;">percent</span></b>);<br />run;</code></pre>用 ods output 另存的資料裡面有個 percent 變數就是每一層的比例。然後用 proc transpose 轉置:<br /><pre><code>proc transpose data=temp out=temp;<br />&nbsp;&nbsp; &nbsp; &nbsp;var percent;<br />run;</code></pre>假設有十層的話,轉置後的 percent 會被存在 COL1~COL10 裡面。然後用 symput 函數分別將他們存成不同的巨集變數:<br /><pre><code>data _null_;<br />&nbsp;&nbsp; &nbsp; &nbsp;set temp;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;call symput('P_1', COL1);<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;call symput('P_2', COL2);<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;call symput('P_3', COL3);<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;call symput('P_4', COL4);<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;call symput('P_5', COL5);<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;call symput('P_6', COL6);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;call symput('P_7', COL7);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;call symput('P_8', COL8);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;call symput('P_9', COL9);<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;call symput('P_10', COL10); <br />run;</code></pre>最後再全部丟到 %let 後面用一個巨集變數整合他們:<br /><pre><code>%let P = &amp;P_1 &amp;P_2 &amp;P_3;</code></pre>如此一來,在寫 estimate 時需要用到這串數字向量時,輕鬆填上&amp;P 即可!<br /><br /><b>CONTACT INFORMATION&nbsp;</b><br />Your comments and questions are valued and encouraged. &nbsp;Contact the author at:<br />&nbsp;David J. Pasta <br />&nbsp;Vice President, Statistics &amp; Data Operations<br />ICON Clinical Research <br />&nbsp;188 Embarcadero, Suite 200<br />&nbsp;San Francisco, CA 94105<br />&nbsp;+1.415.371.2111 &nbsp; <br />&nbsp;david.pasta@iconplc.com <br />&nbsp;www.iconplc.com<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6268919072942670865-3555906576014195085?l=sugiclub.blogspot.com' alt='' /></div>
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 12:58 , Processed in 0.122523 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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