Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Re: How to specify multiple terms in the macro with proc nlmixed

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 07-14-2021 05:16 AM
(906 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

7 REPLIES 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi @FreelanceReinh ,

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Sorry for bothering you again @FreelanceReinh

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

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!

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.