lilinzhong
以下是SAS作ROC曲线的宏程序
%macro rocplot ( version, outroc=, out=, p=, id=, plottype=high, font=swissb,
size=2, position=F, color=black, plotchar=dot,
roffset=4, round=1e-8 );
%if &version ne %then %put ROCPLOT macro Version 1.0;
options nonotes;
%let nomatch=0;
/* Verify ID= is specified */
%if %quote(&id)= %then %do;
%put ERROR: The ID= option is required.;
%goto exit;
%end;
/* Verify P= is specified */
%if %quote(&p)= %then %do;
%put ERROR: The P= option is required.;
%goto exit;
%end;
/* Verify OUTROC= is specified and the data set exists */
%if %quote(&outroc) ne %then %do;
%if %sysfunc(exist(&outroc)) ne 1 %then %do;
%put ERROR: OUTROC= data set not found.;
%goto exit;
%end;
%end;
%else %do;
%put ERROR: The OUTROC= option is required.;
%goto exit;
%end;
/* Verify OUT= is specified and the data set exists */
%if %quote(&out) ne %then %do;
%if %sysfunc(exist(&out)) ne 1 %then %do;
%put ERROR: OUT= data set not found.;
%goto exit;
%end;
%end;
%else %do;
%put ERROR: The OUT= option is required.;
%goto exit;
%end;
data _outroc;
set &outroc;
_prob_=round(_prob_,&round);
run;
data _out;
set &out;
_prob_=round(&p , &round);
length _id $ 200;
/* Create single label variable */
_id=trim(left(%scan(&id,1)))
%let i=2;
%do %while (%scan(&id,&i) ne %str() );
||'/'||trim(left(%scan(&id,&i)))
%let i=%eval(&i+1);
%end;
;
run;
proc sort data=_out nodupkey;
by _prob_ _id;
run;
proc sort data=_outroc nodupkey;
by _prob_;
run;
data _rocplot;
_inout=0; _inroc=0;
merge _outroc(in=_inroc) _out(in=_inout);
by _prob_;
if not(_inout and _inroc) then do;
call symput('nomatch',1);
delete;
end;
run;
%if &nomatch=1 %then %do;
%put ROCPLOT: Some predicted values in OUT= did not match predicted values;
%put %str( in OUTROC=. Verify that you used the ROCEPS=0 option in);
%put %str( PROC LOGISTIC.);
%end;
%if %upcase(%substr(&plottype,1,1))=L %then %do;
footnote "Point labels are values of &id";
proc plot data=_rocplot;
plot _sensit_*_1mspec_ $ _id /
haxis=0 to 1 by .1 vaxis=0 to 1 by .1;
run; quit;
%end;
%if %upcase(%substr(&plottype,1,1))=H %then %do;
data _anno;
length function style color $ 8 position $ 1 text $ 200;
retain function 'label' xsys ysys '2' hsys '3'
size &size position "&position" style "&font"
color "&color";
set _rocplot(keep=_sensit_ _1mspec_ _id) end=eof;
x=_1mspec_; y=_sensit_; text=trim(left(_id)); output;
/* Draw (0,0) to (1,1) reference line */
if eof then do;
x=0; y=0; function='move'; output;
x=1; y=1; function='draw'; line=1; hsys='1'; size=0.25; output;
end;
run;
symbol1 i=join v=&plotchar c=blue l=1;
footnote "Point labels are values of &id";
axis1 offset=(1,&roffset)pct order=(0 to 1 by .1);
proc gplot data=_rocplot;
plot _sensit_*_1mspec_=1 / vaxis=0 to 1 by .1
haxis=axis1 annotate=_anno;
run;
quit;
%end;
footnote;
%exit:
options notes;
%mend;