Calcite | Level 5

## Passing a vector or matrix of regressor values into statement: lsmeans/at (x y)=(vector x,y values)

After fitting a model (e.g. proc mixed) I now wish to make predictions of the mean and 95% CI using LSMEANS statement and option AT. The AT option allows many regressors but only 1 value each.

I have made an external dataset of fixed values for regressors.

I would like pass each row of regressor values to the lsmeans/at statement.

Is there any way of getting the lsmeans statement (within proc mixed) to accept the matrix of regressor values?

If not, and I have to (somehow) use a loop, please could you tell or point me to a resource that provides me with code for this complicated task of passing data row-by-row from an external dataset to the scalar lsmeans/at statement.

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Passing a vector or matrix of regressor values into statement: lsmeans/at (x y)=(vector x,y valu

Hello @treetop,

Using CALL EXECUTE you could create one LSMEANS statement per observation in your external dataset.

Example:

``````/* Create test dataset with regressor values */

data regr;
input r s t;
cards;
1.2 0.3 -0.5
1.5 0.7  0.1
;

/* Call PROC MIXED with one LSMEANS statement per obs. in dataset REGR */

data _null_;
set regr end=last;
if _n_=1 then
call execute('proc mixed data=test;'
|| 'class a;'
|| 'model y = a r s t r*s;');
call execute('lsmeans a / cl at (r--t)=('||catx(' ', of r--t)||');');
if last then
call execute('run;');
run;``````

This would create and execute the following PROC MIXED code:

``````proc mixed data=test;class a;model y = a r s t r*s;
lsmeans a / cl at (r--t)=(1.2 0.3 -0.5);
lsmeans a / cl at (r--t)=(1.5 0.7 0.1);
run;``````

Just adapt the PROC MIXED code and the regressor variable names and values to your requirements.

2 REPLIES 2

## Re: Passing a vector or matrix of regressor values into statement: lsmeans/at (x y)=(vector x,y valu

Hello @treetop,

Using CALL EXECUTE you could create one LSMEANS statement per observation in your external dataset.

Example:

``````/* Create test dataset with regressor values */

data regr;
input r s t;
cards;
1.2 0.3 -0.5
1.5 0.7  0.1
;

/* Call PROC MIXED with one LSMEANS statement per obs. in dataset REGR */

data _null_;
set regr end=last;
if _n_=1 then
call execute('proc mixed data=test;'
|| 'class a;'
|| 'model y = a r s t r*s;');
call execute('lsmeans a / cl at (r--t)=('||catx(' ', of r--t)||');');
if last then
call execute('run;');
run;``````

This would create and execute the following PROC MIXED code:

``````proc mixed data=test;class a;model y = a r s t r*s;
lsmeans a / cl at (r--t)=(1.2 0.3 -0.5);
lsmeans a / cl at (r--t)=(1.5 0.7 0.1);
run;``````

Just adapt the PROC MIXED code and the regressor variable names and values to your requirements.

Calcite | Level 5

## Re: Passing a vector or matrix of regressor values into statement: lsmeans/at (x y)=(vector x,y valu

Great thanks to FreelanceReinhard for your help, this worked fantastic!
Discussion stats
• 2 replies
• 1436 views
• 2 likes
• 2 in conversation