I am trying to use phreg procedure and extract the likelihood value. I can do this for one value of c however I need to repeat this for c=1,...,50. For each value of c, I need to store the likelihood value. So far, I have the following which works just fine one value of c.
proc iml;
use test; read all;
proc phreg data = test;
ods output FitStatistics=Output noprint;
model times * dead(0) = sexE sexL;
c = 1;
if times <= c and sex = 1 then sexE=1; else sexE=0;
if times > c and sex = 1 then sexL=1; else sexL=0;
run;
proc print data=Output noobs;
run;
data _null_;
set Output;
if Criterion="-2 LOG L" then call symputx("value",WithCovariates);
run;
%put value= &value;
Any help would be appreciated. Thanks.
Then you'll need a macro or CALL EXECUTE.
There's an example in this tutorial that's fairly close to what you're trying to do UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
Instead of running the proc multiple times, expand your data set and then run the PROC PHREG once with a BY statement.
data test_large;
set have;
do c=1 to 50;
if times <= c and sex = 1 then sexE=1; else sexE=0;
if times > c and sex = 1 then sexL=1; else sexL=0;
output;
end;
run;
proc sort data=test_large;
by c;
run;
proc phreg data=........
BY C;
*everything else here is the same minus the manual coding of the variable;
....
run;
Thank you for the reply. Actually, the manual coding of the variable needs to be inside the proc phreg. Otherwise, the results are not the same.
Then you'll need a macro or CALL EXECUTE.
There's an example in this tutorial that's fairly close to what you're trying to do UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
Our shop does this a lot. If you don't have an indicator variable for each dataset, you will need to create one. Then you can do something like the following (NOTE: This is a different PROC, so look at the various ODS table names for PHREG to get what you want..):
This part generates the by variables and merges them back onto the original dataset:
/*Process anlsdat byvar*/
proc sort data=anlsdatin out=byvars (keep = paramid parameter) nodupkey;
by paramid parameter;
run;
data byvars;
set byvars end=eof;
by paramid parameter;
byvar = _N_;
if eof then call symputx('LastByVar', _N_);
run;
proc sort data=anlsdatin;
by paramid parameter;
run;
data anlsdat;
merge anlsdatin byvars;
by paramid parameter;
run;
This extracts the fit information from the PROC (GLIMMIX here):
%macro runtimes();
%do b=2 %to &LastByVar;
proc sort data=anlsdat (where=(byvar=&b.)) out=anlsdat_&b.;
by byvar paramid parameter grp_no sex animal;
run;
%macro runtests(covtype=, it=);
proc glimmix data=anlsdat_&b. method = laplace;
nloptions maxiter = 5000;
by byvar paramid parameter;
class grp_no sex animal;
model value = grp_no sex grp_no*sex / dist = gamma ddfm=bw;
random grp_no / type = &covtype. subject=animal;
ods output fitstatistics = _rm_fit_&b._&it._1 convergencestatus=_convdat1_&b._&it._1;
run;
data _rm_fit_&b._&it._1;
length covtype $10 value 8 descr $100;
%if %sysfunc(exist(_rm_fit_&b._&it._1)) %then %do; set _rm_fit_&b._&it._1; %end;
covtype = "&covtype.";
Distribution="Gamma";
run;
%mend runtests;/*
%runtests(covtype=CSH, it=1);
%runtests(covtype=CHOL, it=2);
data fit&b.;
set _rm_fit_&b.:;
run;
proc datasets nolist lib=work;
delete _rm_fit_&b.:;
run;
proc sort data=fit&b. out=checkcov&b. (where=(lowcase(descr) = "aicc (smaller is better)" or lowcase(descr) = "aicc (smaller is better)"));
by value;
run;
%let cs = 0;
%let dist = ;
/*
data checkcov&b.;
%if %sysfunc(exist(checkcov&b.)) %then %do;
set checkcov&b.;
%end;
if _N_ = 1 then do;
call symputx('cs', covtype);
call symputx('dist', Distribution);
selected = 1;
end;
run;
*/
%if "&cs" = "0" %then %do;
%let cs = UN(1);
%let dist = Gamma;
%end;
%mend runtimes
If you use this, be sure that all the %do loops are closed, as I have edited out some things we would rather keep proprietary.
SteveDenham
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.