SAS宏只能使用一个变量,而不能使用另一个变量。群集2-ols宏错误:(执行)无效的参数

时间:2018-08-13 02:00:56

标签: sas sas-iml

这是我的宏:

%MACRO clus2OLS(yvar, xvars, cluster1, cluster2, dset=);
    /* do interesection cluster*/
    proc surveyreg data=&dset; cluster &cluster1 &cluster2; model &yvar= &xvars /  covb; ods output CovB = CovI; quit;
    /* Do first cluster */
    proc surveyreg data=&dset; cluster &cluster1; model &yvar= &xvars /  covb; ods output CovB = Cov1; quit;
    /* Do second cluster */
    proc surveyreg data=&dset; cluster &cluster2; model &yvar= &xvars /  covb; ods output CovB = Cov2 ParameterEstimates = params;  quit;

    /*  Now get the covariances numbers created above. Calc coefs, SEs, t-stats, p-vals using COV = COV1 + COV2 - COVI*/
    proc iml; reset noprint; use params;
        read all var{Parameter} into varnames;
        read all var _all_ into b;
        use Cov1; read all var _num_ into x1;
        use Cov2; read all var _num_ into x2;
        use CovI; read all var _num_ into x3;

        cov = x1 + x2 - x3; /* Calculate covariance matrix */
        dfe = b[1,3]; stdb = sqrt(vecdiag(cov)); beta = b[,1]; t = beta/stdb; prob = 1-probf(t#t,1,dfe); /* Calc stats */

        print,"Parameter estimates",,varnames beta[format=8.4] stdb[format=8.4] t[format=8.4] prob[format=8.4];

          conc =    beta || stdb || t || prob;
          cname = {"estimates" "stderror" "tstat" "pvalue"};
          create clus2dstats from conc [ colname=cname ];
          append from conc;

          conc =   varnames;
          cname = {"varnames"};
          create names from conc [ colname=cname ];
          append from conc;
    quit;

    data clus2dstats; merge names clus2dstats; run;
%MEND clus2OLS;  

这是我的宏调用:

*call cluster 2-ols macro for first chgroa1 model;
%clus2OLs(yvar=Chgroa3, xvars=vb_nvb roa chgroa GrAS, cluster1=gvkey, cluster2=fyear, dset=Reg_ROA);
*set up macro for second chgroa1 model;
%clus2OLS(yvar=Chgroa3, xvars=SERIAL roa chgroa GrAS, cluster1=gvkey, cluster2=fyear, dset=Reg_ROA);
*set up macro for third chgroa1 model;
%clus2OLS(yvar=Chgroa3, xvars=recyc_V roa chgroa GrAS, cluster1=gvkey, cluster2=fyear, dset=Reg_ROA);
*set up macro for fourth chgroa1 model;  

我使用类似的代码,唯一的区别是yvar=Chgroa3。当我使用yvar=Chgroa1时,它可以工作。否则我会收到错误消息

NOTE: IML Ready
ERROR: (execution) Invalid argument to function.

 operation : SQRT at line 4121 column 1
 operands  : _TEM1001

_TEM1001      5 rows      1 col     (numeric)

 0.0002809
 0.0005076
 0.0112643
  -0.00117
 0.0018209

 statement : ASSIGN at line 4121 column 1
ERROR: (execution) Matrix has not been set to a value.

 operation : / at line 4121 column 1
 operands  : beta, stdb

beta      5 rows      1 col     (numeric)

 -0.026229
 -0.018565
 -0.484585
 -0.086641
 -0.052028

stdb      0 row       0 col     (type ?, size 0)


 statement : ASSIGN at line 4121 column 1
ERROR: (execution) Matrix has not been set to a value.

1 个答案:

答案 0 :(得分:1)

正如@Quentin在评论中指出的那样,您正在尝试取负数的平方根。参见打印到日志的临时矩阵的第四行;这是负面的。 (请注意,变量本身可以为负数,但协方差不能为负。)

请参阅以下具有相同错误的示例代码:

13   proc iml;
NOTE: IML Ready
14     x = {1 2 -3};
15     y = sqrt(x);
ERROR: (execution) Invalid argument to function.

 operation : SQRT at line 15 column 11
 operands  : x

x      1 row       3 cols    (numeric)

         1         2        -3

 statement : ASSIGN at line 15 column 3
16   quit;

我对那个特定的方程式不熟悉,但是您应该验证Cov1+Cov2-CovI在逻辑上是否可以为负;对我来说似乎不应该。