proc print data=GTEST3 noobs label;
run ;

*** SGPLOTプロシジャ使用例 ;
proc sgplot data=GTEST1 ;
   vline VISIT
     / response  = FBS
       group     = TREAT
       stat      = mean
       limitstat = stddev
       numstd    = 1
       markers
       markerattrs
         =(symbol=circlefilled);
run ;

proc sgplot data=GTEST2 ;
   reg x=HEIGHT y=WEIGHT / clm cli ;
run ;

*** SGPANELプロシジャ使用例 ;
proc sgpanel data=GTEST1 ;
   panelby TREAT GENDER /
      layout = lattice 
               novarname ;
   vline VISIT /
      response  = FBS
      stat      = mean
      limitstat = stddev
      numstd    = 1
      lineattrs = 
      (color = black)
      markers
      markerattrs =
      (color  = black
       symbol = circlefilled) ;
run ;

proc sgpanel data=GTEST2 ;
   panelby GENDER / layout=panel ;
   histogram WEIGHT ;
   density WEIGHT / 
      type        = normal
      name        = "aaa"
      legendlabel = "Normal" ;
   density WEIGHT /
      type        = kernel
      name        = "bbb"
      legendlabel = "Kernel" ;
   keylegend "aaa" "bbb" ;
run ;

*** SGSCATTERプロシジャ使用例 ;
proc sgscatter data=GTEST2 ;
   compare y=WEIGHT x=(HEIGHT ABDCF) /
   reg = 
   (clm cli lineattrs=(color=blue)) ;
run ;

proc sgscatter data=GTEST2 ;
   matrix WEIGHT HEIGHT ABDCF /
   diagonal = 
   (histogram normal kernel) ;
run ;

*** 5.3節 GTLでテンプレート定義 ;
proc template ;
  define statgraph _GTEST ;              *--- テンプレート定義開始 ;
    begingraph ;                         *--- グラフ定義開始 ;
      layout lattice 
      / columns=2 rows=1 columngutter=5px columnweights=(.5 .5)
        rowdatarange=union ;             *--- レイアウト定義開始 ;

        layout overlay ;                 *--- グラフ(1)定義開始 ;
          modelband "clm" ;              *--- 信頼区間出力 ;
          modelband "cli" / 
            display=(outline)
            outlineattrs=(color=blue pattern=2) ; *--- 予測区間出力 ;
          scatterplot x=HEIGHT y=WEIGHT ;*--- 散布図出力 ;
          regressionplot x=HEIGHT y=WEIGHT /
            cli="cli" clm="clm"  ;       *--- 回帰直線出力 ;
        endlayout ;                      *--- グラフ(1)定義終了 ;

        layout overlay ;                 *--- グラフ(2)定義開始 ;
          barchart x=SMOKE y=WEIGHT /
            stat=mean orient=vertical ;  *--- 棒グラフ出力 ;
        endlayout ;                      *--- グラフ(2)定義終了 ;

      endlayout ;                        *--- レイアウト定義終了 ;
    endgraph ;                           *--- グラフ定義終了 ;
  end ;                                  *--- テンプレート定義終了 ;
run ;

*** SGRENDERプロシジャでグラフ作成 ;
proc sgrender data=GTEST2 template=_GTEST ; run ;

*** 5.4節 グラフの作成例 ;
*** ①個別推移図 *********************************************** ;
*--- 1) Statistical Graphics Procedure-------------------------- ; 
ods listing style=journal ;
title bold f="MS Gothic" h=12pt "個別推移図: SGPLOTプロシジャ" ;
proc sgplot data=GTEST1 noautolegend ;
   where TREAT = 1 ;
   band y=WEIGHT lower=100 upper=110
   / transparency=0.5 fill
     fillattrs=(color=lightgray) ;       *--- グレーの網掛け ;
   xaxis values=(100 to 130 by 10) ;     *--- 横軸 ;
   yaxis values=(40 to 90 by 10) ;       *--- 縦軸 ;
   series x=VISIT y=WEIGHT
   / group=ID lineattrs=(color=black pattern=1) ; *--- 折れ線グラフ ;
   refline 105
   / axis=x label="|--Grayed out--|"  lineattrs=(thickness=0)
     labelloc=outside labelpos=auto ;    *--- 軸外のラベル ;
run ;
title ;
ods listing style=default ;

ods listing style=journal ;
title bold f="MS Gothic" h=12pt "個別推移図: SGPANELプロシジャ" ;
proc sgpanel data=GTEST1 noautolegend ;
   panelby GENDER TREAT
   / layout=lattice columns=2 ;        *--- 層別項目 ;
   colaxis values=(100 to 130 by 10) ; *--- 横軸 ;
   rowaxis values=(40 to 90 by 10) ;   *--- 縦軸 ;
   series x=VISIT y=WEIGHT
   / group=ID lineattrs=(color=black pattern=1) ; *--- 折れ線グラフ ;
run ;
title ;
ods listing style=default ;
*--------------------------------------------------------------- ; 

*--- 2) Traditional -------------------------------------------- ; 
ods listing close ;
ods listing ;
axis1 minor=none length=70 pct order=(100 to 130 by 10) ; *--- 横軸 ;
axis2 minor=none length=70 pct order=(40 to 90 by 10) ;   *--- 縦軸 ;
symbol v=none i=join ci=black line=1 r=100 ; *--- 線種,色,シンボル ;

title bold f="MS Gothic" h=12pt "個別推移図: GPLOTプロシジャ" ;
proc gplot data=GTEST1 ;
   where TREAT = 1 ;
   plot WEIGHT*VISIT=ID / haxis=axis1 vaxis=axis2 nolegend ;
run ;
quit ;
title ;

*--------------------------------------------------------------- ; 

*** ②平均値推移図 ********************************************* ;

*--- 1) Statistical Graphics Procedure-------------------------- ; 

*--- 薬剤群でVISITをずらす ;
data GTEST1_M ;
   set GTEST1 ;
   if TREAT = 2 then VISIT = VISIT + 1 ;
run ;

*--- 薬剤群ごとの平均値,標準偏差の推移図 ;
title bold f="MS Gothic" h=12pt "平均値推移図 : SGPLOTプロシジャ" ;
proc sgplot data=GTEST1_M ;
   vline VISIT
   / group=TREAT response=FBS stat=mean
     limitstat=stddev numstd=1 markers
     markerattrs=(symbol=circlefilled)  ; *--- 平均値,SD,シンボル ;
   xaxis type=linear offsetmin=0.05 offsetmax=0.05 ; *--- 軸の指定 ;
run ;

*--- 薬剤群,性別ごとの平均値,標準偏差の推移図 ;
title bold f="MS Gothic" h=12pt "平均値推移図: SGPANELプロシジャ" ;
proc sgpanel data=GTEST1 ;
   panelby TREAT GENDER / layout=lattice ; *--- 層別項目 ;
   vline VISIT
 / response=FBS stat=mean limitstat=stddev numstd=1
  lineattrs=(color=blue) markers
     markerattrs
=(color=blue symbol=circlefilled) ; *--- 平均値,SD,シンボル ;
run ;
title ;
*--------------------------------------------------------------- ; 

*--- 2) Traditional -------------------------------------------- ; 
goptions reset=all ;
axis1 minor=none length=70 pct order=(100 to 140 by 10) offset=(15,0) ;
axis2 minor=none length=70 pct order=(90 to 150 by 10) ;
symbol1 v=none ci=red  i=std1tj ;
symbol2 v=none ci=blue i=std1tj ;
legend1 value=(j=l) label=("Legend") across=1 position=(top right inside)
        offset=(-2,-2)pct frame ;

*--- 140はダミーのフォーマットを作成 ;
proc format ;
   value VISIT2F 100="0週" 110="4週後" 120="8週後" 130="12週後" 140=" " ;
run ;

title bold f="MS Gothic" h=12pt "平均値推移図: GPLOTプロシジャ" ;
proc gplot data=GTEST1_M ;
   plot FBS*VISIT=TREAT / haxis=axis1 vaxis=axis2 legend=legend1 ;
   format VISIT VISIT2F. ;
run ;
quit ;
title ;
*--------------------------------------------------------------- ; 

*** ③棒グラフ ************************************************* ;
*--- 1) Statistical Graphics Procedure-------------------------- ; 
title bold f="MS Gothic" h=12pt "棒グラフ: SGPLOTプロシジャ" ;
proc sgplot data=GTEST2 ;
   yaxis label="Mean Weight(kg)" ;
   vbar TREAT
   / response=WEIGHT stat=mean limitstat=stddev
     limits=upper transparency=0.3 barwidth=0.4 ; *--- 平均値とSD棒グラフ ;
run ;

ods listing style=journal ;
title bold f="MS Gothic" h=12pt "棒グラフ: SGPANELプロシジャ" ;
proc sgpanel data=GTEST1 ;
   panelby VISIT
   / layout=columnlattice noborder novarname
     onepanel colheaderpos=bottom ; *--- レイアウトを指定 ;
   vbar TREAT
   / response=FBS group=TREAT stat=mean 
     transparency=0 barwidth=0.7 ; *--- 平均値とSD棒グラフ ;
   rowaxis values=(50 to 130 by 10) ;
   colaxis display=none ;
run ;

ods listing close ;
ods listing ;

title bold f="MS Gothic" h=12pt "棒グラフ: SGPANELプロシジャ2" ;
proc sgpanel data=GTEST1 noautolegend ;
   panelby TREAT / layout=columnlattice novarname
                   onepanel colheaderpos=bottom ;
   vbar VISIT / response=WEIGHT stat=mean transparency=0
                barwidth=0.7 limitstat=stddev limits=upper ;
   rowaxis values=(50 to 80 by 10) ;
   colaxis display=(nolabel) ;
run ;
title ;
*--------------------------------------------------------------- ; 

*--- 2) Traditional -------------------------------------------- ; 
pattern1 v=x2    c=red  ;
pattern2 v=solid c=blue ;
axis1 minor=none length=65 pct label=none ;
axis2 minor=none length=65 pct order=(50 to 130 by 10) ;
axis3 minor=none length=65 pct label=none ;

title bold f="MS Gothic" h=12pt "棒グラフ: GCHARTプロシジャ" ;
proc gchart data=GTEST1 ;
   vbar VISIT / group=TREAT sumvar=FBS type=mean
                midpoints=(100 to 130 by 10)
                maxis=axis1 raxis=axis2 gaxis=axis3
                space=7 width=6 coutline=same
                errorbar=top patternid=group ;
run ; quit ;

/*
axis1 minor=none length=65 pct value=none label=none ;
axis2 minor=none length=65 pct order=(50 to 130 by 10) ;
axis3 minor=none length=65 pct label=none ;

title bold f="MS Gothic" h=12pt "棒グラフ: GCHARTプロシジャ2" ;
proc gchart data=GTEST1 ;
   vbar TREAT / type=mean sumvar=FBS subgroup=TREAT group=VISIT
                midpoints=(1 to 2 by 1)
                maxis=axis1 raxis=axis2 gaxis=axis3
                space=7 width=6 coutline=same ;
run ; quit ;
title ;
*/

*** ④帯グラフ ************************************************* ;
*--- 1) Statistical Graphics Procedure-------------------------- ;
ods listing style=journal ;
title bold f="MS Gothic" h=12pt "帯グラフ: SGPLOTプロシジャ1" ;
proc sgplot data=GTEST2 ;
   yaxis label="Frequency" ;
   vbar GENDER / stat=freq group=SMOKE
                 transparency=0.3 barwidth=0.4 ;
run ;
ods listing style=default ;

*--- パーセントを格納したデータセットを作成する ;
proc tabulate data=GTEST2 out=_OUTPCT ;
   class SMOKE GENDER ;
   table GENDER,SMOKE*(n rowpctn) ;
run ;

ods listing style=journal ;
title bold f="MS Gothic" h=12pt "帯グラフ: SGPLOTプロシジャ2" ;
proc sgplot data=_OUTPCT ;
   vbar GENDER / response=PctN_01 group=SMOKE stat=sum
                 transparency=0.3 barwidth=0.4 ;
   yaxis label='Percent(%)' ;
run ;
title ;
ods listing style=default ;
*--------------------------------------------------------------- ; 
ods listing close ;
ods listing ;
*--- 2) Traditional -------------------------------------------- ; 
goptions reset=all ;
pattern1 color=black ;
pattern2 color=gray ;
pattern3 color=cxEEEEEE ;

title bold f="MS Gothic" h=12pt "帯グラフ: GCHARTプロシジャ1" ;
proc gchart data=GTEST2 ;
   vbar GENDER / subgroup=SMOKE discrete type=freq
                 space=7 width=6 patternid=subgroup ;
run ; quit ;
title ;

pattern1 color=black ;
pattern2 color=gray ;
pattern3 color=cxEEEEEE ;

title bold f="MS Gothic" h=12pt "帯グラフ: GCHARTプロシジャ2" ;
proc gchart data=_OUTPCT ;
   vbar GENDER / sumvar=PctN_01 subgroup=SMOKE type=sum
                 discrete space=7 width=6 patternid=subgroup ;
   label PctN_01='Percent(%)' ;
run ; quit ;
title ;
*--------------------------------------------------------------- ; 

*** ⑤箱ひげ図 ************************************************* ;

*--- 1) Statistical Graphics Procedure-------------------------- ; 
title bold f="MS Gothic" h=12pt "箱ひげ図: SGPANELプロシジャ1" ;
proc sgpanel data=GTEST1 ;
   panelby VISIT / layout=columnlattice onepanel novarname
                   missing colheaderpos=bottom ;
   vbox WEIGHT / category=TREAT legendlabel="Box-Plot" ;
   colaxis label="Visit" ;
run ;

title bold f="MS Gothic" h=12pt "箱ひげ図: SGPANELプロシジャ2" ;
proc sgpanel data=GTEST1 ;
   panelby GENDER TREAT / layout=lattice ;
   vbox WEIGHT / category=VISIT legendlabel="Box-Plot" ;
run ;
title ;
*--------------------------------------------------------------- ; 

*--- 2) Traditional -------------------------------------------- ; 
symbol1 ; symbol2 ; axis ;

*--- GPLOT ;
data GTEST1_M ;
   set GTEST1 ;
   if TREAT = 2 then VISIT=VISIT + 2 ;
run ;

symbol1 v=none cv=black i=boxt bwidth=4 w=0.5 h=1 ;
symbol2 v=none cv=gray  i=boxt bwidth=4 w=0.5 h=1 ;
axis1 order=(100 to 140 by 10) offset=(100,0)pt 
      value=("0週" "4週" "8週" "12週" " ") major=none minor=none ;
axis2 minor=none ;

title bold f="MS Gothic" h=12pt "箱ひげ図: GPLOTプロシジャ" ;
proc gplot data=GTEST1_M ;
   plot WEIGHT*VISIT=TREAT / haxis=axis1 vaxis=axis2 ;
run ; quit ;

*--- BOXPLOT: VISIT毎に群を横に並べて出す場合はvisitをずらすしかない・・・ ;
*--- Visitをずらして出力(どうも群を文字変数に変えてVISITでソートするとずらさなくてもブロック変数使って出力できるらしい) ;
*--- なぜかods graphicsを使用するとsymbol変数のレジェンドが出力されない ;
proc sort data=GTEST1 out=GTEST1_S ;
   by VISIT TREAT ;
run ;

data GTEST1_S ;
   set GTEST1_S ;
   by VISIT TREAT ;
   retain _TREAT ;
   if first.TREAT then _TREAT+1 ;
run ;

proc sort data=GTEST1_S ; by VISIT _TREAT ; run ;

proc format ;
   value TRT2F 1,3,5,7="薬剤A" 2,4,6,8="薬剤B" ;
run ;

symbol1 v=plus   cv=black ;
symbol2 v=circle cv=gray ;
axis1 offset=(10,10)pct order=(1 to 8 by 1) major=none ;
legend1 value=(j=l) label=("Legend") across=1
        position=(top right inside) offset=(-2,-2)pct frame ;

title bold f="MS Gothic" h=12pt "箱ひげ図: BOXPLOTプロシジャ" ;
proc boxplot data=GTEST1_S ;
   plot WEIGHT*_TREAT(VISIT)=TREAT / boxstyle=schematic boxwidth=4 totpanels=1
                                     vaxis=30 to 100 by 10 haxis=axis1 symbollegend=legend1 ;
   format _TREAT TRT2F. ;
   label _TREAT="" ;
run ; quit ;
title ;

*--------------------------------------------------------------- ; 

*** ⑥散布図&回帰直線 ****************************************** ;

*--- 1) Statistical Graphics Procedure-------------------------- ; 
*--- 回帰 ;
title bold f="MS Gothic" h=12pt '散布図&回帰直線: SGPANELプロシジャ' ;
proc sgpanel data=GTEST2 ;
   panelby GENDER / layout=columnlattice ;
   reg x=ABDCF y=WEIGHT / alpha=0.05 clm cli ;
run ;
title ;

title bold f="MS Gothic" h=12pt '散布図&回帰直線: SGSCATTERプロシジャ' ;
proc sgscatter data=GTEST2 ;
   compare x=(HEIGHT ABDCF) y=WEIGHT /reg ;
run ;

title bold f="MS Gothic" h=12pt '散布図行列: SGSCATTERプロシジャ1' ;
proc sgscatter data=GTEST2 ;
   matrix WEIGHT HEIGHT ABDCF / group=TREAT ;
run ;

title bold f="MS Gothic" h=12pt '散布図行列: SGSCATTERプロシジャ2' ;
proc sgscatter data=GTEST2 ;
   matrix WEIGHT HEIGHT ABDCF / diagonal=(histogram normal kernel) ;
run ;

*--------------------------------------------------------------- ; 

*** ⑦生存関数のグラフ ***************************************** ;

ods graphics on ;
proc lifetest data=GTEST3 plots=(survival(atrisk test cl) logsurv loglogs) ;
   strata TREAT / test=logrank ;
   time TIME*EVENT(2) ;
run ;
ods graphics off ;

*** ⑧ヒストグラムと密度推定 *********************************** ;

*--- 1) Statistical Graphics Procedure-------------------------- ; 

*--- 重ね描きOK ;
title bold f="MS Gothic" h=12pt 'ヒストグラム & 密度推定: SGPANELプロシジャ' ;
proc sgpanel data=GTEST2 ;
   panelby GENDER / layout=panel ;
   histogram WEIGHT / scale=percent ;
   density WEIGHT / type=normal name="aaa" legendlabel="Normal" ;
   density WEIGHT / type=kernel name="bbb" legendlabel="Kernel" ;
   keylegend "aaa" "bbb" ;
run ;
title ;

*--- Traditionalと同じ指定をODS Graphicsで出力 ;
ods graphics on ;
proc univariate data=GTEST2 ;
   var WEIGHT ;
   histogram WEIGHT / normal(color=red) kernel(color=blue)
                      cbarline=black cfill=lightgray ;
run ;
ods graphics off ;

*--- 2変量の場合bivarステートメントで3Dグラフ描画可能 ;
ods graphics on ;
proc kde data=GTEST2 ;
   bivar WEIGHT ABDCF / plots=(surface histogram) gridl = 0 gridu = 200 ngrid = 201 ;
   bivar WEIGHT ABDCF / plots=(histsurface)       gridl = 0 gridu = 200 ngrid = 201 ;
run ;
ods graphics off ;
*--------------------------------------------------------------- ; 

*** ⑨累積分布曲線 ********************************************* ;
*--- 1) ODS Graphics ------------------------------------------- ; 
ods graphics on ;
proc univariate data=GTEST2 ;
   class TREAT ;
   var WEIGHT ;
   cdfplot WEIGHT / overlay ;
run ;
ods graphics off ;

*--- 2) Traditional -------------------------------------------- ; 
proc sort data=GTEST2 out=_GTEST2 ;
   by TREAT ;
run ;
ods listing close ;
ods output Onewayfreqs=_CDF(keep=TREAT WEIGHT Cumpercent) ;
proc freq data=_GTEST2 ;
   by TREAT ;
   table WEIGHT ;
run ;
ods output close ;
ods listing ;

goptions reset=all ;
symbol1 c=blue i=j l=1 ;
symbol2 c=red  i=j l=3 ;
axis1 minor=none ;

proc gplot data=_CDF ;
   plot Cumpercent*WEIGHT=TREAT / vaxis=axis1 haxis=axis1 ;
run ; quit ;

*** ⑩フォレストプロット ************************************** ;

*--- 1) Statistical Graphics Procedure-------------------------- ; 
*--- 群間差(TTESTプロシジャ+SGPLOTのScatter) ;
proc sort data=GTEST1 ; by VISIT ; run ;

ods listing close ;
ods output Conflimits=_CLM(where=(method="Pooled")) ;
proc ttest data=GTEST1 ;
   by VISIT ; var FBS ; class TREAT ;
run ;
ods output close ;
ods listing ;

title bold f="MS Gothic" h=12pt "フォレストプロット: SGPLOTプロシジャ1" ;
proc sgplot data=_CLM ;
   scatter x=Mean y=VISIT / markerattrs=(symbol=circlefilled color=black)
                            xerrorlower=LowerCLMean
                            xerrorupper=UpperCLMean
                            errorbarattrs=(color=black) ;
   xaxis values=(-15 to  20 by  5) offsetmin=0.05 ;
   yaxis values=(100 to 130 by 10) offsetmin=0.05 ;
   refline VISIT / axis=y lineattrs=(pattern=1) transparency=0.6 ;
   refline 0     / axis=x lineattrs=(pattern=2) transparency=0.3 ;
   label Mean='Mean Difference and 95% CI' ;
run ;
title ;

*--- markercharで群間差と信頼区間の結果も出力 ;
data _CLM2 ;
   set _CLM ;
   X1 = "  Mean" ;
   X2 = " Lower CL" ;
   X3 = "Upper CL" ;
run ;

title bold f="MS Gothic" h=12pt "フォレストプロット: SGPLOTプロシジャ2" ;
proc sgplot data=_CLM2 noautolegend ;
   scatter x=Mean y=VISIT /
           xerrorlower=LowerCLMean xerrorupper=UpperCLMean
           markerattrs=(color=black symbol=circlefilled) ;
   scatter x=x1 y=VISIT / x2axis markerchar=Mean ;
   scatter x=x2 y=VISIT / x2axis markerchar=LowerCLMean ;
   scatter x=x3 y=VISIT / x2axis markerchar=UpperCLMean ;
 
   refline 0  / axis=x lineattrs=(pattern=2) ;
   refline 20 / axis=x lineattrs=(pattern=1 color=black) ;
   yaxis display=(nolabel) values=(100 to 130 by 10)
         discreteorder=unformatted ;
   xaxis values=(-15 to 20 by 5) offsetmax=0.35
         label='Mean Difference and 95% CI' ;
   x2axis display=(noticks nolabel)  offsetmin=0.7 offsetmax=0.05 ;
   format Mean 7.1 LowerCLMean UpperCLMean 7.2 ;
run ;
title ;
*--------------------------------------------------------------- ; 

*** ⑪棒グラフと折れ線グラフの重ね合わせ *********************** ;
*--- Statistical Graphics Procedure----------------------------- ; 
data URIAGE ;
   input YEAR URIAGE RIEKI ;
   label YEAR = "年" URIAGE = "売上高(億)" RIEKI = "純利益(億)" ;
cards;
2006 505 50 
2007 560 52
2008 660 65
2009 590 45
2010 690 66
;
run ;

title bold f="MS Gothic" h=12pt "棒グラフと折れ線グラフ: SGPLOTプロシジャ" ;
proc sgplot data=URIAGE ;
   vbar YEAR / response=URIAGE ;
   vline YEAR / response=RIEKI y2axis markers
                markerattrs=(color=black symbol=circlefilled) ;
   yaxis values=(200 to 800 by 200) ;
   y2axis values=(-20 to 100 by 20) ;
run ;

*--- Traditional Graphics Procedure ---------------------------- ; 
symbol v=dot color=black ;
axis1 offset=(5,5)pct ;
axis2 order=(200 to 800 by 200) minor=none length=70pct ;
axis3 order=(-20 to 100 by  20) minor=none length=70pct ;

title bold f="MS Gothic" h=12pt "棒グラフと折れ線グラフ: GBARLINEプロシジャ" ;
proc gbarline data=URIAGE ;
   bar YEAR / sumvar=URIAGE discrete maxis=axis1 raxis=axis2 space=2 ;
   plot / sumvar=RIEKI raxis=axis3 ;
run ; quit ;
title ;

*** ⑫AE発現率 & リスク比プロット ****************************** ;

*--- Statistical Graphics Procedure----------------------------- ; 

*--- あらかじめAEの発現率,リスク比とその信頼区間を格納したデータセットを作成する ;
data AEPLOT ;
   input PTNAME $14. TREAT1P TREAT2P RELRISK L_RR U_RR ;
cards;
胃腸炎         7  5  1.4     0.4597   4.26369
咽頭炎         8  7  1.14286 0.43074  3.03225
下腹部痛       5  4  1.25    0.34573  4.51939
下痢           4  5  0.8     0.22127  2.89241
過敏性腸症候群 8  4  2       0.62211  6.42972
外耳炎         5  5  1       0.29873  3.34746
角膜炎         3  7  0.42857 0.11405  1.61043
感染性腸炎     4  4  1       0.2572   3.88803
肝機能異常     8  5  1.6     0.54204  4.72293
発疹           6  4  1.5     0.43651  5.15455
発熱           5  5  1       0.29873  3.34746
;
run ;

proc print data=AEPLOT noobs ;
run ;

*--- GTLでテンプレート作成 ;
proc template ;
  define statgraph _AEPCTRR ;
    dynamic YVAR VAR1 VAR2 EST LOWER UPPER TITLE VLABEL HLABEL ;
    begingraph ;
      entrytitle TITLE ;
      layout lattice / columns=2 rows=1 columngutter=5px
                       columnweights=(.5 .5) rowdatarange=union ;
        rowaxes ; rowaxis / label=VLABEL ; endrowaxes ;
        columnaxes ; columnaxis / label=HLABEL ; endcolumnaxes ;

        layout overlay /
          xaxisopts=(linearopts=(viewmin=0 viewmax=20)) ;
          referenceline y=YVAR /
                        lineattrs=(color=lightgray pattern=2) ;
          scatterplot x=VAR1 y=YVAR /
                      markerattrs=(symbol=trianglefilled)
                      name="A" legendlabel="薬剤A" ;
          scatterplot x=VAR2 y=YVAR /
                      markerattrs=(symbol=square)
                      name="B" legendlabel="薬剤B";
          discretelegend "A" "B" / valign=bottom ;
        endlayout ;

        layout overlay /
          xaxisopts=(linearopts=(viewmin=0 viewmax=10
                     tickvaluesequence=(start=0 end=10 increment=1))
                    ) ;
          referenceline x=1 / lineattrs=(color=lightgray pattern=1) ;
          referenceline y=YVAR /
                        lineattrs=(color=lightgray pattern=2) ;
          scatterplot x=EST y=YVAR /
                      errorbarattrs=(pattern=1 color=black)
                      markerattrs=(color=black symbol=circlefilled)
                      xerrorlower=LOWER xerrorupper=UPPER ;
        endlayout ;
      endlayout ;
    endgraph ;
   end ;
run ;

proc sgrender data=AEPLOT template=_AEPCTRR ;
   label TREAT1P="Percentage" RELRISK='Relative Risk with 95% CI' ;
   dynamic 
     YVAR="PTNAME" VAR1="TREAT1P" VAR2="TREAT2P"
     EST="RELRISK" LOWER="L_RR" UPPER="U_RR" 
     TITLE="有害事象の発現率とリスク比: GTL and SGRENDERプロシジャ"
     HLABEL="Percentage and Relative Risks"
     VLABEL="有害事象名" ;
run ;
*--------------------------------------------------------------- ; 

*** ⑬AE発現率 Butterfly plot ************************************** ;
*--- Statistical Graphics Procedure----------------------------- ; 
ods listing style=journal ;
data AEPLOT2 ;
   set AEPLOT ;
   TREAT1P=(-1)*TREAT1P ; *--- 負の数に変換 ;
run ;
*--- pictureステートメントで負の数も正の数として出力するフォーマットを作成 ;
proc format ;
   picture POSF low - high="009" ;
run ;
*--- 左右逆方向に各薬剤群の棒グラフを出力 ;
proc sgplot data=AEPLOT2 ;
   hbar PTNAME / response=TREAT1P legendlabel="薬剤A" ;
   hbar PTNAME / response=TREAT2P legendlabel="薬剤B" ;
   xaxis values=(-10 to 10 by 5) grid ;
   label TREAT1P='発現率(%)' PTNAME="有害事象名" ;
   format TREAT1P TREAT2P POSF. ; 
run ;
ods listing style=default ;

goptions reset=all ;
ods rtf close ;



*** 5章演習問題 ;

*--- 1) ;
title "MPG(City) by Type" ;
proc sgplot data=sashelp.cars ;
   vbar TYPE / response=MPG_City group=TYPE stat=mean datalabel ;
   format MPG_City 8.1 ;
run ;
title ;

*--- 2) ;
title "MPG(City) by Origin and Type" ;
proc sgpanel data=sashelp.cars ;
   panelby ORIGIN / layout=panel rows=2 columns=2 novarname ;
   vbar TYPE / response=MPG_CITY group=TYPE stat=mean datalabel ;
   format MPG_City 8. ;
run ;
title ;