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

Please help correct this 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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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);

View solution in original post

5 REPLIES 5
Reeza
Super User

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

desireatem
Pyrite | Level 9

Thanks, added it

Patrick
Opal | Level 21

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;

Tom
Super User Tom
Super User

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);

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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