2013-10-02 121 views
1

我想構建一個包含兩個預測器的邏輯模型。一個來自all_indeps1,另一個來自all_indeps2。我在宏下運行,但是,它只運行all_indeps1中的第一個變量和all_indeps2中的所有變量。我應該如何修復這個宏,以便可以從兩組中獲得兩個變量的所有可能組合?SAS宏嵌套循環

另外,我想只輸出邏輯模型中的每個預測變量的p值,任何想法?

非常感謝!

%macro trivariate(all_indeps1, all_indeps2); 
%let k = 1; 
%let l = 1; 
%let indep1 = %scan(&all_indeps1, &k); 
%let indep2 = %scan(&all_indeps2, &l); 

    %do %while("&indep1" NE ""); 
     %do %while ("&indep2" NE ""); 
    title "independent variable is &Indep1 and &Indep2"; 
    proc logistic data = A descending; 
     model Y = &indep1 &indep2; 
    run; 
     %let l = %eval(&l + 1); 
     %let indep2 = %scan(&all_indeps2, &l); 
     %end; 
    %let k = %eval(&k + 1); 
    %let indep1 = %scan(&all_indeps1, &k); 

    %end; 

%修復;

回答

1

我不會將它編碼爲一個宏循環,而是將它設置爲讓你的宏只是內部位,並調用宏n1 * n2次。

假設您有兩個數據集,分別爲indep1indep2,其中每個數據集都包含一列,每列只有一個變量名。然後,如果你有一個宏:

%macro trivariate(indep1,indep2); 
    title "independent variable is &Indep1 and &Indep2"; 
    proc logistic data = A descending; 
     model Y = &indep1 &indep2; 
    run; 
%mend trivariate; 

proc sql; 
select cats('%trivariate(',indep1.var,',',indep2.var,')') into :trivarlist 
    separated by ' ' 
    from indep1, indep2; 
quit; 

&trivarlist.; 

它幾乎總是更容易控制比它裏面的宏語言之外的重複,除了最簡單的情況;這是一個更好的編程風格,因爲它使得更便攜和可重用的代碼。

1

這真的是2個問題。

1.對我來說,沒有什麼會跳出你的宏觀錯誤。嘗試options mprint mlogic;以查看幕後發生的更多情況。

我會親自編寫這是

%macro trivariate(all_indeps1, all_indeps2); 
%let n1 = %sysfunc(countw(&all_indeps1)); 
%let n2 = %sysfunc(countw(&all_indeps2)); 
%do i=1 to &n1; 
    %let indep1 = %scan(&all_indeps1,&i); 
    %do j=1 %to &n2; 
     %let indep2 = %scan(&all_indeps2,&i); 

     STUFF 
    %end 
%end; 
%mend; 

2.Choosing只有1從PROC輸出

。使用 ods trace on;您的程序,然後 ods trace off;這將打印放置到輸出目標的表名。

然後,您可以使用ods select <list of table names>;您的程序,然後ods select default;這將告訴輸出傳送系統(ODS)只打印您要求的表格,然後重置爲默認輸出。 (這個表可能是你的情況下的ParameterEstimates)