2014-09-26 34 views
1

我有一個包含n個級別的id和(n + 4)變量的數據集。我希望使用n-1個變量的值作爲解釋變量對分類變量的n個層次中的每一個層次進行迴歸。這是我的數據集:SAS運行多個迴歸並收集結果

data have; 
    input s id $ x z y00 y01 y02; 
cards; 
1 00 95 5.00 .02 .43 .33 
2 00 100 5.50 .01 .44 .75 
3 00 110 5.25 .10 .37 .34 
4 00 97 5.00 .02 .43 .33 
5 00 100 5.50 .01 .43 .75 
6 00 120 5.25 .10 .38 .47 
7 00 95 5.00 .02 .43 .35 
8 00 130 5.50 .01 .44 .75 
9 00 110 5.25 .10 .39 .44 
10 00 85 5.00 .02 .43 .33 
11 00 110 5.50 .01 .47 .78 
12 00 110 5.25 .10 .37 .44 
1 01 20 6.00 .22 .01 .66 
2 01 25 5.95 .43 .10 .20 
3 01 70 4.50 .88 .05 .17 
1 02 80 2.50 .65 .33 .03 
2 02 85 3.25 .55 .47 .04 
3 02 90 2.75 .77 .55 .01 
; 
run; 

所以我希望用Z,Y01,Y02和解釋X的ID 00.同樣,Z,Y00,Y02和將解釋x對於ID 01.最後,Z,Y00 ,並且y01將解釋x爲ID 02.

我只能使用'BY'語句,但我想不出如何告訴模型忽略與我目前使用的ID相同前綴的變量與...合作。

我可以創建單獨的數據集,但對於其中一些分析,n> 100。

理想情況下,我會爲每個ID運行一個proc mixed和proc reg,如上所述,併爲每個ID都設置一個數據集。

任何想法?

proc mixed data=have(where=(id='00')) plots(only)=all method=REML nobound ; 
    class s; 
    model x=z y01 y02 
    /solution; 
    random z y01 y02; 
run; 


proc reg data=have(where=(id='00')); 
    model x=z y01 y02; 
run; 

謝謝。

+0

您可以用宏解決方案反覆運行分析不會產生不同的數據集(一對飛凡和MODEL)解決這個問題,但我想看看是否有人知道更好的解決方案;重複的宏觀解決方案將是時間和磁盤密集型 – Joe 2014-09-26 14:44:54

回答

2

不幸的是,我不知道有什麼辦法可以做到這一點,但是這裏有兩種可能的方法讓人想起。

選項1.將所需的獨立變量複製到新變量中。

/* Count the number of y variables */ 
proc sql noprint; 
    select max(input(id, best.)) + 1 
    into :dimY 
    from have; 
quit; 

data alsoHave; 
    set have; 
    /* Create an array for indexing the y variables */ 
    array y[&dimY.] y:; 
    /* Create new variables to contain y values */ 
    array newy[%eval(&dimY.-1)]; 
    _j = 1; 
    do _i = 1 to &dimY.; 
     /* Put each y value in a newy variable where it isn't y_id */ 
     if input(id, best.) + 1 ~= _i then do; 
      newy[_j] = y[_i]; 
      _j + 1; 
     end; 
    end; 
    drop _:; 
run; 

proc reg data = alsoHave; 
    by id; 
    model x = z newy:; 
run; 

選項2.將不需要的變量的方差降低爲0,以便它們不會影響迴歸。

data alsoHave; 
    set have; 
    /* Create an array for indexing the y variables */ 
    array y[*] y:; 
    _i = input(id, best.) + 1; 
    backUp = y[_i]; 
    /* Overwrite the unwanted variables with 0 */ 
    y[_i] = 0; 
    drop _:; 
run; 

proc reg data = alsoHave; 
    by id; 
    model x = z y:; 
run; 

我更喜歡選項2的簡單性,但數組編程很有趣,所以我總是包括選項1。

編輯:下面是Option 2的id不可知的版本,不需要連續的整數。有趣的函數是dim(),它返回數組中的變量數量,vname()返回數組和索引中的變量名稱。最後compress()k(保留)和d(數字)選項一起使用。類似的變化可能對選項進行1

data alsoHave; 
    set have; 
    /* Create an array for indexing the y variables */ 
    array y[*] y:; 
    /* Loop through the y variables */ 
    do _i = 1 to dim(y); 
     /* Replace with 0 when the variable name matches the id */ 
     if input(compress(vname(y[_i]), , "dk"), best.) = input(id, best.) then y[_i] = 0; 
    end; 
    drop _:; 
run; 
+0

我的錯誤是沒有提到id不會始終是連續的。也許一個迴歸的ID是01,25,145等。任何想法如何改變你的解決方案來處理這個,而不需要爲ID創建一個虛擬名稱?否則,很好的解決方案 – pyll 2014-09-26 22:38:58

+1

我已經添加了第三個應該滿足您需求的示例。如果您希望做大量的SAS編程陣列,那麼這是一個非常有用的工具,值得[研究](http://support.sas.com/resources/papers/97529_Using_Arrays_in_SAS_Programming.pdf)。 – SRSwift 2014-09-27 10:38:30

+0

這非常有幫助。謝謝。我絕對需要在我的數組編程上工作。選項2和3非常聰明,但我認爲我更喜歡選項1.我在選項3中使用了您的建議來更改選項1,但仍然存在問題:我失去了與每個變量相對應的後綴。所以這段代碼會生成newy1和newy2,這對於id = 00是非常好的。但是,當id = 01時,變量y00被命名爲newy1。所以我在結果中失去了解釋,因爲newy並不總是對應於我想要的解釋變量。我猜想選項3在這裏效果最好。謝謝您的幫助。 – pyll 2014-09-29 12:38:55