2013-10-16 23 views
0

我试图在循环中运行PROC GLM,因为我有很多模型(依赖变量和独立变量的不同组合),并且它们非常耗时地逐个运行它们。但是在PROC GLM中,日志错误表示only one MODEL statement allowed,所以对此有何解决方案?SAS PROC GLM in a loop

我的代码看起来像下面

data old; 
    input year A1 A2 A3 A4 B C D; 
datalines; 
2000 22 22 30 37 4 13 14 
2000 37 29 31 38 6 16 12 
2000 42 29 34 37 3 15 15 
2000 28 28 27 35 10 13 15 
2000 33 22 37 40 9 12 15 
2000 22 29 26 40 3 11 15 
2000 37 20 32 40 6 12 13 
2001 44 22 33 35 7 20 12 
2001 33 20 26 40 6 13 15 
2001 32 30 37 35 1 12 13 
2001 44 25 31 39 4 20 14 
2001 25 30 37 38 4 20 10 
2001 43 21 35 38 6 11 10 
2001 25 23 34 37 5 17 11 
2001 45 30 35 37 1 13 14 
2001 48 24 36 39 2 13 15 
2001 25 24 35 40 9 16 11 
2002 38 26 33 35 2 14 10 
2002 29 24 35 36 1 16 13 
2002 34 28 32 35 9 16 11 
2002 45 26 29 35 9 19 10 
2002 26 22 38 35 1 14 12 
2002 20 26 26 39 8 17 10 
2002 33 20 35 37 9 18 12 
; 
run; 

    %macro regression (in, YLIST,XLIST); 
    %local NYLIST; 
    %let NYLIST=%sysfunc(countw(&YLIST)); 

    ods tagsets.ExcelXP path='D:\REG' file='Regression.xls'; 

    Proc GLM data=∈ class year;  

     %do i=1 %to &NYLIST; 
      Model %scan(&YLIST,&i)=%scan(&XLIST,1) %scan(&XLIST,2)/ solution; 
      Model %scan(&YLIST,&i)=%scan(&XLIST,1) %scan(&XLIST,2)/ solution; 
      Model %scan(&YLIST,&i)=%scan(&XLIST,1) %scan(&XLIST,2)/ solution; 
     %end; 

     %do i=2 %to &NYLIST; 
      Model %scan(&YLIST,&i)=%scan(&XLIST,1) %scan(&XLIST,2) %scan(&XLIST,3)/ solution; 
      Model %scan(&YLIST,&i)=%scan(&XLIST,1) %scan(&XLIST,2) %scan(&XLIST,3)/ solution; 
      Model %scan(&YLIST,&i)=%scan(&XLIST,1) %scan(&XLIST,2) %scan(&XLIST,3)/ solution; 
     %end; 

    run; 

    ods tagsets.excelxp close; 

    %mend regression; 
    options mprint; 
    %regression 
    (in=old 
    ,YLIST= A1 A2 A3 A4 
    ,XLIST= B C D); 

/*potential solutions*/ 

    %macro regression(data,y,x1,x2,x3); 
    proc glm data=&data; 
    class year; 
    model &y=&x1 &x2 &x3/solution; 
    run; 
    %mend regression; 


    %macro sql (mydataset,y,x1,x2,x3); 
    proc sql noprint; 

    select cats('%regression(&mydataset,',&y,',',&x1,',',&x2,',',&x3,')') 
    into :calllist separated by ' ' from &mydataset; 
    quit; 
    &calllist.; 

    %mend sql; 
    %sql 
    (mydataset=old 
    ,y=A1 
    ,X1=B 
    ,X2=C 
    ,X3=D); 

回答

1

你应该这样做的两个步骤。一个是包含PROC GLM的一个实例宏:

%macro regression(data,y,x1,x2,x3); 
proc glm data=&data; 
class year; 
model &y &x1 &x2 &x3/solution; 
run; 
%mend regression; 

然后调用从别的,宏,无论是与循环元素更好的宏观,或从包含您的Y/X1/X2的数据集/ x3作为列(每个模型语句一行),使用call executeproc sql select into方法。例如,与数据设定modeldata含有Y/X值模型:

proc sql noprint; 
select cats('%regression(mydataset,',y,',',x1,',',x2,',',x3,')') into :calllist separated by ' ' from modeldata; 
quit; 
&calllist.; 

或类似物。

+0

谢谢乔。有没有一种方法可以在'%macro regression'中隐式定义'x'参数的数量?我的'xi'并不总是3(有时会达到10 ...)。 –

+0

你可以做几件事情。你可以将'x'作为一个字符串传递(它毕竟被用作一个字符串 - 所有的参数都被一个空格分开)。你可以通过10并传递任何空缺的空格。您可以传递参数的总数并遍历该列表。 – Joe

+0

好的......有一个问题,mydataset和modeldata有什么区别? –