PROC MIXED中的SAS宏变量

时间:2016-07-19 17:57:13

标签: sas sas-macro mixed

这是我第一次尝试使用SAS宏,我跟随着令人惊叹的加州大学洛杉矶分校统计咨询小组的this page。我对在PROC MIXED中使用宏变量感兴趣,以避免复制和粘贴代码块(我的实际数据集有~400个变量)。

我的例子修改了加州大学洛杉矶分校的例子,让学生在很多学校学习。

data hsb3;
  input  id school female race ses prog
         read write math science socst;
datalines;
1   1   0   4   1   1   57  52  41  47  57
2   1   1   4   2   3   68  59  53  63  61
3   1   0   2   3   1   44  33  54  58  31
4   1   0   4   3   3   63  44  47  53  56
5   1   0   4   2   2   47  51  43  50  61
6   1   1   4   2   2   44  52  51  50  61
7   1   0   3   2   1   50  59  60  56  52
8   1   0   1   2   2   34  46  52  53  57
9   1   0   4   2   2   63  57  51  63  61
19  2   0   3   1   2   57  63  41  63  61
20  2   1   4   2   2   60  57  51  58  31
21  2   0   4   3   2   57  55  51  53  56
22  2   0   4   3   2   73  46  71  50  61
23  2   0   4   2   1   54  65  57  50  61
24  2   1   4   2   2   45  60  50  56  52
25  2   0   3   2   1   42  63  43  53  57
26  2   0   1   1   2   34  57  51  63  61
27  2   0   4   2   2   63  49  60  55  31
10  3   1   3   2   2   57  55  51  55  31
11  3   1   4   3   3   60  46  71  31  56
12  3   1   4   2   2   57  66  57  55  61
13  3   0   3   3   2   50  60  50  31  61
14  3   0   4   3   2   57  57  57  55  46
15  3   0   3   3   3   68  55  50  31  56
16  3   0   4   1   2   34  46  43  50  56
17  3   0   4   3   2   34  65  51  50  56
18  3   0   4   1   2   63  60  60  47  57
28  4   1   3   2   2   57  52  52  53  61
29  4   1   4   2   3   60  57  51  63  61
30  4   1   1   2   2   57  65  51  55  46
31  4   0   4   3   2   73  60  71  31  56
32  4   0   4   3   2   54  63  57  55  46
33  4   0   3   1   2   45  57  50  31  56
34  4   0   1   1   1   42  49  43  50  56
35  4   0   4   3   2   47  52  51  50  56
36  4   0   4   2   1   57  57  60  56  52
;
run;

UCLA示例显示如何使用带有proc reg的宏变量来做几个简单的线性回归模型来预测任何其他变量的阅读得分:

%let indvars = write math female socst;

proc reg data = hsb3;
  model read = &indvars;
run;
quit;

要做到这一点,考虑到学校,我们可以使用PROC MIXED:

proc mixed data = hsb3;
  class school;
  model read = &indvars;
  random school;
run;
quit;

但我真正想做的是看看是否有任何不同性别的分数(仍然考虑到学校)。

%let scores = read write math science socst;

proc mixed data = hsb3;
  class school;
  model &scores = female;
  random school;
run;
quit;

现在我收到错误:

NOTE: The SAS System stopped processing this step because of errors.
167    class school;
168    model &indvars = female;
                      -
                      22
                      200
NOTE: Line generated by the macro variable "INDVARS".
1     write math female socst
            ----
            73
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, (, *, -, /, :, @,
              _CHARACTER_, _CHAR_, _NUMERIC_, |.
ERROR 200-322: The symbol is not recognized and will be ignored.
ERROR 73-322: Expecting an =.

不知何故,宏变量无效。 在PROC MIXED中使用宏变量作为响应变量是否有问题?它们在PROC REG中作为响应变量工作....

proc reg data = hsb3;
  model &scores = female;
run;
quit;

1 个答案:

答案 0 :(得分:0)

您的问题与宏变量或宏代码无关。相反,您没有创建在PROC MIXED中使用的有效MODEL语句。

  

MODEL语句命名一个因变量......

尝试转换数据吗?

%let scores = read write math science socst;
data want ; set hsb3 ;
  array scores &scores ;
  do i=1 to dim(scores);
    score=scores(i);
    name=vname(scores(i));
    output;
  end;
 run;
 proc sort; by name ; run;

 proc mixed data = want;
   by name;
   class school;
   model score = female;
   random school;
 run;