DATA Step, Macro, Functions and more

Code does not work within a macro

Accepted Solution Solved
Reply
Super Contributor
Posts: 303
Accepted Solution

Code does not work within a macro

Hello, I need the baseline covariates fro PHREG for each of the random sample with replacement. THis code does see to work well. The problem is with

baseline covariates=Pattern_&pt out=survs_&u survival= survival;.

NB: I need to run this within a macro because I have some matrices to compute. Or within a for loop.

CODE.

data neww;

seed=-1;

  alpha1 = 10;

  beta1 = 10;

  beta2=10;

  do i = 1 to 141;

  Age=40+(5)*rannor(1);

   lambdaT = 0.025; *baseline hazard;

   lambdaC= .03;  *heavy=0.15; *light=0.03;

   er=0+(0.01)*rannor(1);

    t = rand("WEIBULL", 0.75, lambdaT);  * time of event;

    c = rand("WEIBULL", 1.25, lambdaC) ;* time of censoring;

   monset = min(t, c);    * which came first?;

      obs=(t lt c); * creating observation vaiable from censored when observeed obs=1;

  pibflr= alpha1 + beta1*t + beta2*Age +er;

    output;

  end;

run;

%let pt=pt;

%macro pat(patt=);

%do &pt=1 %to &patt;  /*** I NEED THE BASELINE COVARIATE FOR AGE=0, So for each data below new_1, new_2, I want the first to set the covariates=Pattern_1, covariates=Pattern_2*/

data Pattern_&pt;

age=0;

output;

run;

%end;

%mend pat;

%MACRO DO_BRANCH(replicates=);

%do u=1 %to &replicates;

proc surveyselect data=neww  method = urs sampsize = 141 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

%pat(patt=20);

ods trace on /listing;

ods graphics on;

ods output ParameterEstimates=CPHH_&u;

proc phreg data=new_&u ;

model monset*obs(0)=age;

*baseline out=survs survival= survival;

baseline covariates=Pattern_&pt out=survs_&u survival= survival;  /* The issue is that SAS doesnot seem to understand that I want covariates=pattern_1 when running the data set new_1 and covariates=pattern_2 for new_2*/

run;

ods trace off;

%end;

%MEND DO_BRANCH;

%DO_BRANCH(replicates=20);

THIS CODE RUNS BUT NOT THE ABOVE. Below is what I want the macro to do for each case.

data Pattern;

Age=0;

output;

run;

ods trace on /listing;

ods graphics on;

ods output ParameterEstimates=CPHH;

proc phreg data=new ;

model monset*obs(0)=age;

baseline covariates=pattern out=survs survival= survival  ;

run;

ods trace off;


Accepted Solutions
Solution
‎01-17-2014 06:19 PM
Super User
Posts: 19,815

Re: Code does not work within a macro

Posted in reply to desireatem

Does this do what you expect it do?

Test it %put &pt afterwards to see what it resolves to:

%let pt=pt;

This will create a set of pattern_1, pattern_2 datasets that are identical. Why? There is no point to this, simply have one pattern dataset.

%macro pat(patt=);

%do &pt=1 %to &patt;

data Pattern_&pt;

age=0;

output;

run;

%end;

%mend pat;

%MACRO DO_BRANCH(replicates=);

%do u=1 %to &replicates;

proc surveyselect data=neww  method = urs sampsize = 141 OUTHITS

     reps=1 samprate=1  seed=-1 out=new_&u;

run;

%pat(patt=2); /*this isn't needed, see the above comments regarding why*/

ods trace on /listing;

ods graphics on;

ods output ParameterEstimates=CPHH_&u;

proc phreg data=new_&u ;

model monset*obs(0)=age;

*baseline out=survs survival= survival;

baseline covariates=Pattern_&pt out=survs_&u survival= survival;  /* The issue is that SAS doesnot seem to understand that I want covariates=pattern_1 when running the data set new_1 and covariates=pattern_2 for new_2*/

/*Does macro variable &pt exist here, and is it what you want, from the initial question?*/

run;

ods trace off;

%end;

%MEND DO_BRANCH;

%DO_BRANCH(replicates=2);

/*Why compare your random samples?*/

proc compare base=New_1 compare=New_2 printall;

   title 'Comparing Two Data Sets: Full Report';

run;

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Code does not work within a macro

Posted in reply to desireatem

Are you getting errors or identical output?

If it is identical output it is likely because the seed value in the surveyselect is selecting the same records for each dataset.

Try doing a proc compare on two of the new_ datasets.

A correction would be to increment the seed value using your U macro variable.

Super Contributor
Posts: 303

Re: Code does not work within a macro

I corrected the proc surveyselect, the data set are different but the code is not working;

data neww;

seed=-1;

  alpha1 = 10;

  beta1 = 10;

  beta2=10;

  do i = 1 to 141;

  Age=40+(5)*rannor(1);

   lambdaT = 0.025; *baseline hazard;

   lambdaC= .03;  *heavy=0.15; *light=0.03;

   er=0+(0.01)*rannor(1);

    t = rand("WEIBULL", 0.75, lambdaT);  * time of event;

    c = rand("WEIBULL", 1.25, lambdaC) ;* time of censoring;

   monset = min(t, c);    * which came first?;

      obs=(t lt c); * creating observation vaiable from censored when observeed obs=1;

  pibflr= alpha1 + beta1*t + beta2*Age +er;

    output;

  end;

run;

%let pt=pt;

%macro pat(patt=);

%do &pt=1 %to &patt;  /*** I NEED THE BASELINE COVARIATE FOR AGE=0, So for each data below new_1, new_2, I want the first to set the covariates=Pattern_1, covariates=Pattern_2*/

data Pattern_&pt;

age=0;

output;

run;

%end;

%mend pat;

%MACRO DO_BRANCH(replicates=);

%do u=1 %to &replicates;

proc surveyselect data=neww  method = urs sampsize = 141 OUTHITS

     reps=1 samprate=1  seed=-1 out=new_&u;

run;

%pat(patt=2);

ods trace on /listing;

ods graphics on;

ods output ParameterEstimates=CPHH_&u;

proc phreg data=new_&u ;

model monset*obs(0)=age;

*baseline out=survs survival= survival;

baseline covariates=Pattern_&pt out=survs_&u survival= survival;  /* The issue is that SAS doesnot seem to understand that I want covariates=pattern_1 when running the data set new_1 and covariates=pattern_2 for new_2*/

run;

ods trace off;

%end;

%MEND DO_BRANCH;

%DO_BRANCH(replicates=2);

proc compare base=New_1 compare=New_2 printall;

   title 'Comparing Two Data Sets: Full Report';

run;

data Pattern;

Age=0;

output;

run;

ods trace on /listing;

ods graphics on;

ods output ParameterEstimates=CPHH;

proc phreg data=neww;

model monset*obs(0)=age;

baseline covariates=pattern out=survs survival= survival  ;

run;

ods trace off;

Solution
‎01-17-2014 06:19 PM
Super User
Posts: 19,815

Re: Code does not work within a macro

Posted in reply to desireatem

Does this do what you expect it do?

Test it %put &pt afterwards to see what it resolves to:

%let pt=pt;

This will create a set of pattern_1, pattern_2 datasets that are identical. Why? There is no point to this, simply have one pattern dataset.

%macro pat(patt=);

%do &pt=1 %to &patt;

data Pattern_&pt;

age=0;

output;

run;

%end;

%mend pat;

%MACRO DO_BRANCH(replicates=);

%do u=1 %to &replicates;

proc surveyselect data=neww  method = urs sampsize = 141 OUTHITS

     reps=1 samprate=1  seed=-1 out=new_&u;

run;

%pat(patt=2); /*this isn't needed, see the above comments regarding why*/

ods trace on /listing;

ods graphics on;

ods output ParameterEstimates=CPHH_&u;

proc phreg data=new_&u ;

model monset*obs(0)=age;

*baseline out=survs survival= survival;

baseline covariates=Pattern_&pt out=survs_&u survival= survival;  /* The issue is that SAS doesnot seem to understand that I want covariates=pattern_1 when running the data set new_1 and covariates=pattern_2 for new_2*/

/*Does macro variable &pt exist here, and is it what you want, from the initial question?*/

run;

ods trace off;

%end;

%MEND DO_BRANCH;

%DO_BRANCH(replicates=2);

/*Why compare your random samples?*/

proc compare base=New_1 compare=New_2 printall;

   title 'Comparing Two Data Sets: Full Report';

run;

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 307 views
  • 0 likes
  • 3 in conversation