LMIXED Procedure

Getting Started: LMIXED Procedure

(View the complete code for this example.)

This example demonstrates how you can perform a mixed model analysis of covariance when you have many groups. Suppose you are an educational researcher who studies how student scores on math tests change over time. Students are tested four times, and you want to estimate the overall rise or fall, accounting for correlation between test response behaviors of students in the same neighborhood and school. One way to model this correlation is by using a random-effects analysis of covariance, where in addition to having a student-specific quadratic mean response function, the scores for students from the same neighborhood and school are all assumed to share the same quadratic mean test response function, whose parameters are random. The following statements simulate data that have this structure; these statements assume that your libref is named mylib, but you can substitute any appropriately defined libref.

Note: Input data must be in a table that is accessible in your session. You can refer to this table by using a two-level name. The first level is a libref, and the second level is the table name. For more information, see the section Using SAS Viya Workbench in Chapter 2, Shared Concepts.

data mylib.SchoolSample;
   do SchoolID = 1 to 1000;
      do nID = 1 to 50;
         Neighborhood = (SchoolID-1)*15 + nId;
         bInt   = 15*ranuni(1);
         bTime  = 15*ranuni(1);
         bTime2 =   ranuni(1);
         do sID = 1 to 2;
            do Time = 1 to 4;
               Math = bInt + bTime*Time + bTime2*Time*Time + rannor(2);
               output;
               end;
            end;
         end;
      end;
run;

These data comes from 1,000 schools and about 15,035 neighborhoods; neighborhoods are associated with more than one school and vice versa. There are 400,000 observations and 50,000 levels of the subject effect.

The following statements use the LMIXED procedure to fit a linear mixed model to these data and produce Figure 1 through Figure 4:

proc lmixed data=mylib.SchoolSample;
   class Neighborhood SchoolID;
   model Math = Time Time*Time / solution;
   random   int Time Time*Time / sub=Neighborhood(SchoolID) type=un;
run;

This model fits a quadratic mean response model that has an unstructured covariance matrix to model the covariance between the random parameters of the response model. With 15,035 neighborhood/school combinations, this model can be computationally daunting to fit, but the LMIXED procedure finishes quickly by multithreaded computing.

Figure 1 displays the "Model Information," "Optimization Information," "Class Level Information," "Number of Observations Information," and "Dimensions" tables.

Figure 1: Mixed Model Analysis of Covariance

The LMIXED Procedure

Model Information
Data SourceSCHOOLSAMPLE
Response VariableMath
Estimation MethodRestricted Maximum Likelihood (REML)
Degrees of Freedom MethodResidual
Design Matrix MethodDense

Optimization Information
Optimization TechniqueNewton-Raphson with Ridging
Hessian in OptimizationExact
Parameters in Optimization6
Lower Boundaries3
Upper Boundaries0
Residual VarianceProfiled
Starting Values FromData

Class Level Information
ClassLevelsValues
Neighborhood150351 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
SchoolID10001 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

Number of Observations Read400000
Number of Observations Used400000

Dimensions
G-side Covariance Parameters6
R-side Covariance Parameters1
Columns in X3
Columns in Z per Subject3
Subjects (Blocks in V)50000


Figure 2 displays the convergence status and the "Covariance Parameter Estimates" table.

Figure 2: Mixed Model Analysis of Covariance

Convergence criterion (ABSGCONV=0.00001) satisfied.

Covariance Parameter Estimates
Cov ParmSubjectEstimate
UN(1,1)Neighborhood(SchoolID)18.7964
UN(2,1)Neighborhood(SchoolID)-0.1215
UN(2,2)Neighborhood(SchoolID)18.9874
UN(3,1)Neighborhood(SchoolID)-0.00044
UN(3,2)Neighborhood(SchoolID)-0.00805
UN(3,3)Neighborhood(SchoolID)0.08394
Residual 0.9998


Figure 3 displays the "Fit Statistics" table.

Figure 3: Mixed Model Analysis of Covariance

Fit Statistics
-2 Res Log Likelihood1681870
AIC (smaller is better)1681884
AICC (smaller is better)1681884
BIC (smaller is better)1681945
CAIC (smaller is better)1681952
HQIC (smaller is better)1681903


Figure 4 displays the "Solution for Fixed Effects" table.

Figure 4: Mixed Model Analysis of Covariance

Solution for Fixed Effects
EffectEstimateStandard
Error
DFt ValuePr > |t|
Intercept7.49610.021294E5352.04<.0001
Time7.49390.021084E5355.55<.0001
Time*Time0.50090.0020444E5245.03<.0001


These estimates are very close to the quadratic coefficients that are used in the DATA step multiplied by 1/2, which is the expected value of a uniform random variable on [0,1].

Last updated: May 14, 2026