0

This is my first foray into using SAS macros, and I'm following this page from the amazing UCLA Stats Consulting Group. I'm interested in using macro variables in PROC MIXED to avoid copying and pasting blocks of code (my actual data set has ~400 variables).

My example modifies the UCLA example to have students in many schools.

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;

The UCLA example shows how to use macro variables with proc reg to do several simple linear regression models to predict reading score with any of the other variables:

%let indvars = write math female socst;

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

To do this taking school into account, we can use PROC MIXED instead:

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

But what I really want to do is to see if any of the scores differ by gender (still taking school into account).

%let scores = read write math science socst;

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

Now I get the error:

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 =.

Somehow the macro variable is not working. Is there a problem with using macro variables as a response variable in PROC MIXED? They work as a response variable in PROC REG....

proc reg data = hsb3;
  model &scores = female;
run;
quit;
emudrak
  • 789
  • 8
  • 25

1 Answers1

0

Your problem doesn't have anything to do with macro variables or macro code. Instead you are not creating a valid MODEL statement to use in PROC MIXED.

The MODEL statement names a single dependent variable ...

Try transforming the data perhaps?

%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;
Tom
  • 47,574
  • 2
  • 16
  • 29
  • That's why I was goign for the macro variables. When I use them in proc reg (last code block), it does five regressions with one response each (the five macro variables in turn), not a single multivariate regression with five response variables. I was looking for that to happen with PROC MIXED. Can't we use macro variables with it to get five mixed models, each with a different single response variable? Without writing the code block over 5 times? – emudrak Jul 19 '16 at 20:25
  • Not according to the documentation. You would need to generate mulitple PROC MIXED steps. You could transform the data so that each variable's value appears on a separate observations and then use a BY statement. Someone that understands MIXED better might know another method. – Tom Jul 19 '16 at 20:34
  • From the documentation Tom cited, it looks like no, the PROC MIXED model statement doesn't allow a variable list for the dependent variable. I would start by writing the SAS code to make run 3 models as you would want. And get that to be working SAS code. Then you can likely use the macro language to generate the code for those 3 models (or 300). Or Tom's BY statement approach is probably better. If you post the SAS code you want to generate as part of your question, people can help you better. Note that all the macro language does is generate SAS code – Quentin Jul 19 '16 at 20:39