boxplot.sas

Go to the documentation of this file.
00001  
00032 /* History
00033    05.10.2010 AM  Changed output format to pdf in order to be able to run on linux
00034 */ 
00035 
00036 %MACRO boxplot(
00037    data   =
00038   ,x      =
00039   ,y      = 
00040   ,group  = 
00041   ,report =
00042 );
00043 
00044 %local dsid grouptype xvalues xvalues2;
00045 
00046 /*-- check input data set ----------------------------------------------------*/
00047 %let dsid=%sysfunc(open(&data));
00048 %if &dsid=0 %then %do; 
00049    %put ERROR: boxplot: Data set &data does not exist; 
00050    %return; 
00051 %end; 
00052 /*-- check whether x variable has been specified -----------------------------*/
00053 %if "&x"="" %then %do;
00054    %put ERROR: boxplot: X variable not specified; 
00055    %let dsid=%sysfunc(close(&dsid));
00056    %return; 
00057 %end; 
00058 /*-- check for existence of x variable ---------------------------------------*/
00059 %if %sysfunc(varnum(&dsid,&x))=0 %then %do;
00060    %put ERROR: boxplot: Variable &x does not exist in data set &data ; 
00061    %let dsid=%sysfunc(close(&dsid));
00062    %return; 
00063 %end; 
00064 
00065 /*-- check whether x variable is numeric -------------------------------------*/
00066 %if %sysfunc(vartype(&dsid,%sysfunc(varnum(&dsid,&x)))) NE N %then %do;
00067    %put ERROR: boxplot: Variable &x in data set &data must be numeric; 
00068    %let dsid=%sysfunc(close(&dsid));
00069    %return; 
00070 %end; 
00071 /*-- determine values of x variable for axis ---------------------------------*/
00072 proc sql noprint;
00073    select distinct &x into :xvalues separated by '" "' from &data;
00074    select distinct &x into :xvalues2 separated by ' ' from &data;
00075 quit;
00076 /*-- check whether y variable has been specified -----------------------------*/
00077 %if "&y"="" %then %do;
00078    %put ERROR: boxplot: Y variable not specified; 
00079    %let dsid=%sysfunc(close(&dsid));
00080    %return; 
00081 %end; 
00082 /*-- check for existence of y variable ---------------------------------------*/
00083 %if %sysfunc(varnum(&dsid,&y))=0 %then %do;
00084    %put ERROR: boxplot: Variable &y does not exist in data set &data ; 
00085    %let dsid=%sysfunc(close(&dsid));
00086    %return; 
00087 %end; 
00088 /*-- check wheter y variable ist numeric -------------------------------------*/
00089 %if %sysfunc(vartype(&dsid,%sysfunc(varnum(&dsid,&y)))) NE N %then %do;
00090    %put ERROR: boxplot: Variable &y in data set &data must be numeric; 
00091    %let dsid=%sysfunc(close(&dsid));
00092    %return; 
00093 %end; 
00094 /*-- check whether group variable has been specified -------------------------*/
00095 %if "&group"="" %then %do;
00096    %put ERROR: boxplot: Group variable must be specified; 
00097    %let dsid=%sysfunc(close(&dsid));
00098    %return; 
00099 %end; 
00100 /*-- check for existence of group variable -----------------------------------*/
00101 %if %sysfunc(varnum(&dsid,&group))=0 %then %do;
00102    %put ERROR: boxplot: Variable &group does not exist in data set &data ; 
00103    %let dsid=%sysfunc(close(&dsid));
00104    %return; 
00105 %end; 
00106 /*-- check for number of groups and determine variable type and group sequence -*/
00107 %let grouptype=%sysfunc(vartype(&dsid,%sysfunc(varnum(&dsid,&group))));
00108 %local count lower;
00109 proc sql noprint;
00110    select count(distinct &group) into :count from &data;
00111    select min(&group) into :lower from &data;
00112 quit; 
00113 %if &lower=. %then %do; 
00114    %put ERROR: boxplot: Missing values in group variable are not allowed; 
00115    %return; 
00116    proc sql noprint; drop table &d_1; quit;
00117 %end; 
00118 %if &count NE 2 %then %do;
00119    %put ERROR: boxplot: Variable &group must have exactly two values; 
00120    %return; 
00121 %end; 
00122 
00123 %let dsid=%sysfunc(close(&dsid));
00124 
00125 /*-- calculate distance between the x values ---------------------------------*/
00126 %local d_1;
00127 DATA; RUN; 
00128 %let d_1=&syslast; 
00129 
00130 proc sql noprint;   
00131    create table &d_1 as select distinct &x from &data;
00132 quit;
00133  
00134 data &d_1; 
00135    set &d_1; 
00136    &x = &x - lag(&x);
00137    if _n_>1 then output; 
00138 run; 
00139 
00140 %local xdiff1 xdiff2 xmin xmax misscount;
00141 proc sql noprint; 
00142    select mean(&x), min(&x) into :xdiff1, :xdiff2 from &d_1;
00143    select min(&x), max(&x) into :xmin, :xmax from &data;
00144 %let misscount=0;
00145    select count(*) into :misscount from &data where &x is missing;
00146 quit;
00147 %if &xdiff1=. %then %do;
00148    %put ERROR: boxplot: x variable must have at least two values;
00149    %return; 
00150    proc sql noprint; drop table &d_1; quit;
00151 %end; 
00152 %if &misscount>0 %then %do; 
00153    %put ERROR: boxplot: Missing values in x variable are not allowed;
00154    %return; 
00155    proc sql noprint; drop table &d_1; quit;
00156 %end; 
00157 
00158 %let xmin=%sysevalf(&xmin-&xdiff1);
00159 %let xmax=%sysevalf(&xmax+&xdiff1);
00160 
00161 run; 
00162 %if &xdiff1 ne &xdiff2 %then %do; 
00163    %put ERROR: boxplot: Values of x variable are not equidistant;
00164    %return; 
00165    proc sql noprint; drop table &d_1; quit;
00166 %end; 
00167 
00168 /*-- calculate offset between the plots of the two groups --------------------*/
00169 %local d_plot;
00170 data;
00171    SET &data (KEEP=&x &y &group);
00172    IF &group = %if &grouptype=N %then &lower; %else "&lower"; THEN DO;
00173       &x = &x - 0.11*&xdiff1;
00174    END;
00175    ELSE DO;
00176       &x = &x + 0.11*&xdiff1;
00177    END;
00178 RUN;
00179 %let d_plot=&syslast;
00180 
00181 /*-- create chart ------------------------------------------------------------*/
00182 GOPTIONS FTEXT="Helvetica" HTEXT=12pt hsize=16cm vsize=16cm;
00183 SYMBOL1 WIDTH = 3 BWIDTH = 3 COLOR = gray  LINE = 2 VALUE = none INTERPOL = BOXJT00 MODE = include;
00184 SYMBOL2 WIDTH = 3 BWIDTH = 3 COLOR = black LINE = 1 VALUE = none INTERPOL = BOXJT00 MODE = include;
00185 AXIS1 LABEL=(ANGLE=90) MINOR=none;
00186 AXIS2 ORDER=(&xmin &xvalues2 &xmax) VALUE=(" " "&xvalues" " ") MINOR=none;
00187 LEGEND1 FRAME;
00188 
00189 ODS PDF FILE="&report";
00190 ODS LISTING CLOSE;
00191 PROC GPLOT DATA=&d_plot;
00192    PLOT &y * &x = &group / VAXIS=Axis1 HAXIS=Axis2 LEGEND=Legend1 NOFRAME;
00193 RUN;
00194 QUIT;
00195 ODS PDF CLOSE;
00196 ODS LISTING;
00197 
00198 proc sql noprint; 
00199    drop table &d_plot; 
00200    drop table &d_1; 
00201 quit;
00202 
00203 %MEND boxplot;

Generated on Sun Oct 10 14:10:15 2010 for SASUnit Examples by  doxygen 1.5.3