BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
windy
Quartz | Level 8

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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 " + ".

 

View solution in original post

7 REPLIES 7
FreelanceReinh
Jade | Level 19

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 " + ".

 

windy
Quartz | Level 8

Hi @FreelanceReinh ,

 

This is working beautifully. Thank you so much for helping me again. I really appreciate it. 

 

windy
Quartz | Level 8

Sorry for bothering you again @FreelanceReinh 

What should I do if I want to remove the + sign?

FreelanceReinh
Jade | Level 19

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

 

windy
Quartz | Level 8

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. 

FreelanceReinh
Jade | Level 19

@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
windy
Quartz | Level 8
The second version has become so flexible and efficient that I can utilize it in other cases with minor modifications. I can't thank you enough for showing me the good piece of code.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 903 views
  • 3 likes
  • 2 in conversation