*--- 4.7 複雑なレポートの作成 ;
*--- データ作成 ;
data DEMOG ;
   input ID TREAT AGE GENDER HEIGHT WEIGHT SMOKE @@ ;
cards;
1  1  86 2  187.2 67.6  2 21 2  64 1  170.7 63.5  2
2  2  52 1  157.6 55.4  2 22 1  40 1  166.4 42.4  2
3  2  59 1  159.0 45.5  1 23 1  58 1  165.3 75.2  2
4  1  50 2  176.6 64.2  2 24 2  48 2  158.6 63.4  2
5  1  71 2  177.1 55.0  1 25 1  45 1  156.7 52.9  2
6  2  52 1  148.4 44.4  1 26 1  42 1  144.6 52.6  2
7  1  52 2  171.8 51.2  1 27 1  61 2  177.3 65.1  2
8  1  48 2  162.7 68.9  1 28 1  58 1  163.6 62.7  1
9  1  66 1  160.5 74.4  2 29 2  63 1  170.2 65.6  1
10 2  69 2  174.3 50.8  2 30 1  71 2  190.2 75.5  2
11 1  58 1  149.9 61.9  1 31 2  48 2  169.7 81.1  1
12 2  38 2  163.5 66.9  2 32 2  49 2  177.8 74.9  1
13 1  51 2  153.1 51.9  1 33 1  72 1  165.4 50.5  2
14 2  49 2  175.9 56.8  2 34 2  53 1  163.7 59.9  1
15 1  37 2  170.4 41.8  2 35 2  46 1  153.8 54.7  1
16 1  51 1  165.2 68.9  1 36 1  51 1  158.3 55.7  1
17 2  34 2  188.0 52.7  2 37 2  57 1  156.9 70.3  1
18 2  55 1  173.0 48.1  2 38 2  57 1  162.4 59.5  2
19 1  57 2  180.9 54.3  2 39 2  55 1  141.8 77.8  2
20 1  40 2  162.2 67.5  1 40 2  45 1  171.9 51.6  2
;
run ;

*--- (1) 連続変数 ;
*--- 1. 各薬剤群の要約統計量の算出 ;
proc summary data=DEMOG nway ;
   var AGE ;
   class TREAT  ;
   output out=_AGE(drop=_FREQ_ _TYPE_) n=_N mean=_MEAN stddev=_SD ;
run ;
*--- 2. 統計量のラベル指定 ;
data _AGE ;
   set _AGE ;
   label _N="N" _MEAN="Mean" _SD="SD" ;
run ;
*--- 3. データセットの転置(薬剤群を横に展開) ;
proc transpose data=_AGE out=_AGE_T prefix=_GRP ;
   var _N _MEAN _SD ;
   id TREAT ;
run ;
*--- 4. 統計量の値を各フォーマットで文字列に変換 ;
data RES_AGE ;
   length LABEL _LABEL_ _RES1 _RES2 $20. ;
   set _AGE_T ;

   LABEL = "Age(years)" ;

   if _NAME_ = "_N"    then do ; *--- 例数を文字列変換 ;
      _RES1 = put(_GRP1,best.) ;
      _RES2 = put(_GRP2,best.) ;
   end ;
   if _NAME_ = "_MEAN" then do ; *--- 平均値を文字列変換 ;
      _RES1 = put(_GRP1,8.1) ;
      _RES2 = put(_GRP2,8.1) ;
   end ;
   if _NAME_ = "_SD"   then do ; *--- 標準偏差を文字列変換 ;
      _RES1 = put(_GRP1,8.2) ;
      _RES2 = put(_GRP2,8.2) ;
   end ;
   keep LABEL _LABEL_ _RES1 _RES2 ;
run ;

*--- (2) カテゴリ変数 ;
*--- 1. 各薬剤群のカテゴリごとの例数とパーセントを算出 ;
ods listing close ;
proc tabulate data=DEMOG out=_GENDER ;
   class TREAT GENDER ;
   var WEIGHT ;
   table TREAT,GENDER*(n rowpctn)  ;
run ;
ods listing ;
*--- 2. 例数とパーセントを文字列変換して例数(%)に結合 ;
data _GENDER ;
   set _GENDER ;
   _RES=put(N,best.)||"("||right(put(pctn_10,6.1))||")" ;
run ;
*--- 3. データセットの転置(薬剤群を横に展開) ;
proc sort data=_GENDER ; by GENDER ; run ;
proc transpose data=_GENDER out=_GENDER_t prefix=_RES ;
   by GENDER ;
   var _RES ;
   id TREAT ;
run ;
*--- 4. カテゴリ変数のフォーマットを作成 ;
proc format ;
   value GENDERF 1="Male" 2="Female" ;
run ;
*--- 5. カテゴリの値にフォーマットを割り当てて文字列変換 ;
data RES_GENDER ;
   length LABEL _LABEL_ $20. ;
   LABEL="Gender" ;
   set _GENDER_t ;
   _LABEL_ = left(put(GENDER,GENDERF.)) ;
   keep LABEL _LABEL_ _RES1 _RES2 ; 
run ;

*--- (3) レポートの作成 ;
*--- 1. 連続変数とカテゴリ変数の結果を縦結合 ;
data RESULT ;
   set RES_AGE RES_GENDER ;
run ;

*--- 2. レポートの出力(styleステートメントで属性をカスタマイズ) ;
ods listing close ;
ods rtf file="C:\temp\report.rtf"
    style=minimal ;
proc report data=RESULT nowd split="#"
 style(report)={frame=hsides rules=none background=white cellpadding=0pt}
 style(header)={fontfamily="MS Gothic"   fontsize=10pt height=15pt
                just=c vjust=bottom}
 style(column)={fontfamily="Courier New" fontsize=10pt height=15pt
                    vjust=c} ;
 column LABEL _LABEL_ _RES1 _RES2 DEF ;
 define LABEL   / order "項目" style={width=80pt} ;
 define _LABEL_ / display "統計量/カテゴリ" style={width=80pt} ;
 define _RES1   / display "薬剤1" style(column)={just=r width=80pt} ;
 define _RES2   / display "薬剤2" style(column)={just=r width=80pt} ;
 define DEF     / computed noprint ;

 compute DEF ; *--- 項目ごとの横罫線の制御 ;
  if LABEL ne "" then do ;
   call define(_ROW_,'style','style={bordertopstyle=solid bordertopwidth=1}') ;
  end ;
 endcomp ;
run ;

ods rtf close ;
ods listing ;

*--- 要約統計量と検定結果の出力 ;
*--- 要約統計量の算出 ;
proc summary data=DEMOG nway ;
   class TREAT ;
   var WEIGHT ;
   output out=_OUT(drop=_TYPE_ _FREQ_) n=N mean=MEAN stddev=SD ;
run ;
*--- 2標本t検定の実行 ;
ods listing close ;
ods output Ttests=_TTEST(where=(method="Pooled")) ;
proc ttest data=DEMOG ;
   class TREAT ;
   var WEIGHT ;
run ;
ods output close ;
ods listing ;
*--- 計算結果を格納したデータセットの結合 ;
data _OUT ; merge _OUT _TTEST ; run ;
*--- 解析対象変数のラベルの格納 ;
data _OUT ;
   length LABEL $20. ;
   set _OUT ;
   LABEL = "Weight(kg)" ;
run ;
*--- 欠測値の空白出力 ;
options missing=" " ;
*--- 薬剤群変数のフォーマット作成 ;
proc format ;
   value TRTRF 1 = "薬剤1" 2 = "薬剤2" ;
run ;
*--- レポートの作成 ;
ods listing close ;
ods rtf file="%sysfunc(pathname(work))\report.rtf" style=minimal ;
proc report data=_OUT nowd
   style(report)={frame=hsides rules=rows}
   style(header)={background=white fontfamily="Courier New"}
   style(column)={background=white fontfamily="Courier New"} ;
   column LABEL TREAT N MEAN SD tValue Probt _LINE ;
   define LABEL  / order ;
   define TREAT  / display format=TRTRF. ;
   define N      / display format=best. ;
   define MEAN   / display format=8.1 ;
   define SD     / display format=8.2 ;
   define _LINE  / computed noprint ;
   define tValue / display "t-value" format=8.3 ;
   define Probt  / display "p-value" format=8.3 ;
   compute _LINE ; *--- 1行目と2行目の間の罫線出力の制御 ;
      if LABEL ne "" then do ;
         call define('_C1_','style','style={borderbottomstyle=none}') ;
         call define('_C6_','style','style={borderbottomstyle=none}') ;
         call define('_C7_','style','style={borderbottomstyle=none}') ;
      end ;
   endcomp ;
run ;
ods rtf close ;
ods listing ;



*** 4章演習問題 ;

*--- 1) ;
options linesize=200 ;
proc tabulate data=sashelp.cars ;
   class Make ;
   var MPG_City ;
   table Make,MPG_City*(n mean stddev min median max) ;
run ;

proc means data=sashelp.cars n mean stddev min median max ;
   class Make ;
   var MPG_City ;
run ;

proc report data=sashelp.cars nowd ;
   column Make
          (MPG_CITY=MPG_N   MPG_CITY=MPG_MEAN MPG_CITY=MPG_SD
           MPG_CITY=MPG_MIN MPG_CITY=MPG_MED  MPG_CITY=MPG_MAX) ;
   define Make     / group width=15 ;
   define MPG_N    / analysis n      "N" ;
   define MPG_Mean / analysis mean   "Mean" ;
   define MPG_SD   / analysis std    "SD" ;
   define MPG_MIN  / analysis min    "Min" ;
   define MPG_MED  / analysis median "Median" ;
   define MPG_MAX  / analysis max    "Max" ;
quit ;

ods pdf style=default ;
ods rtf style=default ;

*---2) ;
proc tabulate data=sashelp.cars ;
   class Make ;
   var MPG_City ;
   table Make,MPG_City*(n mean stddev min median max) ;
run ;

proc means data=sashelp.cars n mean stddev min median max ;
   class Make ;
   var MPG_City ;
run ;

proc report data=sashelp.cars nowd ;
   column Make
          (MPG_CITY=MPG_N   MPG_CITY=MPG_MEAN MPG_CITY=MPG_SD
           MPG_CITY=MPG_MIN MPG_CITY=MPG_MED  MPG_CITY=MPG_MAX) ;
   define Make     / group width=15 ;
   define MPG_N    / analysis n      "N" ;
   define MPG_Mean / analysis mean   "Mean" ;
   define MPG_SD   / analysis std    "SD" ;
   define MPG_MIN  / analysis min    "Min" ;
   define MPG_MED  / analysis median "Median" ;
   define MPG_MAX  / analysis max    "Max" ;
quit ;

ods rtf close ;
ods pdf close ;