/*-------------------------------------------------------------------- */ /* SAS(R) Survival Analysis Techniques for Medical Research, Second Edition*/ /* by Dr. Alan B. Cantor */ /* Copyright(c) 2003 by SAS Institute Inc., Cary, NC, USA */ /* SAS Publications order # 58416 */ /* ISBN 1-59047-135-0 */ /*-------------------------------------------------------------------------*/ /* Date Last Updated: 09Feb07 */ /*-------------------------------------------------------------------------*/ */ ****************************************************** /*The following macro code appears on page 100. */ ******************************************************; %macro rand_gen( indata= , time = , cens = , group = , numreps=1000, seed=0); %let indata=&indata; /* forces evaluation of &INDATA at the right time */ /* Get size of input dataset into macro variable &NUMRECS */ proc sql noprint; select count(*) into :numrecs from &INDATA; quit; /* Generate &NUMREPS random numbers for each record, so records can be randomly sorted within each replicate */ data __temp_1; retain seed &SEED ; drop seed; set &INDATA; do replicate = 1 to &NUMREPS; call ranuni(seed,rand_dep); output; end; run; proc sort data=__temp_1; by replicate rand_dep; run; /* Now append the new re-orderings to the original dataset. Label the original as Replicate=0, so the %TEST macro will be able to pick out the correct p-value. Then use the ordering of __counter within each replicate to write the original values of &time and &cens, thus creating a randomization of these variables in every replicate. */ data reps ; array timelist{ &NUMRECS } _temporary_ ; array censlist{ &NUMRECS } _temporary_; set &INDATA(in=in_orig) __temp_1(drop=rand_dep); if in_orig then do; replicate=0; timelist{_n_} = &time ; censlist{_n_} = &cens ; end; else do ; &time = timelist{ 1+ mod(_n_,&NUMRECS) }; &cens = censlist{ 1+mod(_n_, &NUMRECS) }; end; run; %mend rand_gen; ****************************************************** /*The following macro code appears on page 102. */ ******************************************************; %macro test(time = , cens = , censval = , test = , group = , type = ); proc lifetest data = reps outtest = out noprint; time &time*&cens(&censval); test &group; by replicate; run; data out2 ; set out; if "&test" = 'logrank' then type = 'LOG RANK'; if "&test" = 'gehan' then type = 'WILCOXON'; if _TYPE_ = type and _NAME_ = "&time" then output; data out3; set out2 end = last; retain chisq; if replicate = 0 then chisq = &time; else do; if &time + .00000001 ge chisq then num+1; end; if last then do; pvalue = num/(_n_ - 1); stderr = sqrt((pvalue*(1-pvalue))/(_n_ - 1)); lowbound = max(pvalue - 1.96*stderr, 0); upperbound = min(pvalue + 1.96*stderr, 1); n = _n_ - 1; output; end; %if &type = rand %then %do; label n = 'Number of Replicates'; label pvalue = "Randomization &test Test Estimated P-Value (2-sided)"; label lowbound = 'Lower 95 Pct Bound'; label upperbound = 'Upper 95 Pct Bound'; %end; %else %do; label pvalue = "Permutation &test Test P-Value (2-sided)"; %end; %if &type = rand %then %do; proc print noobs l; var pvalue stderr lowbound upperbound n; %end; %else %do; proc print noobs l ; var pvalue; %end; run; data; set out2; if replicate = 0; p = 1 - probchi(&time, 1); label p = 'Asymptotic P-Value'; proc print noobs l ; var p; run; %mend; %macro perm_gen(indata = &syslast, time = , cens = , n1 = , n2 = , group =); %let n = %eval(&n1 + &n2); %let ncomb = %sysfunc(comb(&n, &n1)); ods output Plan=Combinations; proc plan ; factors replicate= &ncomb ordered r= &n1 of &n comb; run; data reps; set combinations; keep replicate i &group ; array r{*} r1 - r&n1; array grp{*} group1 - group&n; do i = 1 to &n; grp{i} = 2; do j = 1 to &n1; if r{j} = i then grp{i} = 1; end; &group = grp{i}; output; end; run; data temp; set _null_; %do i = 1 %to &ncomb; data temp; set temp &indata; keep &time &cens; %end; data reps; merge reps temp; data temp2; set &indata; replicate = 0; data reps; set temp2 reps; run; %mend;