New Contributor
Posts: 4

# How to sum a variable by group when a variable is to be estimated along with other parameters in a proc model?

I want to estimate a system of equations using FIML. There are some variables that need to be summed by group. However, these variables do not exist within the data set and need to be estimated along with other parameters from the model. I would be grateful if you could let me know how to do this.

SAS Employee
Posts: 27

## Re: How to sum a variable by group when a variable is to be estimated along with other parameters in a proc model?

Could you clarify the form of your model regarding the variables which are summed across by groups?  In the case where you'd like to compute an endogenous quantity in the model then estimate its total within each by group you could first assign to a temporary variable, 'ytmp,' then use that variable in an estimated equation. Here's a code snippet which may be a good starting point.  In this example alpha and sum are parameters to be estimated, x is an exogenous variable,  'eq.tot' is the equation to be estimated, and you'd have to pre-caclulate nby, (the number of observations in each by group) in a separate data step.

...

ytmp = alpha*x;

eq.tot = ytmp - sum/nby;

fit tot;

...

New Contributor
Posts: 4

## Re: How to sum a variable by group when a variable is to be estimated along with other parameters in a proc model?

Many thanks,

Here is the model that I'm working with.

proc model data = SS  hessian=cross;

prod = a0 + a1*x1 +  a2*x2 + a3*x1**2 + a4*x2**2;

risk=(x1**b1)*(x2**b2);

g_price = 5;

mu_pi=(g_price*prod – pricex1*x1 – pricex2*x2)*x3;

tmu_pi = sum(mu_pi); the question is here-how to sum mu_pi by hhld id?

sigma_pi =g_price*risk*x3;

tsigma_pi = sum(sigma_pi); how to sum sigma_pi by hhld id?

tsigma2_pi = tsigma_pi**2;

AR = d0 + d1*tmu_pi + 0.5*d2*tmu_pi**2;

DR = -d1 - d2*tmu_pi + AR**2;

eq.techn = y/ risk - prod_fu /risk;

eq.fert=a1 + a3*x1*2 – pricex1/g_price +((-AR*tsigma_pi + 0.5*DR*tsigma2_pi*(d3))/(1 + 0.5*DR*tsigma2_pi))*risk*b1/x1;

eq.herb = a2 + a4*x2*2 – pricex2/g_price + ((-AR*tsigma_pi + 0.5*DR*tsigma2_pi*(d3))/(1 + 0.5*DR*tsigma2_pi))*risk*b2/x2;

endogenous y x1 x2;

fit techn fert herb / fiml;

run;

In the above model, parameters to be estimated are: a0, a1, a2, a3, a4, b1, b2, d0, d1, d2 and d3 and variables that exist within my data are: x1, x2, x3 and y. In my actual model, there are some additional exogenous variables.

SAS Employee
Posts: 27

## Re: How to sum a variable by group when a variable is to be estimated along with other parameters in a proc model?

Your model appears to be sufficiently complex that the example I provided earlier for treating sums of endogenous variables will not work.  Specifically, the fact the summed terms enter into the estimated equations non-linearly precludes this approach for your model.

PROC MODEL estimates parameters for a model by adding the contribution of each observation in a data set to a log likelihood (or other objective) function. In your model the objective function requires the observations in each hhld group to contribute to the objective function in a non-additive way.  For this kind of model and data PROC OPTMODEL is usually a better choice since that procedure allows you to read in all the data at once and then compute your objective function afterwards.  Unfortunately, PROC OPTMODEL does not directly support FIML or any of the other estmation methods available in PROC MODEL.

New Contributor
Posts: 4

## Re: How to sum a variable by group when a variable is to be estimated along with other parameters in a proc model?

Thank you very much!

Discussion stats
• 4 replies
• 587 views
• 0 likes
• 2 in conversation