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

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
overmar
Obsidian | Level 7

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;

View solution in original post

11 REPLIES 11
ballardw
Super User

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.

Reeza
Super User

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

Xiaoningdemao
Quartz | Level 8
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.
Reeza
Super User

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?

Xiaoningdemao
Quartz | Level 8
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.
ballardw
Super User

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.

overmar
Obsidian | Level 7

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.

overmar
Obsidian | Level 7

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;

Xiaoningdemao
Quartz | Level 8
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.
overmar
Obsidian | Level 7
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.
Xiaoningdemao
Quartz | Level 8
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,

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 2148 views
  • 1 like
  • 4 in conversation