DATA Step, Macro, Functions and more

How to put PROC NLMIXED into a FOR LOOP in SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How to put PROC NLMIXED into a FOR LOOP in SAS

[ Edited ]

Hi,

I have a PROC NLMIXED where some parameters are known to take on some values in a pre-specified grids. I want to put this PROC NLMIXED procedure into a FOR LOOP or something similar, where everytime the parameters take the value in the grid, then the PROC NLMIXED is run, then the output (for example, the parameter estimate or the likelihood) are exported. Finally, I want to combine all these outputs together.

I tried searching on the internet but it seems there is no useful source for me.

Let take an example: I use the data set and the code from the website of UCLA: (Link: http://www.ats.ucla.edu/stat/sas/faq/cens_nlmixed.htm).

The code is:

proc nlmixed data=Simu XTOL=1E-12 method=GAUSS qpoints=100;
  parms beta0=7.3 beta1=-.21 beta2=.96 beta3=3
    sigma2_u=.7 sigma2=75;
  bounds sigma2_u sigma2 >= 0;
  pi = constant('pi');
  mu = beta0 + b_0j + beta1*Xc1 + beta2*Xc2 + beta3*Xi1;
  if Ycensr < 16 then 
    ll = (1 / (sqrt(2*pi*sigma2))) * exp( -(Ycensr-mu)**2 / (2*sigma2) );
  if Ycensr >= 16 then 
    ll = 1 - probnorm( (Ycensr - mu) / sqrt(sigma2) );
  L=log(ll);
  model Ycensr ~ general(L);
  random b_0j ~ normal(0, sigma2_u) subject=ID;
  run;
quit;

The data file is put in the attachment for easy following the question.

Suppose now I have known that beta0 is in c(1,2,3,4,5,6,7,8,9,10) and beta1 is in (-2.5, -2.0,-1.5,-0.5,0). I want to put this PROC NLMIXED procedure into a FOR loop and let beta0 and beta1 only receive values from these two sequences.

I have tried macro variables but it seemed that PROC NLMIXED considered beta0 and beta1 as parameters need to be estimated and optimized the likelihood function with respect of these two parameters as well.

How do I do this? Does any one have a good reference?

 

P/s: I use SAS 9.4

Thank you.


Accepted Solutions
Solution
‎07-15-2016 05:33 AM
SAS Super FREQ
Posts: 3,488

Re: How to put PROC NLMIXED into a FOR LOOP in SAS

 

It sounds like you want beta0 and beta1 to be fixed constants, rather than parameters in the problem that need to be optimized. Perhaps this is part of a simulation design? 

 

 

In general the "SAS way" to "run a procedure in a FOR loop" is to use a BY statement. See

Simulation in SAS: The slow way or the BY way

 

It sounds like you want the data to be the same for each run, but only change the values of beta0 and beta1.  (???) In that case, you can write a data set like this:

 

data Want;
set Simu;
do beta0 = 1 to 10;
   do beta1 = -2.5 to 0 by 0.5;
      output;
   end;
end;
run;

proc sort data=Want;
by beta0 beta1;
run;


proc nlmixed data=Want;
by beta0 beta1;
/* remove beta0 and beta1 from PARMS statement */
...
run;

The WANT data set contains multiple copies of your data, each with different values of beta0 and beta1.

If you remove beta0 and beta1 from the PARMS statement, they are treated as constants.

 

The other way to solve this problem is to run the optimization in SAS/IML software, which gives you complete control over loops and parameters.  A relevant articles is Maximum likelihood estimation in SAS/IML

 

If you want a DIFFERENT random sample for each value of beta0 and beta1, then now you are talking about simulating data in SAS. There is a book on simulating data in SAS that contains hundreds of tips and techniques.  You can also search my blog for simulation articles that discuss general simulation techniques in SAS. 

 

View solution in original post


All Replies
Solution
‎07-15-2016 05:33 AM
SAS Super FREQ
Posts: 3,488

Re: How to put PROC NLMIXED into a FOR LOOP in SAS

 

It sounds like you want beta0 and beta1 to be fixed constants, rather than parameters in the problem that need to be optimized. Perhaps this is part of a simulation design? 

 

 

In general the "SAS way" to "run a procedure in a FOR loop" is to use a BY statement. See

Simulation in SAS: The slow way or the BY way

 

It sounds like you want the data to be the same for each run, but only change the values of beta0 and beta1.  (???) In that case, you can write a data set like this:

 

data Want;
set Simu;
do beta0 = 1 to 10;
   do beta1 = -2.5 to 0 by 0.5;
      output;
   end;
end;
run;

proc sort data=Want;
by beta0 beta1;
run;


proc nlmixed data=Want;
by beta0 beta1;
/* remove beta0 and beta1 from PARMS statement */
...
run;

The WANT data set contains multiple copies of your data, each with different values of beta0 and beta1.

If you remove beta0 and beta1 from the PARMS statement, they are treated as constants.

 

The other way to solve this problem is to run the optimization in SAS/IML software, which gives you complete control over loops and parameters.  A relevant articles is Maximum likelihood estimation in SAS/IML

 

If you want a DIFFERENT random sample for each value of beta0 and beta1, then now you are talking about simulating data in SAS. There is a book on simulating data in SAS that contains hundreds of tips and techniques.  You can also search my blog for simulation articles that discuss general simulation techniques in SAS. 

 

Occasional Contributor
Posts: 15

Re: How to put PROC NLMIXED into a FOR LOOP in SAS

Thank you a lot.

That is indeed what I want to do.

Big thanks.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 230 views
  • 1 like
  • 2 in conversation