turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- use macro variable in model statement

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-24-2016 06:46 PM

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,

Accepted Solutions

Solution

08-25-2016
01:52 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 01:39 PM

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;

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-24-2016 06:54 PM

Besides missing an & in A*&B what did you want to create? Since there are many possible variations of possible syntax involving () around individual or groups of variables and the * it helps to know what the desired result for your model statement would be if **no** macro variables at all were involved.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-24-2016 06:55 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-24-2016 07:31 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-24-2016 08:27 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 02:00 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 11:33 AM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 01:30 PM

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.

Solution

08-25-2016
01:52 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 01:39 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 01:55 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 01:58 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

08-25-2016 02:04 PM

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,