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
- /
- Programming
- /
- Re: use macro variable in model statement

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 08-24-2016 06:46 PM
(1477 views)

Dear All,

I have a simple question here, I have two list of variables:

%let A= A1 A2 A3;

%let B= B1 B2 B3;

I want to fit a PH regression model with effect of A's and B's and their interaction repectively, i.e. :A1*B1 A2*B2 A3*B3.

I ceded as follow, but this doesn't give me the interaction term i desired, it only gives me A3*B1.

**proc** **phreg** data=modeldata;

model followup*pass(**0**) = &A &B aA*&B ;

**run**;

Can anyone help address this bug?

Thank youvery much!!

Best wishes,

1 ACCEPTED SOLUTION

Accepted Solutions

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

This is essentially how you could do it, wanted1 is solution 1, and wanted2 is solution 2. Hope this makes sense.

%macro wanted1;

%let A= A1 A2 A3;

%let B= B1 B2 B3;

proc phreg data=_null_;

model followup*pass(0) =

%do i = 1 %to 3;

%let wanta = %scan(&a,&i,' ');

%let wantb = %scan(&b,&i,' ');

&wanta &wantb &wanta*&wantb

%end;

;

run;

%mend;

%wanted1;

%macro wanted2;

%let A= A1 A2 A3;

%let B= B1 B2 B3;

%do i = 1 %to 3;

%let wanta = %scan(&a,&i,' ');

%let wantb = %scan(&b,&i,' ');

proc phreg data=modeldata;

model followup*pass(0) = &wanta &wantb &wanta*&wantb;

run;

%end;

%mend;

%wanted2;

11 REPLIES 11

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

**no** macro variables at all were involved.

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

You can specify interaction terms by including @2 after your variables to get all 2 way interactions.

Model x = a b @2;

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

Dear Reeza,

Thank you for the quick response!

I tried

a b @2; &a &b @2; &a| &b @2; .....

But I still only got the interaction of A1*B3..... Is this only effect on non-macro variables?

Thanks again!

Best wishes.

Thank you for the quick response!

I tried

a b @2; &a &b @2; &a| &b @2; .....

But I still only got the interaction of A1*B3..... Is this only effect on non-macro variables?

Thanks again!

Best wishes.

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

No, maybe you need the bar between them. What does the documentation say? I'd have to reference it to verify anyways.

Can you add bars between variables in your macrovarisble?

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

Dear Reeza,

Sorry I didn't get what you meant, I tried &a| &b @2; but is doesn't work. If I do it this way, there's no error message, just one note:

NOTE: '@n' notation is only valid with bar effects.

Thanks again!

Best wishes.

Sorry I didn't get what you meant, I tried &a| &b @2; but is doesn't work. If I do it this way, there's no error message, just one note:

NOTE: '@n' notation is only valid with bar effects.

Thanks again!

Best wishes.

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

Before you attempt a macro solution you should have a **working** base case without macro variables.

Show the entire code of the procedure that was working before the introduction of macro variables.

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

Which are you trying to get as an a running model?

**Example 1) **

**proc** **phreg** data=modeldata;

model followup*pass(**0**) = A1 A2 A3 B1 B2 B3 A1*B1 A2*B2 A3*B3 ;

**run**;

OR

**Example 2)**

**proc** **phreg** data=modeldata;

model followup*pass(**0**) = A1 B1 A1*B1;

**run**;

**proc** **phreg** data=modeldata;

model followup*pass(**0**) = A2 B2 A2*B2;

**run**;

**proc** **phreg** data=modeldata;

model followup*pass(**0**) = A3 B3 A3*B3;

**run**;

Because the way to do them is very different.

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

This is essentially how you could do it, wanted1 is solution 1, and wanted2 is solution 2. Hope this makes sense.

%macro wanted1;

%let A= A1 A2 A3;

%let B= B1 B2 B3;

proc phreg data=_null_;

model followup*pass(0) =

%do i = 1 %to 3;

%let wanta = %scan(&a,&i,' ');

%let wantb = %scan(&b,&i,' ');

&wanta &wantb &wanta*&wantb

%end;

;

run;

%mend;

%wanted1;

%macro wanted2;

%let A= A1 A2 A3;

%let B= B1 B2 B3;

%do i = 1 %to 3;

%let wanta = %scan(&a,&i,' ');

%let wantb = %scan(&b,&i,' ');

proc phreg data=modeldata;

model followup*pass(0) = &wanta &wantb &wanta*&wantb;

run;

%end;

%mend;

%wanted2;

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

Dear overmar,

Thank you very much! Sorry I didn't response you earlier, the first one I what I wanted. But it is also good to know how to code the second scenario, cause I may need it in future.

This is fantastic!

Thanks again!

Best wishes.

Thank you very much! Sorry I didn't response you earlier, the first one I what I wanted. But it is also good to know how to code the second scenario, cause I may need it in future.

This is fantastic!

Thanks again!

Best wishes.

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

Just for reference when I make lists such as your %let A = A1 A2 A3; I always intentionally put a character or something into the list that shouldn't be there, ie a '^', so I would say

%let A = A1^A2^A3;

then in the scan code the third parameter would be '^' rather than ' '. I do this because a space is a lot easier to accidentally code than a ^. But maybe its just me.

%let A = A1^A2^A3;

then in the scan code the third parameter would be '^' rather than ' '. I do this because a space is a lot easier to accidentally code than a ^. But maybe its just me.

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

Dear overmar,

Haha, thank you for the note! I was wondering what the third parameter " " mean..... if I just use space between element, I can omit this ' ' right? Cause that is how I coded before.

Thanks.

Best wishes,

Haha, thank you for the note! I was wondering what the third parameter " " mean..... if I just use space between element, I can omit this ' ' right? Cause that is how I coded before.

Thanks.

Best wishes,

**Don't miss out on SAS Innovate - Register now for the FREE Livestream!**

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.