## use macro variable in model statement

Solved
Frequent Contributor
Posts: 76

# use macro variable in model statement

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
Frequent Contributor
Posts: 83

## Re: use macro variable in model statement

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
Super User
Posts: 13,563

## Re: use macro variable in model statement

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.

Super User
Posts: 23,752

## Re: use macro variable in model statement

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

Model x = a b @2;

https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_glm_sect027...

Frequent Contributor
Posts: 76

## Re: use macro variable in model statement

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.
Super User
Posts: 23,752

## Re: use macro variable in model statement

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

Frequent Contributor
Posts: 76

## Re: use macro variable in model statement

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.
Super User
Posts: 13,563

## Re: use macro variable in model statement

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.

Frequent Contributor
Posts: 83

## Re: use macro variable in model statement

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
Frequent Contributor
Posts: 83

## Re: use macro variable in model statement

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;

Frequent Contributor
Posts: 76

## Re: use macro variable in model statement

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.
Frequent Contributor
Posts: 83

## Re: use macro variable in model statement

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.
Frequent Contributor
Posts: 76

## Re: use macro variable in model statement

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,
☑ This topic is solved.