## SAS MACRO withing MACRO

Solved
Super Contributor
Posts: 328

# SAS MACRO withing MACRO

%macro surv(replicates=);

%do p=1 %to &replicates;

%macro predict_&p (outest=, out=_last_, xbeta=, time=);

%predict_&p (outtest=a_&p, out=b_&p, xbeta=lp, time=30);

data _pred_&p;

_p_=1;

set &outest point=_p_;

set &out;

lp=&xbeta;

gamma=1/_scale_;

alpha=exp(-lp*gamma);

prob=0;

_dist_=upcase(_dist_);

if _dist_='WEIBULL' or _dist_='EXPONENTIAL' or _dist_='EXPONENT' then prob=exp(-alpha*time**gamma);

if _dist_='LOGNORMAL' or _dist_='LNORMAL' then prob=1-probnorm((log(time)-lp)/_scale_);

if  _dist_='LLOGISTIC' or _dist_='LLOGISTC' then prob=1/(1+alpha*time**gamma);

if _dist_='GAMMA' then do;

d=_shape1_;

k=1/(d*d);

u=(t*exp(-lp))**gamma;

prob=1-probgam(k*u**d,k);

if d lt 0 then prob=1-prob;

end;

drop lp gamma alpha _dist_ _scale_ intercept _shepe1_ _model_ _name_ _type_ _status_ _prob_ _lnlike_ d k u;

run;

proc print data=_pred_&p;

run;

%mend predict_&p;

DATA test_&p;

one   = 1;

beta0 =  1.0;

beta1 = 0.75;

beta2 =  -.5;

subsd =  -.5;

errsd =  1;

seed  = 974657731;

n=1;

do while (n le 100);

ni=1;

sun = rannor(seed);

do while (ni le 10);

x1 = abs(normal(6));

error = rannor(seed);

time = ni-1;

test=7*ranuni(seed)+1;  /** uniform (1,8)  */;

m1=min(time, test);

m2=max(0,time-test);

olf = (time lt test);

*grp = 0; if n le 50 then grp=1;

y = beta0*x1 + beta1*m1 +beta2*m2 + subsd*sun + errsd*error;

output test_&p;

ni+1;

end;

n+1;

end;

proc lifereg data=test_&p outest=a_&p;

class n;

model (m1, m2) = x1/D=LNORMAL;

output out=b_&p xbeta=lp;

run;

%PREDICT_&P(OUTEST=a_&p, out=b_&p, xbeta=lp, time=30);

%end;

%mend surv;

%surv(replicates=2);

ods html;

Accepted Solutions
Solution
‎09-16-2014 12:18 AM
Super User
Posts: 8,089

## Re: SAS MACRO withing MACRO

You generate a variable TIME in your data generation step and then in your PRED calculation step you reference a non-existent variable T, which I assume you meant TIME.

You generate records where TIME=0 and then in your PRED calculation step you try to take the LOG() of TIME.  You cannot take the LOG() of zero.  I changed it to generate TIME starting with 1 instead of 0.

Not sure why you just didn't use iterative DO loops instead of trying to code your own.

So here is a program that will at least run but you will need to go over the actual calculations to see if it doing what you want.  Note that their are newer random number generating functions in SAS that probably would do a better job.

%macro predict(p,outest=, out=_last_, xbeta=, time=);

data _pred_&p;

if _n_=1 then set &outest ;

set &out;

lp=&xbeta;

gamma=1/_scale_;

alpha=exp(-lp*gamma);

prob=0;

_dist_=upcase(_dist_);

if _dist_='WEIBULL' or _dist_='EXPONENTIAL' or _dist_='EXPONENT' then prob=exp(-alpha*time**gamma);

if _dist_='LOGNORMAL' or _dist_='LNORMAL' then prob=1-probnorm((log(time)-lp)/_scale_);

if  _dist_='LLOGISTIC' or _dist_='LLOGISTC' then prob=1/(1+alpha*time**gamma);

if _dist_='GAMMA' then do;

d=_shape1_;

k=1/(d*d);

u=(time*exp(-lp))**gamma;

prob=1-probgam(k*u**d,k);

if d lt 0 then prob=1-prob;

end;

drop lp gamma alpha _dist_ _scale_ intercept _shape1_ _model_ _name_ _type_ _status_ _prob_ _lnlike_ d k u;

run;

proc print data=_pred_&p;

run;

%mend predict;

%macro surv(replicates=);

%do p=1 %to &replicates;

data test_&p;

one   = 1;

beta0 = 1.0;

beta1 = 0.75;

beta2 = -.5;

subsd = -.5;

errsd = 1;

seed  = 974657731;

do n=1 to 100 ;

grp=(n <= 50);

sun = rannor(seed);

do ni=1 to 10;

x1 = abs(normal(6));

error = rannor(seed);

time = ni;

test=7*ranuni(seed)+1/** uniform (1,8)  */;

m1=min(time, test);

m2=max(0,time-test);

olf = (time lt test);

y = beta0*x1 + beta1*m1 +beta2*m2 + subsd*sun + errsd*error;

output test_&p;

end;

end;

run;

proc lifereg data=test_&p outest=a_&p;

class n;

model (m1, m2) = x1/D=LNORMAL;

output out=b_&p xbeta=lp;

run;

%PREDICT(&P,OUTEST=a_&p, out=b_&p, xbeta=lp, time=30);

%end;

%mend surv;

%surv(replicates=1);

All Replies
Super User
Posts: 23,700

## Re: SAS MACRO withing MACRO

Posted in reply to desireatem

I think your code got cut off. I don't see any %mend for example.

Super Contributor
Posts: 328

Posts: 4,736

## Re: SAS MACRO withing MACRO

Posted in reply to desireatem

The code below executes and creates output. Looking into the log there seem to be a few more issues for you to resolve.

%macro predict (outest=, out=_last_, xbeta=, time=);

/*  %predict_&p (outtest=a_&p, out=b_&p, xbeta=lp, time=30);*/

data _pred_&p;

_p_=1;

set &outest point=_p_;

set &out;

lp=&xbeta;

gamma=1/_scale_;

alpha=exp(-lp*gamma);

prob=0;

_dist_=upcase(_dist_);

if _dist_='WEIBULL' or _dist_='EXPONENTIAL' or _dist_='EXPONENT' then

prob=exp(-alpha*time**gamma);

if _dist_='LOGNORMAL' or _dist_='LNORMAL' then

prob=1-probnorm((log(time)-lp)/_scale_);

if  _dist_='LLOGISTIC' or _dist_='LLOGISTC' then

prob=1/(1+alpha*time**gamma);

if _dist_='GAMMA' then

do;

d=_shape1_;

k=1/(d*d);

u=(t*exp(-lp))**gamma;

prob=1-probgam(k*u**d,k);

if d lt 0 then

prob=1-prob;

end;

drop lp gamma alpha _dist_ _scale_ intercept _shepe1_ _model_ _name_ _type_ _status_ _prob_ _lnlike_ d k u;

run;

proc print data=_pred_&p;

run;

%mend predict;

%macro surv(replicates=);

%do p=1 %to &replicates;

DATA test_&p;

one   = 1;

beta0 =  1.0;

beta1 = 0.75;

beta2 =  -.5;

subsd =  -.5;

errsd =  1;

seed  = 974657731;

n=1;

do while (n le 100);

ni=1;

sun = rannor(seed);

do while (ni le 10);

x1 = abs(normal(6));

error = rannor(seed);

time = ni-1;

test=7*ranuni(seed)+1;  /** uniform (1,8)  */;

m1=min(time, test);

m2=max(0,time-test);

olf = (time lt test);

*grp = 0;

if n le 50 then

grp=1;

y = beta0*x1 + beta1*m1 +beta2*m2 + subsd*sun + errsd*error;

output test_&p;

ni+1;

end;

n+1;

end;

proc lifereg data=test_&p outest=a_&p;

class n;

model (m1, m2) = x1/D=LNORMAL;

output out=b_&p xbeta=lp;

run;

%predict(OUTEST=a_&p, out=b_&p, xbeta=lp, time=30);

%end;

%mend surv;

%surv(replicates=2);

ods html;

Solution
‎09-16-2014 12:18 AM
Super User
Posts: 8,089

## Re: SAS MACRO withing MACRO

You generate a variable TIME in your data generation step and then in your PRED calculation step you reference a non-existent variable T, which I assume you meant TIME.

You generate records where TIME=0 and then in your PRED calculation step you try to take the LOG() of TIME.  You cannot take the LOG() of zero.  I changed it to generate TIME starting with 1 instead of 0.

Not sure why you just didn't use iterative DO loops instead of trying to code your own.

So here is a program that will at least run but you will need to go over the actual calculations to see if it doing what you want.  Note that their are newer random number generating functions in SAS that probably would do a better job.

%macro predict(p,outest=, out=_last_, xbeta=, time=);

data _pred_&p;

if _n_=1 then set &outest ;

set &out;

lp=&xbeta;

gamma=1/_scale_;

alpha=exp(-lp*gamma);

prob=0;

_dist_=upcase(_dist_);

if _dist_='WEIBULL' or _dist_='EXPONENTIAL' or _dist_='EXPONENT' then prob=exp(-alpha*time**gamma);

if _dist_='LOGNORMAL' or _dist_='LNORMAL' then prob=1-probnorm((log(time)-lp)/_scale_);

if  _dist_='LLOGISTIC' or _dist_='LLOGISTC' then prob=1/(1+alpha*time**gamma);

if _dist_='GAMMA' then do;

d=_shape1_;

k=1/(d*d);

u=(time*exp(-lp))**gamma;

prob=1-probgam(k*u**d,k);

if d lt 0 then prob=1-prob;

end;

drop lp gamma alpha _dist_ _scale_ intercept _shape1_ _model_ _name_ _type_ _status_ _prob_ _lnlike_ d k u;

run;

proc print data=_pred_&p;

run;

%mend predict;

%macro surv(replicates=);

%do p=1 %to &replicates;

data test_&p;

one   = 1;

beta0 = 1.0;

beta1 = 0.75;

beta2 = -.5;

subsd = -.5;

errsd = 1;

seed  = 974657731;

do n=1 to 100 ;

grp=(n <= 50);

sun = rannor(seed);

do ni=1 to 10;

x1 = abs(normal(6));

error = rannor(seed);

time = ni;

test=7*ranuni(seed)+1/** uniform (1,8)  */;

m1=min(time, test);

m2=max(0,time-test);

olf = (time lt test);

y = beta0*x1 + beta1*m1 +beta2*m2 + subsd*sun + errsd*error;

output test_&p;

end;

end;

run;

proc lifereg data=test_&p outest=a_&p;

class n;

model (m1, m2) = x1/D=LNORMAL;

output out=b_&p xbeta=lp;

run;

%PREDICT(&P,OUTEST=a_&p, out=b_&p, xbeta=lp, time=30);

%end;

%mend surv;

%surv(replicates=1);

Super User
Posts: 8,089

## Re: SAS MACRO withing MACRO

Posted in reply to desireatem

DO NOT define a macro inside of another macro.

Move the definition of the PREDICT macro outside of the definition of the SURV macro.

Why would you want to use a macro variable reference in the name of a macro?  If the parameter P changes what code the macro should generate then make it a parameter to the macro.

Do NOT call a macro within itself unless you really understand recursion. And this means being sure that the recursion will always stop and the total depth of the recursion will not be so large that it will swamp your machine's ability to store that many levels of macro environments.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 328 views
• 0 likes
• 4 in conversation