现在的位置: 首页 > 综合 > 正文

Learning PROC REPORT by Comparison

2018年10月23日 ⁄ 综合 ⁄ 共 4836字 ⁄ 字号 评论关闭

摘录自:

Learning PROC REPORT by Comparison
Keith Cranford, Office of the Attorney General of Texas, Austin, Texas

 

PROC PRINT COMPARISON

 

SIMPLE LISTING

*****PROC PRINT****;
title 'Simple Listing' ;
proc print data=sashelp.class ;
   var name sex age height weight ;
   title2 'PROC PRINT' ;
run ;
*****PROC REPORT****;
proc report data=sashelp.class nowd headskip ;
   column name sex age height weight ; /*COLUMN和PROC PRINT中VAR语句效果同 */
   define sex / width=3 ;              /*DEFINE设置属性选项*/
   title2 'PROC REPORT' ;
run ;
****specifying formats***** ;
proc report data=sashelp.class nowd headskip ;
   column Obs name sex age height weight ;
   define sex / width=3 center ;
   define age / width=5 ;
   define height / format=6.1 ;
   define weight / format=6.1 ;
   define obs / computed ;
   compute obs ;
      count+1 ;
      obs=count ;
   endcomp ;
   title2 'PROC REPORT, again' ;
run ;

 

SORTED LISTING

****proc sort,proc print*****;
proc sort data=sashelp.class out=class ;
   by sex ;
run ;
title 'Sorted Listing' ;
proc print data=class ;
   var name age ;
   by sex ;
   id sex ;
   title2 'PROC PRINT' ;
run ;
****proc report*****;
proc report data=sashelp.class nowd headskip ;
   column sex name age ;
   define sex / order width=3 ;
   break after sex / skip ;          /*brack after ,skip每组后插入空格*/
   title2 'PROC REPORT' ;
run ;

 

LISTING WITH SUBTOTALS

*****proc print, suby******;
title 'Subtotals' ;
proc print data=class ;
   var name age ;
   by sex ;
   id sex ;
   sumby sex ;                       /*sumby对所有数值型变量求和*/
   title2 'PROC PRINT' ;
run ;
*******proc report*********;
proc report data=sashelp.class nowd ;
   column sex name age ;
   define sex / order width=3 ;
   define age / analysis sum format=4.;
   break before sex / skip ;
   break after sex / summarize ol ; /*ol,(overline)跨线*/
   rbreak after / summarize dol ;   /*dol,(double overline)双跨线*/
   title2 'PROC REPORT' ;
run ;

*****with other statistics such as mean*****;
proc report data=sashelp.class nowd ;
   column sex sex2 name age ;        
   define sex / order width=3 noprint ;
   define sex2 / computed 'Sex' ;    /*揭示sex2是计算列*/
   define age / mean format=best4. ;  /*求mean*/
   break before sex / skip ;
   break after sex / summarize ol ;
   rbreak after / summarize dol ;
   compute sex2 / character length=15 ; /*详细说明sex2的属性*/
       sex2=sex ;
   endcomp ;
   compute after sex ;
       sex2='Average for ' || sex ;    /*改变sex2的显示为Average for F or M*/
endcomp ;
compute after ;
       sex2='Overall Average' ;
endcomp ;
title2 'PROC REPORT and more' ;
run ;
****creating a new column ***;
proc format ;
   value $gender 'F'='females'
                 'M'='males' ;
run ;
proc report data=sashelp.class nowd headline ;
    column sex name age ;
    define sex / order width=3 ;
    define age / mean ;
    break after sex / skip ;
    compute after sex ;
       text='Average for ' || strip(put(sex, $gender.)) ||
                       ' is ' || put(age.mean, 4.1) ;
           line ' ';                  /*line 语句与data 步中的put语句类似*/
       line text $40. ;            
    endcomp ;
    compute after ;
       line 'Overall Average is ' age.mean 4.1 ;
    endcomp ;
    title2 'PROC REPORT and more, again' ;
run ;

 

PROC TABULATE COMPARISON

SUMMARY STATISTICS

***proc tabulate***;
title 'Summary Statistics' ;
proc tabulate data=sashelp.class ;
   class sex ;
   var age ;
   table sex, age*(n*f=4. mean*f=6.2) ;
   title2 'PROC TABULATE' ;
run ;

proc report data=sashelp.class nowd split='~' box ;
   column sex age age=avg_age ;
   define sex / group width=3 ;
   define age / n 'N' format=4.0 width=4;
   define avg_age / mean 'Average~Age' format=6.2 width=7;
   title2 'PROC REPORT' ;
run ;
**Removing the BOX option**;
proc report data=sashelp.class nowd split='~' headline ;
   column sex age age=avg_age ;
   define sex / group width=3 ;
   define age / n 'N' format=4.0 width=4;
   define avg_age / mean 'Average~Age' format=6.2 width=7;
   title2 'PROC REPORT' ;
run ;

Cross-Tabulation

***Cross-Tabulation****;
options missing='0' ;
title 'Cross-tabulation' ;
proc tabulate data=sashelp.class ;
   class sex age ;
   table age, sex*n*f=4. ;
   title2 'PROC TABULATE' ;
run ;

proc report data=sashelp.class nowd headline ;
   column age sex ;
   define age / group ;
   define sex / across width=3 right ;
   title2 'PROC REPORT' ;
run
;

 

CROSS-TABULATION WITH SUMMARY

***CROSS-TABULATION WITH SUMMARY***;
options missing='?';
title 'Cross-tabulation with Summary' ;
proc tabulate data=sashelp.class ;
   class sex age ;
   var height ;
   table age, sex*height*mean*f=6.1 ;
   title2 'PROC TABULATE' ;
run ;

proc report data=sashelp.class nowd split='~' headline ;
   column age sex, height ;
   define age / group ;
   define sex / across '__Sex__' width=3 right ;
   define height / analysis mean 'Average~Height'
   format=6.1 width=8 ;
   title2 'PROC REPORT' ;
run ;

 

PERCENTAGES

***PERCENTAGES*****;
title 'Percentages' ;
proc tabulate data=sashelp.class ;
   class sex ;
   table sex, n pctn<sex> ;
   title2 'PROC TABULATE' ;
run ;

proc report data=sashelp.class nowd box ;
   column sex N pctn ;
   define sex / group width=3;
   define n / width=5 ;
   define pctn / '%' format=percent7.1 ;
   title2 'PROC REPORT' ;
run ;

PROC MEANS COMPARISON

***SUMMARY STATISTICS****;
title 'Summary Statistics 2' ;
proc means data=sashelp.class mean std min max maxdec=1;
   class sex ;
   var height ;
   title2 'PROC MEANS' ;
run ;

proc report data=sashelp.class nowd split='~' headline ;
   column sex height, (n mean std min max) ;
   define sex / group width=3 ;
   define height / '__Height__' ;
   define n / 'N' width=3 ;
   define mean / 'Mean' format=5.1 ;
   define std / 'Std~Dev' format=5.2 ;
   define min / 'Min' format=5.1 ;
   define max / 'Max' format=5.1 ;
   title2 'PROC REPORT' ;
run ;

抱歉!评论已关闭.