Hello everyone,
I am seeking help to write a macro using proc nlmixed with multiple terms.
I want to define the model in proc nlmixed with multiple dummies (let say D1, D2, D3, D4, D5) and the interaction terms between variable X with each of those dummy variables.
The code can be written normally as:
proc nlmixed data=have;
eta = int + a1*D1 + a2* D2 + a3*D3 + a4*D4 + a5*D5 + b1*D1*X + b2*D2*X + b3*D3*X + b4*D4*X + b5*D5*X;
......
run;
My actual model is much longer than that since I have up to 9 dummies and 2 variables X. In total, I will need to specify around 30 terms in my model. Is there way to make my model specification shorter which allows me to modify the model easily?
Thank you so much in advance.
Windy.
Hello @windy,
You could write a little utility macro like this (just an example -- feel free to modify it)
%macro terms(pattern,n);
%local i;
%do i=1 %to &n;
+ %sysfunc(tranwrd(&pattern,#,&i))
%end;
%mend terms;
and then write the model like this:
eta = int %terms(a#*D#,5) %terms(b#*D#*X,5);
That is, the wildcard "#" in the first argument of the macro is replaced by numbers 1, 2, ..., n (n=second argument of the macro) and the resulting model terms are concatenated with " + ".
Hello @windy,
You could write a little utility macro like this (just an example -- feel free to modify it)
%macro terms(pattern,n);
%local i;
%do i=1 %to &n;
+ %sysfunc(tranwrd(&pattern,#,&i))
%end;
%mend terms;
and then write the model like this:
eta = int %terms(a#*D#,5) %terms(b#*D#*X,5);
That is, the wildcard "#" in the first argument of the macro is replaced by numbers 1, 2, ..., n (n=second argument of the macro) and the resulting model terms are concatenated with " + ".
Hi @FreelanceReinh ,
This is working beautifully. Thank you so much for helping me again. I really appreciate it.
Sorry for bothering you again @FreelanceReinh
What should I do if I want to remove the + sign?
The plus sign is just text in the macro. If you remove it,
%macro terms(pattern,n);
%local i;
%do i=1 %to &n;
%sysfunc(tranwrd(&pattern,#,&i))
%end;
%mend terms;
the result will look something like this (log excerpt)
99 %put eta = int %terms(a#*D#,5) %terms(b#*D#*X,5); eta = int a1*D1 a2*D2 a3*D3 a4*D4 a5*D5 b1*D1*X b2*D2*X b3*D3*X b4*D4*X b5*D5*X
I saw it. By the way, I need the + sign there though.
I was thinking that I can remove the + sign in the first macro and use it later in eta, but the code is not resolved.
Thank you again for your support.
@windy wrote:
I was thinking that I can remove the + sign in the first macro and use it later in eta, ...
This is actually a good idea because it makes the macro a bit more flexible (for situations where the plus sign is not appropriate).
So, with the second version of the macro you can make the plus sign an optional part of the first argument in the macro call:
108 %put eta = int %terms(+ a#*D#,5) %terms(+ b#*D#*X,5); eta = int + a1*D1 + a2*D2 + a3*D3 + a4*D4 + a5*D5 + b1*D1*X + b2*D2*X + b3*D3*X + b4*D4*X + b5*D5*X
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.