BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
john1111
Obsidian | Level 7
I am writing a Proc nlmixed code and for my likelihood function I want to used some values generated from a proc iml procedure. So I am wondering if there is a way of using proc iml inside a proc nlmixed code.
I have read from support sas and I see I am being advised to use proc fcmp......Could someone give me an example if possible.

proc nlmixed data = xxx;
parms b0=0 b1=0;
mu = exp(b0 + b1*Age);
ll = log(((mu**y)*exp(-mu))/gamma(y+1));
model y~ general(ll);
run;

proc iml;
v = {5,6,7,8,9,10,11,12,13,14};
z = j(10,1,.);
do i = 1 to 6;
z[i] = ((v[i]-5)/5)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
end;
ll=log(sum(z));
quit;

The idea is to use mu from the first proc step inside the second proc step but the ll from both steps should be inside nlmixed
1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Your 'proc iml' part does not contain any matrix operations, so you can implement it by using the programming statements that are supported by PROC NLMIXED. You can use the ARRAY statement to define the v array. The PROC NLMIXED syntax also supports a DO loop. Within a DO loop, you can accumulate the sum of z. You do not need z to be an array in order to compute sum(z).It might look something like this (untested)

 


proc nlmixed data = Work.final ;
  array v[10];
  do i = 1 to 10;
     v[i] = i + 4;
   end;
  parms b0=0 b1=0;
  mu = exp(b0 + b1*Age);
  if SMD650 ~= 10 then
  ll = log(((mu**SMD650)*exp(-mu))/gamma(SMD650+1));
  else do;
     sum = 0;
     do i = 1 to 6;
      sum = sum + ((v[i]-5)/5)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
     end;
     do i = 7 to 10;
      sum = sum + ((v[i]-4)/4)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
     end;
     ll=log(sum);
  end;
  
  model SMD650 ~ general(ll);
run;	

 

 

View solution in original post

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

No, its not possible.  From the proc iml you would save the output data;

https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html

 

You would then use this output data, or merge it with other data and feed that into your proc mixed.

Ksharp
Super User

You could use proc nlmixed insider proc iml.

 

proc iml;

.........

submit ;

proc nlmixed;

......

endsubmit;

 

quit;

 

calling @Rick_SAS

john1111
Obsidian | Level 7
Its the other way round. proc iml inside proc nlmixed
john1111
Obsidian | Level 7
@RW9 executing proc iml alone is not possible because it uses some data from proc nlmixed.....i.e. mu from the above code.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Then put an out= to create a dataset from the proc nlmixed and use that in your iml.

john1111
Obsidian | Level 7

This is what I have

 

 

proc nlmixed data = Work.final ;
  parms b0=0 b1=0;
  mu = exp(b0 + b1*Age);
  if SMD650 ~= 10 then
  ll = log(((mu**SMD650)*exp(-mu))/gamma(SMD650+1));
  else
  proc iml;
  v = {5,6,7,8,9,10,11,12,13,14};
  z = j(10,1,.);
  do i = 1 to 6;
   z[i] = ((v[i]-5)/5)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
  end;
  do i = 7 to 10;
   z[i] = ((v[i]-4)/4)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
  end;
  ll=log(sum(z));
  
  model SMD650 ~ general(ll);
run;	

 

In other words the second last line (model SND50...) is part of the proc nlmixed, the if statement is also part of proc nlmixed. but the else part will have to go through the proc iml. So they are intertwined in that I can not create out= because the proc nlmixed will not be complete without proc iml.

Rick_SAS
SAS Super FREQ

Your 'proc iml' part does not contain any matrix operations, so you can implement it by using the programming statements that are supported by PROC NLMIXED. You can use the ARRAY statement to define the v array. The PROC NLMIXED syntax also supports a DO loop. Within a DO loop, you can accumulate the sum of z. You do not need z to be an array in order to compute sum(z).It might look something like this (untested)

 


proc nlmixed data = Work.final ;
  array v[10];
  do i = 1 to 10;
     v[i] = i + 4;
   end;
  parms b0=0 b1=0;
  mu = exp(b0 + b1*Age);
  if SMD650 ~= 10 then
  ll = log(((mu**SMD650)*exp(-mu))/gamma(SMD650+1));
  else do;
     sum = 0;
     do i = 1 to 6;
      sum = sum + ((v[i]-5)/5)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
     end;
     do i = 7 to 10;
      sum = sum + ((v[i]-4)/4)*((mu**v[i])*exp(-mu))/gamma(v[i]+1);
     end;
     ll=log(sum);
  end;
  
  model SMD650 ~ general(ll);
run;	

 

 

john1111
Obsidian | Level 7

Thanks a lot! this works perfectly but I can not figure out how I would add another else statement;  I mean if I have the following; How would I rearrange the commented section, with an inclusion array variable n.

 

proc nlmixed data = Work.final ;
  array v[10];
  do i = 1 to 10;
     v[i] = i + 4;
   end;
  parms b0=0 b1=0;
  mu = exp(b0 + b1*Age);
  if SMD650 ~= 10 and SMD650 ~=20 then
  ll = log(((mu**SMD650)*exp(-mu))/gamma(SMD650+1));


/**********
else if SMD650= 20 then
p=0;
  do n = 15 to 20;
      p = p+((n[i]-15)/5)*((mu**n[i])*exp(-mu))/gamma(n[i]+1);
  end;
  do n= 21 to 24;
      p = p+((24-n[i])/4)*((mu**n[i])*exp(-mu))/gamma(n[i]+1);
  end;
ll=log(p)
*********/

else do; sum = 0; do i = 1 to 6; sum = sum + ((v[i]-5)/5)*((mu**v[i])*exp(-mu))/gamma(v[i]+1); end; do i = 7 to 10; sum = sum + ((v[i]-4)/4)*((mu**v[i])*exp(-mu))/gamma(v[i]+1); end; ll=log(sum); end; model SMD650 ~ general(ll); run;

 

Rick_SAS
SAS Super FREQ

You must use a DO/END block with the IF-THEN/ELSE statement if you want the body of the IF/THEN to contain more than one statement. Notice how I used DO/END in my example. In your example:

 

else if SMD650= 20 then DO;
   p=0; 
   /* put more statements here */
END;
else  DO;
   /* put more statements here */
END; 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2718 views
  • 2 likes
  • 4 in conversation