## Looping with a macro variable

Solved
Regular Contributor
Posts: 169

# Looping with a macro variable

Hi all

I have a code that looks like this:

%let var=1;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;

The reason why I am using a macro variable is that I need to repeat this code 20 times, with the "var" value from 1 to 20.

I have been doing this manually by changing the number and re-running the code. There should however be a way to "loop" through numbers 1 to 20 and re-run the code automatically

Any tips?

Appreciated

Best reg

Accepted Solutions
Solution
‎06-13-2015 06:49 PM
Super User
Posts: 20,715

## Re: Looping with a macro variable

Use a %do loop, but it needs to be within a macro.

%macro repeat_phreg(ntimes=);

%do var=1 %to &ntimes;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;

%end;

%mend;

%repeat_phreg(ntimes=20);

All Replies
Solution
‎06-13-2015 06:49 PM
Super User
Posts: 20,715

## Re: Looping with a macro variable

Use a %do loop, but it needs to be within a macro.

%macro repeat_phreg(ntimes=);

%do var=1 %to &ntimes;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;

%end;

%mend;

%repeat_phreg(ntimes=20);

Regular Contributor
Posts: 169

## Re: Looping with a macro variable

Thanks Reza, much appreciated

SAS Super FREQ
Posts: 3,834

## Re: Looping with a macro variable

Convert the data from wide to long format. The data then has 20 BY groups with values 1-20, and a single variable (call it FACTOR) that represents the stacking of factor1-factor20.  Then run a single analysis. It will be much easier to compare the results, which are now in a single data set.

proc phreg data=have ;

by group;

strata country;

class sex ;

model time*event&var(0)=factor age weight / rl;

ods output parameterestimates = CorrelationEstimate;

run;

Regular Contributor
Posts: 169

## Re: Looping with a macro variable

Rick

A very interesting approach indeed. I even like it actually

BUT

there is a small problem: as you can see from my code, i am using the looping variable for 3 things:

%let var=1;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;

Your suggestion would solve the second and the third one, but I am also modelling for event1-20

Any suggestions on how to accommodate this in your code?

Thanks again

Super User
Posts: 20,715

## Re: Looping with a macro variable

The data reshape will take care of that. The last instance of &var becomes unnecessary.

data flipped;

set have;

array event(20) event1-event20;

array factor(20) factor1-factor20;

do group=1 to 20;

event_group=event(i);

factor_group=factor(i);

output;

end;

run;

proc phreg data=flipped;

by group;

strata country;

class sex ;

model time*event_group=factor_group age weight / rl;

ods output parameterestimates = CorrelationEstimate;

run;

Regular Contributor
Posts: 169

## Re: Looping with a macro variable

Correct me if I am wrong Reeza, but I think this code works if event1-20 are mutually exclusive.

In my case the same person may have event1 and event3

The same thing is with factor1-20, they are not mutually exclusive

What do you think?

Cheers

Super User
Posts: 20,715

## Re: Looping with a macro variable

Try it.

It doesn't matter if they're mutually exclusive or not since you have a record for each, and your code doesn't account for that in the initial program anyways.

Regular Contributor
Posts: 169

## Re: Looping with a macro variable

I am officially impressed! It did work!

Big thanks to Rick and Reeza

Cheers

🔒 This topic is solved and locked.